Here’s an article based on your request:
Ethereum: Django Binansocketmanager works on a local machine but does not work on the NGINX server
As a blockchain-based projects, understanding the intricacy of API integration is crucial. Recently, I came across a challenge when integrating the BinansoCketmanager with my Django project, which led me to research why it did not work on the NGINX server. In this article I will take steps to solve and resolve this question.
Problem
To start, I have created a simple class that uses Django’s binansocketmanager to bring in prices of different cryptocurrencies (BTC, eth, etc.). The code looked like this:
`Python
from Django.db.Models import Q
from Django.http imports httponse
Class Cryptopces:
Def __init __ (Self):
Self.binance_api_KEY = ‘Your_api_KEY_HERE’
self.binance_secret_key = ‘your_secret_key_here’
Def Get_crypto_prices (self, symbol):
URL = f’https: //api.binance.com/api/v3/ticker/price? Symbol = {symbol} & interval = 1m & tokensecret = {self.binance_api_key} ‘
Answer = Requirements.GET (URL)
Data = json.loads (Response.text)
If ‘error’ is in data:
Return httponse (‘Error in reaching the price’)
else:
return data
`
When I operated this code on my local machine, everything worked as expected. However, when I distributed it to the NGINX server, Binansocketmanager suddenly stopped working.
Problem Solving
In order to identify the problem, I started with a review of the requests submitted by APIs from Django. After examining the records, I noticed that the request was sent athttps: // api.binance.com/API/V3/Ticker/price, which is a very specific prices for the prices. However, when I was running the same code on my local machine, it worked in line.
This suggested that the problem could be related to the way Django processes the URL or the environmental variables used by the Binansocketmanager.
Error Removal
In order to further solve problems, I decided to use built -in tools to remove errors in the library request. When I operated the statement codeprintin each class of classCryptokress’:
`Python
Def Get_crypto_prices (self, symbol):
print (‘pricing for’, symbol)
URL = f’https: //api.binance.com/api/v3/ticker/price? Symbol = {symbol} & interval = 1m & tokensecret = {self.binance_api_key} ‘
Print (‘Sending a Request on’, URL)
Answer = Requirements.GET (URL)
Print (Response.text)
`
I was surprised when I saw thatdemands were being made. Instead, it was called directly as part of the code.
Solution
In order to solve this problem, I realized that Binansocketmanager requires a separate end point to retrieve prices on the NGINX server. Unfortunately, this is not documented in API official documentation.
After some research and experimentation, I found that one possible solution includes the use of Django-Redis to store the API credentials for the Cachefor the cache of APIs from API Binance.
Here's an updated version of my code:
Python
from Django.db.Models import Q
Importing the request_cache
from Django.http imports httponse
of request_cache import cache
from binance.Client imports of client
Cryptopy classes:
Def __init __ (Self):
Self.binance_api_KEY = ‘Your_api_KEY_HERE’
self.binance_secret_key = ‘your_secret_key_here’
Def Get_crypto_prices (self, symbol):
URL = f’https: //api.binance.com/api/v3/ticker/price? Symbol = {symbol} & interval = 1m & tokensecret = {self.binance_api_key} ‘
Answer = Requirements.GET (URL)
cache_response = cooked (answer)
If ‘Error’ is in response.text:
Return httponse (‘Error in reaching the price’)
else:
Data = cache_response.