Python 2.6.1中的urllib2是否支持通过https代理

发布于 2024-07-25 16:01:04 字数 1549 浏览 3 评论 0原文

Python 2.6.1 中的 urllib2 是否支持通过 https 进行代理?

我在 http://www.voidspace.org.uk/ 找到了以下内容python/articles/urllib2.shtml

注意

目前urllib2不支持 通过获取 https 位置 代理人。 这可能是一个问题。

我正在尝试自动登录网站并下载文档,我有有效的用户名/密码。

proxy_info = {
    'host':"axxx", # commented out the real data
    'port':"1234"  # commented out the real data
}

proxy_handler = urllib2.ProxyHandler(
                 {"http" : "http://%(host)s:%(port)s" % proxy_info})
opener = urllib2.build_opener(proxy_handler,
         urllib2.HTTPHandler(debuglevel=1),urllib2.HTTPCookieProcessor())
urllib2.install_opener(opener)

fullurl = 'https://correct.url.to.login.page.com/user=a&pswd=b' # example
req1 = urllib2.Request(url=fullurl, headers=headers)
response = urllib2.urlopen(req1)

我已经让它在类似的页面上工作,但没有使用 HTTPS,并且我怀疑它没有通过代理 - 它只是以与我没有指定代理时相同的方式卡住。 我需要通过代理出去。

我需要进行身份验证,但不使用基本身份验证,urllib2 在通过 https 站点时会计算出身份验证吗(我通过 url 向站点提供用户名/密码)?

编辑: 不,我测试了

   proxies = {
        "http" : "http://%(host)s:%(port)s" % proxy_info,
        "https" : "https://%(host)s:%(port)s" % proxy_info
    }

    proxy_handler = urllib2.ProxyHandler(proxies)

,但出现错误:

urllib2.URLError: urlopen 错误 [Errno 8] _ssl.c:480: EOF 发生于 违反协议

Does urllib2 in Python 2.6.1 support proxy via https?

I've found the following at http://www.voidspace.org.uk/python/articles/urllib2.shtml:

NOTE

Currently urllib2 does not support
fetching of https locations through a
proxy. This can be a problem.

I'm trying automate login in to web site and downloading document, I have valid username/password.

proxy_info = {
    'host':"axxx", # commented out the real data
    'port':"1234"  # commented out the real data
}

proxy_handler = urllib2.ProxyHandler(
                 {"http" : "http://%(host)s:%(port)s" % proxy_info})
opener = urllib2.build_opener(proxy_handler,
         urllib2.HTTPHandler(debuglevel=1),urllib2.HTTPCookieProcessor())
urllib2.install_opener(opener)

fullurl = 'https://correct.url.to.login.page.com/user=a&pswd=b' # example
req1 = urllib2.Request(url=fullurl, headers=headers)
response = urllib2.urlopen(req1)

I've had it working for similar pages but not using HTTPS and I suspect it does not get through proxy - it just gets stuck in the same way as when I did not specify proxy. I need to go out through proxy.

I need to authenticate but not using basic authentication, will urllib2 figure out authentication when going via https site (I supply username/password to site via url)?

EDIT:
Nope, I tested with

   proxies = {
        "http" : "http://%(host)s:%(port)s" % proxy_info,
        "https" : "https://%(host)s:%(port)s" % proxy_info
    }

    proxy_handler = urllib2.ProxyHandler(proxies)

And I get error:

urllib2.URLError: urlopen error
[Errno 8] _ssl.c:480: EOF occurred in
violation of protocol

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

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

发布评论

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

评论(3

清君侧 2024-08-01 16:01:04

Fixed in Python 2.6.3 and several other branches:

茶色山野 2024-08-01 16:01:04

我不确定您引用的 Michael Foord 的文章是否已更新到 Python 2.6.1 —— 为什么不尝试一下呢? 不要像您现在所做的那样告诉 ProxyHandler 该代理仅适用于 http,也可以将其注册为 https(当然,您应该在调用 ProxyHandler 之前将其格式化为变量一次,然后在dict):这可能有效,也可能无效,但是,你甚至没有尝试,而且那肯定不起作用!-)

I'm not sure Michael Foord's article, that you quote, is updated to Python 2.6.1 -- why not give it a try? Instead of telling ProxyHandler that the proxy is only good for http, as you're doing now, register it for https, too (of course you should format it into a variable just once before you call ProxyHandler and just repeatedly use that variable in the dict): that may or may not work, but, you're not even trying, and that's sure not to work!-)

春风十里 2024-08-01 16:01:04

如果将来其他人遇到这个问题,我想指出它现在确实支持 https 代理,请确保代理也支持它,否则您可能会遇到使 python 库陷入无限循环的错误(这种情况发生了)大部头书)。

有关更多信息,请参阅正在测试 https 代理支持的 python 源代码中的unittest:
http://svn.python.org/view/python/branches/release26-maint/Lib/test/test_urllib2.py?r1=74203&r2=74202&pathrev=74203

Incase anyone else have this issue in the future I'd like to point out that it does support https proxying now, make sure the proxy supports it too or you risk running into a bug that puts the python library into an infinite loop (this happened to me).

See the unittest in the python source that is testing https proxying support for further information:
http://svn.python.org/view/python/branches/release26-maint/Lib/test/test_urllib2.py?r1=74203&r2=74202&pathrev=74203

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文