【问题标题】:python_binance websocket : How I can handle this error with web socket?python_binance websocket:如何使用 web socket 处理这个错误?
【发布时间】:2022-06-11 02:47:21
【问题描述】:
from binance.client import Client
from binance import ThreadedWebsocketManager
import pandas as pd

my_api = ""
my_secret = ""

client = Client(api_key=my_api, api_secret=my_secret, tld="com", testnet=True)

twm = ThreadedWebsocketManager(api_key=my_api, api_secret=my_secret)
twm.start()


def simple_bot(msg):
    ''' define how to process incoming WebSocket messages '''

    time = pd.to_datetime(msg["E"], unit="ms")
    price = float(msg["c"])

    print("Time: {} | Price: {}".format(time, price))

    if int(price) % 10 == 0:
        order = client.create_order(symbol="BTCUSDT", side="BUY", type="MARKET", quantity=0.1)
        print("\n" + 50 * "-")
        print("Buy {} BTC for {} USDT".format(order["executedQty"], order["cummulativeQuoteQty"]))
        print(50 * "-" + "\n")

        twm.stop()

twm.start_symbol_ticker_socket(callback=simple_bot, symbol="BTCUSDT")

这些是我的代码,当我尝试运行这些代码时,出现如下错误。

-----错误文本-----

线程 Thread-1 中的异常: 回溯(最近一次通话最后): _wrap_create_connection 中的文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/connector.py”,第 986 行 return await self._loop.create_connection(*args, **kwargs) # type: ignore[return-value] # noqa 文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py”,第 1089 行,在 create_connection 传输,协议 = 等待 self._create_connection_transport( _create_connection_transport 中的文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py”,第 1119 行 等待服务员 data_received 中的文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/sslproto.py”,第 534 行 ssldata,appdata = self._sslpipe.feed_ssldata(数据) 文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/sslproto.py”,第 188 行,在 feed_ssldata self._sslobj.do_handshake() 文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ssl.py”,第 974 行,在 do_handshake 中 self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:证书链中的自签名证书 (_ssl.c:997)

上述异常是以下异常的直接原因:

Traceback(最近一次调用最后一次): _bootstrap_inner 中的文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/threading.py”,第 1009 行 自我运行() 运行中的文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/binance/threaded_stream.py”,第 56 行 self._loop.run_until_complete(self.socket_listener()) 文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py”,第 646 行,在 run_until_complete 返回future.result() socket_listener 中的文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/binance/threaded_stream.py”,第 35 行 self._client = await AsyncClient.create(loop=self._loop, **self._client_params) 创建文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/binance/client.py”,第 7258 行 等待 self.ping() 文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/binance/client.py”,第 7379 行,在 ping 返回等待 self._get('ping', version=self.PRIVATE_API_VERSION) _get 中的文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/binance/client.py”,第 7344 行 return await self._request_api('get', path, signed, version, **kwargs) _request_api 中的文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/binance/client.py”,第 7307 行 return await self._request(method, uri, signed, **kwargs) _request 中的文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/binance/client.py”,第 7288 行 以 getattr(self.session, method)(uri, **kwargs) 作为响应: aenter 中的文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/client.py”,第 1138 行 self._resp = 等待 self._coro _request 中的文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/client.py”,第 535 行 conn = 等待 self._connector.connect( 文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/connector.py”,第 542 行,在连接中 proto = await self._create_connection(req, traces, timeout) _create_connection 中的文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/connector.py”,第 907 行 _, proto = await self._create_direct_connection(req, traces, timeout) _create_direct_connection 中的文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/connector.py”,第 1206 行 提高 last_exc _create_direct_connection 中的文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/connector.py”,第 1175 行 transp, proto = 等待 self._wrap_create_connection( _wrap_create_connection 中的文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/connector.py”,第 988 行 从 exc 引发 ClientConnectorCertificateError(req.connection_key, exc) aiohttp.client_exceptions.ClientConnectorCertificateError: 无法连接到主机 api.binance.com:443 ssl:True [SSLCertVerificationError: (1, '[SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:证书链中的自签名证书 (_ssl.c:997)' )]

我不明白为什么这不起作用。 你可以帮帮我吗!? 谢谢!

【问题讨论】:

    标签: python websocket binance


    【解决方案1】:

    我遇到了这个问题,最近在 Mac 上更新到 python 3.10。

    您需要进入 Applications/WhateverPythonYouAreUsing 文件夹并点击“Install Certificates.command”。

    它将打开一个终端并安装证书。

    这为我解决了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-01
      • 2021-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-11
      相关资源
      最近更新 更多