python urllib 得到 [Errno 套接字错误]

python urllib got [Errno socket error]

我正在练习 google-python 课程。但是,我遇到了 urllib 的问题。这是简化的代码:

import urllib
url = 'http://www.google.com'
ufile=urllib.urlopen(url)

这段代码确实有效,但如果我将 url 更改为其他代码,例如:

url = 'https://developers.google.com/edu/python/utilities#exceptions'

它会引发错误:

IOError: [Errno socket error] [SSL: CERTIFICATE_VERIFY_FAILED]   certificate verify failed (_ssl.c:590)

完整错误信息如下:

Traceback (most recent call last):
  File "practice2.py", line 4, in <module>
    ufile=urllib.urlopen(url)
  File "/home/jren/Canopy/appdata/canopy-1.6.2.3262.rh5-x86_64/lib/python2.7/urllib.py", line 87, in urlopen
    return opener.open(url)
  File "/home/jren/Canopy/appdata/canopy-1.6.2.3262.rh5-x86_64/lib/python2.7/urllib.py", line 213, in open
    return getattr(self, name)(url)
  File "/home/jren/Canopy/appdata/canopy-1.6.2.3262.rh5-x86_64/lib/python2.7/urllib.py", line 443, in open_https
    h.endheaders(data)
  File "/home/jren/Canopy/appdata/canopy-1.6.2.3262.rh5-x86_64/lib/python2.7/httplib.py", line 1049, in endheaders
    self._send_output(message_body)
  File "/home/jren/Canopy/appdata/canopy-1.6.2.3262.rh5-x86_64/lib/python2.7/httplib.py", line 893, in _send_output
    self.send(msg)
  File "/home/jren/Canopy/appdata/canopy-1.6.2.3262.rh5-x86_64/lib/python2.7/httplib.py", line 855, in send
    self.connect()
  File "/home/jren/Canopy/appdata/canopy-1.6.2.3262.rh5-x86_64/lib/python2.7/httplib.py", line 1274, in connect
    server_hostname=server_hostname)
  File "/home/jren/Canopy/appdata/canopy-1.6.2.3262.rh5-x86_64/lib/python2.7/ssl.py", line 352, in wrap_socket
    _context=self)
  File "/home/jren/Canopy/appdata/canopy-1.6.2.3262.rh5-x86_64/lib/python2.7/ssl.py", line 579, in __init__
    self.do_handshake()
  File "/home/jren/Canopy/appdata/canopy-1.6.2.3262.rh5-x86_64/lib/python2.7/ssl.py", line 808, in do_handshake
    self._sslobj.do_handshake()
IOError: [Errno socket error] [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)

非常感谢您阅读本文!

1 检查 SSL 版本

2 确保socket模块底层是支持SSL编译模式的,即开启SSL编译选项时

3端口错误,检查

Python 对 ssl . You can temporarily bypass the ssl verification. Take a look at this post 严格得多。当然,这可能不是一个永久的解决方案,但如果您无权配置 developers.google.com 证书,那可能是您能做的最好的。