如何使用Python更改币安服务器时间?

问题描述 投票:0回答:4

问题

如何使用编程语言Python更改最大的加密货币交易所之一币安的服务器时间?

详情

  1. 我正在使用 python 库 `python-binance' 与币安进行交互。
  2. 当我输入下面的代码时,您会根据您所在的位置获得当地时间。
from binance.client import Client
from datetime import datetime

# client = Client('api_key' , 'api_secret')  # Binance API key

raw_server_time = client.get_server_time()
server_time = datetime.fromtimestamp(raw_server_time['serverTime']/1000.0)
server_time

由于我在法国,结果如下:

>>> datetime.datetime(2020, 5, 23, 12, 49, 10, 13000)
  1. 即使我在法国,我想让币安认为我在美国东部(例如纽约)。所以我尝试了 python-binance 库支持的 requests 库,如下所示。
proxies = {
    'http': 'http://168.169.146.12:8080'
}

# in the Client instantiation
client = Client(api_key, api_secret, {'proxies': proxies})

# or on an individual call
client.get_all_orders(symbol='BTCUSDT', requests_params={'proxies': proxies})

但是我收到以下错误消息。

>>> BinanceAPIException: APIError(code=-1021): Timestamp for this request was 1000ms ahead of the server's time.

您可以在 python-binance 库的文档页面中找到有关上述代码的更多详细信息。 http 服务器在网站 free-proxy.cz 中找到。下面还提供了纽约代理列表的屏幕截图。

time server vpn binance
4个回答
2
投票

如果您运行的是 Windows 10 或 Windows 11,请执行以下操作:

  1. 开始 -> 搜索日期和时间
  2. 确保您已将自动设置时间设置为
  3. 在打开的窗口中,您可以选择同步时钟,然后按立即同步

我有时会在电脑进入睡眠状态后观察到此问题。


2
投票

我在 Microsoft Visual Code(MVC) 中编写 python 代码时也遇到了这个错误。 当我保持机器人运行时,随着机器人处于活动状态,这种情况也会发生。所以我需要机器人来改变我的电脑时间。我找到了以下解决方案:

如何调整电脑时间:

以“管理员”身份运行程序并激活 WindowsTime 非常重要。 在“服务”中搜索 WindowsTime 并确保其正在运行/活动。如果需要,启动时自动运行

机器人启动时的这行代码:

import os

os.system('w32tm/resync')

随着时间的推移保持机器人活跃

在开始同步时间后,我正在监视来自 binance 的错误响应以查找时间戳错误。当找到时间戳消息时,它将再次重新同步我的计算机时间。

 try:
    ###put for example buy code here###

 except Exception as e:
    print(e)
    substring = 'Timestamp for this request was 1000ms ahead of the server'
    if substring in e.message:
       os.system('w32tm/resync')       

1
投票
import time
import win32api

client = Client(api_key, api_secret)
server_time = client.get_server_time()
gmtime=time.gmtime(int((server_time["serverTime"])/1000))
win32api.SetSystemTime(gmtime[0],gmtime[1],0,gmtime[2],gmtime[3],gmtime[4],gmtime[5],0) // Needed Administrator Permission

-2
投票

对于 Windows,请尝试以下操作:https://steemit.com/crypto/@biyi/how-to-resolve-binance-s-timestamp-ahead-of-server-s-time-challenge 净停止 w32time w32tm /取消注册 w32tm /注册 网络启动 w32time w32tm /重新同步

© www.soinside.com 2019 - 2024. All rights reserved.