Python urllib2 > HTTP 代理 > HTTPS 请求

发布于 2024-09-03 15:34:41 字数 1283 浏览 5 评论 0原文

这工作正常:

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()

但是,如果http更改为https

...
print urllib2.urlopen('https://www.google.com').read()

会出现错误:

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]

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

This work fine:

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()

But, if http change to https:

...
print urllib2.urlopen('https://www.google.com').read()

There are errors:

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]

Why and how solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东北女汉子 2024-09-10 15:34:42

将此行:更改

urllib2.ProxyHandler({'http': 'http://user:pass@proxy:3128'}))

为:

urllib2.ProxyHandler({'https': 'http://user:pass@proxy:3128'}))

它对我来说效果很好。

Change this line:

urllib2.ProxyHandler({'http': 'http://user:pass@proxy:3128'}))

to this:

urllib2.ProxyHandler({'https': 'http://user:pass@proxy:3128'}))

It works fine for me.

深爱不及久伴 2024-09-10 15:34:42

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

On Windows, errno 10060 is a winsock error meaning the connection timed out. Are you able to reach https://www.google.com from the same machine using a web browser with a proxy set to http://user:pass@proxy:3128 ? Are you sure your proxy server can handle both https and http on the same port?

抱着落日 2024-09-10 15:34:42

urllib2 的 文档 说明如下:

注意:目前 urllib2 不支持获取 https 位置
通过代理。然而,这可以通过扩展 urllib2 来启用
此食谱所示。

我必须承认上面的方法对于 Jython 2.5.3 并没有立即起作用,但我仍在尝试。

更新:我将此补丁应用于 Jython 2.5.3,并且它适用于我。我现在可以通过代理服务器获取 HTTPS 资源。

更新2:以下是通过 HTTP 代理使用基本身份验证查询 HTTPS 资源的代码(不要忘记先安装补丁(请参阅之前的更新)):

from suds.client import Client
from suds.transport.https import HttpAuthenticated

credentials = dict(username='...', password='...', proxy={'https': 'host:port', 'http': 'host:port'})
t = HttpAuthenticated(**credentials)
url = 'https://example.com/service?wsdl'
client = Client(url, transport=t)
print client.service.getFoo()

The documentation for urllib2 says the following:

Note: Currently urllib2 does not support fetching of https locations
through a proxy. However, this can be enabled by extending urllib2 as
shown in this recipe.

I must admit above recipe didn't work right away for Jython 2.5.3, but I'm still trying.

UPDATE: I applied this patch to Jython 2.5.3, and it worked for me. I can fetch HTTPS resources over a proxy server now.

UPDATE2: Here is the code to query HTTPS resources with Basic authentication over HTTP Proxy (DON'T FORGET TO INSTALL PATCH FIRST (see previous update)):

from suds.client import Client
from suds.transport.https import HttpAuthenticated

credentials = dict(username='...', password='...', proxy={'https': 'host:port', 'http': 'host:port'})
t = HttpAuthenticated(**credentials)
url = 'https://example.com/service?wsdl'
client = Client(url, transport=t)
print client.service.getFoo()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文