是否可以连接到https URL?

时间:2019-05-02 06:32:55

标签: python python-3.x ssl websocket python-socketio

我有一个简单的代码,使用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)

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

最后,我成功了,我在另一台具有相同软件条件的Mac计算机上尝试了相同的代码,并且可以正常工作,并且在PC with UbuntuRaspberry Pi with Raspbian上也可以正常工作,所以一切正常再次在有问题的第一台Mac上,我再次开始调试,使用pip重新安装和更新软件包,什么都没发生。

我尝试过的其他解决方案是使用certifi重新安装pip程序包,但未解决任何问题。

最后,解决问题的是运行Python随附的脚本,该脚本位于/Applications/Python 3.X/Install Certificates.command中,您可以直接在Finder上运行它,也可以在终端运行两次到文件夹并执行。

这样做之后,我测试了python-socketio 3.1.2python-socketio 4.0.1,一切正常。

最后的建议是,如果尝试运行脚本后仍然出现相同的错误,可以尝试使用python-socketio的替代方法,以找出我尝试过的socketIO-client-2当我遇到错误时,该库在初始条件下运行良好(运行脚本之前)。运行上面的脚本后,两个脚本现在都可以完美运行。