gpt4 book ai didi

python - 是否可以连接到 https URL?

转载 作者:太空宇宙 更新时间:2023-11-03 13:30:45 24 4
gpt4 key购买 nike

我有一个使用 http(不安全)运行良好的简单代码,但随后我将其更新为 https,并收到有关 ssl.SSLCertVerificationError

的错误

我正在使用具有有效 ssl 的 heroku 服务器,当然,mac 上的 python 3.7.3,ssl.OPENSSL_VERSION= 1.1.0,certifi=2019.3 .9, Flask=1.0.2, Flask-SocketIO=3.3.2

代码如下:

import socketio

sio = socketio.Client()

@sio.on('connect')
def on_connect():
print('Connected ...')

@sio.on('message')
def on_message(data):
print('I received a message!')
print(data)

if __name__ == '__main__':
sio.connect('https://heroku.server.url')

这就是我使用 https 得到的结果:

Traceback (most recent call last):
File "/Users/calavraian/Devel/Projects/FlaskTesting/Client.py", line 21, in <module>
sio.connect('https://heroku.server.url')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/socketio/client.py", line 208, in connect
engineio_path=socketio_path)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/engineio/client.py", line 166, in connect
url, headers, engineio_path)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/engineio/client.py", line 303, in _connect_polling
if self._connect_websocket(url, headers, engineio_path):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/engineio/client.py", line 341, in _connect_websocket
cookie=cookies)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/websocket/_core.py", line 514, in create_connection
websock.connect(url, **options)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/websocket/_core.py", line 223, in connect
options.pop('socket', None))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/websocket/_http.py", line 126, in connect
sock = _ssl_socket(sock, options.sslopt, hostname)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/websocket/_http.py", line 260, in _ssl_socket
sock = _wrap_sni_socket(sock, sslopt, hostname, check_hostname)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/websocket/_http.py", line 239, in _wrap_sni_socket
server_hostname=hostname,
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 412, in wrap_socket
session=session
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 853, in _create
self.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 1117, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)

有什么想法吗?

最佳答案

终于我让它工作了,我在另一台具有相同软件条件的 Mac 计算机上尝试了相同的代码并且它工作正常,也在一台装有 Ubuntu 的 PC 和一台装有 Raspbian 的 Raspberry Pi 上,一切正常,所以回到第一台有问题的 Mac,我开始再次调试,使用 pip 重新安装和更新包,但没有任何反应。

我尝试过的其他解决方案是使用 pip 重新安装 certifi 包,但没有任何修复。

最后,解决问题的是运行Python自带的脚本,位于/Applications/Python 3.X/Install Certificates.command,可以直接从Finder 双击它或在终端上转到该文件夹​​并执行它。

完成此操作后,我测试了 python-socketio 3.1.2python-socketio 4.0.1,一切正常。

最后的建议,如果您在尝试运行脚本后仍然遇到相同的错误,您可以尝试使用 python-socketio 的替代方法,在解决这个问题的过程中我已经尝试过了socketIO-client-2 当我收到错误时,这个库在初始条件下运行良好(在运行脚本之前)。运行上面的脚本后,两者现在都运行良好。

关于python - 是否可以连接到 https URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55946816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com