是否可以连接到 https URL?

Is it possible to connect to a https URL?

我有一个简单的代码 运行 可以很好地使用 http(不安全)但后来我将其更新为 https 时收到关于 ssl.SSLCertVerificationError

的错误

我正在使用具有有效 ssl 的 heroku 服务器,python 3.7.3 on a mac,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 计算机上尝试了相同的代码,它工作正常,在 PC with UbuntuRaspberry Pi with Raspbian 上也是如此,一切正常,因此再次回到第一个 Mac 问题,我开始再次调试,使用 pip 重新安装和更新软件包,但没有任何反应。

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

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

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

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