繁体   English   中英

python_binance websocket:如何使用 web socket 处理这个错误?

[英]python_binance websocket : How I can handle this error with web socket?

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 中的异常:回溯(最近一次调用):文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/connector.py”,第 986 行,在 _wrap_create_connection return await self._loop.create_connection(*args, **kwargs) # type: ignore[return-value] # noqa File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio /base_events.py”,第 1089 行,在 create_connection 传输中,协议 = 等待 self._create_connection_transport(文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py”,第 1119 行,在_create_connection_transport等待服务员文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/sslproto.py”,第534行,在data_received ssldata,appdata = self._sslpipe.feed_ssldata(data)文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/sslproto.py”,第 188 行,在 feed_ssldata self._sslobj.do_handshake() 文件“/Library/Frameworks/Py thon.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(最近一次调用最后):文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/threading.py”,第1009行,在_bootstrap_inner self.run()文件“/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() 文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3 .10/site-packages/binance/threaded_stream.py”,第 35 行,在 socket_listener 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 行,在创建 await 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) 文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/binance/client.py”,第 7344 行,在_get return await self._request_api('get', path, signed, version, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/binance/client .py”,第 7307 行,在 _request_api 中返回 await self._request(method, uri, signed, **kwargs) 文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/ binance/client.py”,第 7288 行,在 _request async 中使用 getattr(self.session, method)(uri, **kwargs) 作为响应:文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3 .10/site-packages/aiohttp/client.py”,第 1138 行,进入 self._resp = await self._coro 文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site- packages/aiohttp/client.py",第 535 行,在 _request conn = await self._connector.connect(File"/Library/Frameworks/Python. framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/connector.py”,第 542 行,在 connect proto = await self._create_connection(req, traces, timeout) 文件“/Library/Frameworks/Python. framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/connector.py”,第 907 行,在 _create_connection _,proto = await self._create_direct_connection(req, traces, timeout) 文件“/Library/Frameworks/ Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/connector.py”,第 1206 行,在 _create_direct_connection raise last_exc 文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3 .10/site-packages/aiohttp/connector.py”,第 1175 行,在 _create_direct_connection 传输中,proto = await self._wrap_create_connection(文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site -packages/aiohttp/connector.py",第 988 行,在 _wrap_create_connection 中从 exc aiohttp.client_exceptions.ClientConnectorCertific 引发 ClientConnectorCertificateError(req.connection_key, exc) ateError: 无法连接到主机 api.binance.com:443 ssl:True [SSLCertVerificationError: (1, '[SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:证书链中的自签名证书 (_ssl.c:997)')]

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

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

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

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

那为我修好了。

我在 Python 3.10 中遇到了这个错误

我没有“评论或投票的声誉”,所以我重新发布了对我有用的解决方案:

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

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

谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM