Python urllib2> HTTP代理> HTTPS请求

Sta*_*lav 10 python https proxy urllib2

这项工作很好:

import urllib2

opener = urllib2.build_opener(
                urllib2.HTTPHandler(),
                urllib2.HTTPSHandler(),
                urllib2.ProxyHandler({'http': 'http://user:pass@proxy:3128'}))
urllib2.install_opener(opener)
print urllib2.urlopen('http://www.google.com').read()
Run Code Online (Sandbox Code Playgroud)

但是,如果http更改为https:

...
print urllib2.urlopen('https://www.google.com').read()
Run Code Online (Sandbox Code Playgroud)

有错误:

Traceback (most recent call last):
  File "D:\Temp\6\tmp.py", line 13, in <module>
    print urllib2.urlopen('https://www.google.com').read()
  File "C:\Python26\lib\urllib2.py", line 124, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python26\lib\urllib2.py", line 389, in open
    response = self._open(req, data)
  File "C:\Python26\lib\urllib2.py", line 407, in _open
    '_open', req)
  File "C:\Python26\lib\urllib2.py", line 367, in _call_chain
    result = func(*args)
  File "C:\Python26\lib\urllib2.py", line 1154, in https_open
    return self.do_open(httplib.HTTPSConnection, req)
  File "C:\Python26\lib\urllib2.py", line 1121, in do_open
    raise URLError(err)
URLError: <urlopen error [Errno 10060]
Run Code Online (Sandbox Code Playgroud)

为什么以及如何解决这个问题?

小智 17

改变这一行:

urllib2.ProxyHandler({'http': 'http://user:pass@proxy:3128'}))
Run Code Online (Sandbox Code Playgroud)

对此:

urllib2.ProxyHandler({'https': 'http://user:pass@proxy:3128'}))
Run Code Online (Sandbox Code Playgroud)

这对我来说可以.


ʇsә*_*ɹoɈ 1

在 Windows 上,errno 10060 是一个 winsock 错误,表示连接超时。您是否可以使用代理设置为http://user:pass@proxy:3128 的网络浏览器从同一台计算机访问https://www.google.com?您确定您的代理服务器可以在同一端口上处理 https 和 http 吗?