python - 使用urllib打开HTTPS链接失败

时间:2016-02-01 02:09:56

标签: python ubuntu https beautifulsoup urllib

我无法使用urlib打开具有 HTTPS 协议的链接。 我在Ubuntu中运行 python 2.7.1 ,使用家庭网络(无代理)。

它一直返回并且错误,如果我改为HTTP,它可以工作,我在这里缺少什么?

代码示例:

from BeautifulSoup import *

import urllib

url = "https://path/file.html"

html = urllib.urlopen(url).read()

返回错误:

Traceback (most recent call last): 
  File "/home/.../links.py", line 4, in <module> html = urllib.urlopen(url).read()
    html = urllib.urlopen(url).read()
  File "/usr/lib/python2.7/urllib.py", line 87, in urlopen
    return opener.open(url)
  File "/usr/lib/python2.7/urllib.py", line 213, in open
    return getattr(self, name)(url)
  File "/usr/lib/python2.7/urllib.py", line 443, in open_https
    h.endheaders(data)
  File "/usr/lib/python2.7/httplib.py", line 1048, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python2.7/httplib.py", line 892, in _send_output
    self.send(msg)
  File "/usr/lib/python2.7/httplib.py", line 854, in send
    self.connect()
  File "/usr/lib/python2.7/httplib.py", line 1273, in connect
    server_hostname=server_hostname)
  File "/usr/lib/python2.7/ssl.py", line 352, in wrap_socket
    _context=self)
  File "/usr/lib/python2.7/ssl.py", line 579, in __init__
    self.do_handshake()
  File "/usr/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)

2 个答案:

答案 0 :(得分:2)

我找到了解决方案,必须设置SSL代码。

我的代码中缺少这部分内容!

import requests
import json
import ssl

scontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
req = urllib.urlopen(url, context=scontext)
html = req.read()

` 这样就可以通过HTTPS网站

答案 1 :(得分:0)

这不是最佳答案。

我只在使用不正确的SSL证书的服务器上遇到此问题 - 例如https://pygame.org/

request中,可以选择禁用证书验证。

import requests

r = requests.get("https://pygame.org", verify=False)

html = r.content

验证脚本不起作用。没有验证脚本显示警告但有效。

但我在urllib中找不到此选项。