简体   繁体   English

如何减慢 API 调用 Binance API

[英]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.

相关问题 如何在精确时间在 Python 中获取 Binance API 价格? - How do I get Binance API prices in Python at a precise time? 如何使用币安 API 获取所有价格历史记录,以使用 Python 进行加密? - How do I get all the prices history with binance API for a crypto using Python? Binance API 如何使用 API 密钥构建 URL - Binance API how to build URL with API key 我如何使用币安 api 从符号中查看即时交易 - How can i see instant trades from symbol with binance api Binance API:如何使用python在binance中获取订单ID - Binance API: How to get an Order ID in binance using python 如何从 Binance API 提取实时数据并同时在 Matplotlib 上绘制? 我可以单独做,但不能一起做 - How can I pull live data from Binance API and simultaneously plot it on Matplotlib? I can do both individually but not together 如何仅从 Binance api 的 request_client.get_balance() function 获取余额值作为输出? - How do I get only the balance values as outputs from request_client.get_balance() function of Binance api? Binance Staking api,我无法输入 - Binance staking api, i cant imprt 我无法将币安 api 安装到 pycharm - I cant install binance api to pycharm 如何使用对以下 API 调用的请求? - how do I use requests for the below API calls?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM