[英]How do I slow API calls for Binance API
What must I add to my code so I stop running into API rate limit errors?我必须在我的代码中添加什么才能停止运行 API 速率限制错误? I believe I run into this error because my script is making to many API calls to the Binance servers.
我相信我遇到了这个错误,因为我的脚本对 Binance 服务器进行了许多 API 调用。
My code is:我的代码是:
from binance.client import Client
client = Client(api_key=***, api_secret=***, tld='us')
The client module below uses the requests library.下面的客户端模块使用请求库。 The Client constructor has an optional parameter:
requests_params=None
and allows you to add a "Dictionary of requests params to use for all calls" (quote from documentation.) Client 构造函数有一个可选参数:
requests_params=None
,并允许您添加“用于所有调用的请求参数字典”(引用自文档。)
I have looked through the requests documentation but could not find anything to fix this issue.我查看了请求文档,但找不到任何解决此问题的方法。 I found another library called ratelimit but I do not know how to pass it through client() effectively.
我找到了另一个名为ratelimit的库,但我不知道如何有效地通过 client() 传递它。
The error message I receive is:我收到的错误信息是:
requests.exceptions.SSLError: HTTPSConnectionPool(host='api.binance.us', port=443): Max retries exceeded with url: /api/v1/ping (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available."))
You can simply add a delay using time.sleep
in between your requests.您可以在请求之间使用
time.sleep
简单地添加延迟。
from time import sleep
# Adds a delay of 3 seconds
sleep(3)
have you tried a decorator?你试过装饰师吗? In my opinion a very clean and beautiful way for your problem:-)
在我看来,一个非常干净和漂亮的方式来解决你的问题:-)
Here an example:这里有一个例子:
import requests
from functools import wraps
import time
def delay(sleep_time:int):
def decorator(function):
@wraps(function)
def wrapper(*args, **kwargs):
time.sleep(sleep_time)
print(f"Sleeping {sleep_time} seconds")
return function(*args, **kwargs)
return wrapper
return decorator
@delay(5)
def get_data(url:str) -> requests.models.Response:
return requests.get(url)
while True:
print(get_data("https://www.google.com"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.