【问题标题】:How to connect to HTTPS through proxy using urllib2 (in Python)如何使用 urllib2(在 Python 中)通过代理连接到 HTTPS
【发布时间】:2017-02-12 12:26:58
【问题描述】:

如果我尝试通过代理连接的网站是不安全的 (HTTP),那么我可以连接,但是如果它是安全的 (HTTPS),那么我就不能。

以下代码有效:

import urllib2

proxy_support = urllib2.ProxyHandler({'http':'xxx.xxx.xxx.xx'})
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)

html = urllib2.urlopen('http://www.example.com').read()

但是下面的代码不起作用,

proxy_support = urllib2.ProxyHandler({'https':'xxx.xxx.xxx.xx'})
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)

html = urllib2.urlopen('https://www.example.com').read()

相反,我得到以下回溯:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 431, in open
    response = self._open(req, data)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 449, in _open
    '_open', req)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1240, in https_open
    context=self._context)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1197, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 61] Connection refused>

【问题讨论】:

    标签: python-2.7 http https proxy urllib2


    【解决方案1】:

    根据https://docs.python.org/2/library/urllib2.html:

    在 2.7.9 版中更改:添加了 cafile、capath、cadefault 和 context。

    这个允许我连接到使用自签名 SSL 证书的本地 HTTPS 站点:

    html = urllib2.urlopen('http://www.example.com'),\
    context=ssl._https_verify_certificates(False)
    

    我在您的回溯中注意到与我的相似之处。就像您发布的那样,该代码适用于 Ubuntu 14.04 (Python 2.7.6),但不适用于 16.04 (Python 2.7.13),最后一个例外:

      File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
        return opener.open(url, data, timeout)
      File "/usr/lib/python2.7/urllib2.py", line 429, in open
        response = self._open(req, data)
      File "/usr/lib/python2.7/urllib2.py", line 447, in _open
        '_open', req)
      File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
        result = func(*args)
      File "/usr/lib/python2.7/urllib2.py", line 1241, in https_open
        context=self._context)
      File "/usr/lib/python2.7/urllib2.py", line 1198, in do_open
        raise URLError(err)
    urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>
    

    我不确定这是否对你有用。

    【讨论】:

      猜你喜欢
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      • 1970-01-01
      • 2014-04-11
      • 2015-04-15
      • 2010-11-05
      • 2023-03-16
      相关资源
      最近更新 更多