python 2.5 中的 urllib2.proxyhandler

发布于 2024-09-19 20:55:42 字数 962 浏览 3 评论 0原文

在 Windows XP、Python 2.5 和 2.6 中,我测试了以下代码:

import urllib2
proxy= urllib2.ProxyHandler({'http': '127.0.0.1:8080'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
urllib2.urlopen('http://www.google.com/')

在上面的代码中,我从 httplib.py 的第 349 行得到了 BadStatusLine 异常。

我有一个运行在 127.0.0.1:8080 的代理,它可以工作(我可以设置浏览器以将其与 proxyswitchy 一起使用,当它打开时,我可以访问在它关闭时被阻止的网站[在中国])。

如果我将其更改为socks代理,

proxy= urllib2.ProxyHandler({'socks': '127.0.0.1:8080'})

则根本不使用该代理。

我从 Proxy with urllib2 的问题中得到了代码,它几乎完全相同 - 可能会发生什么错误的?

更新:urllib2不支持socks代理。

最终让它与curl一起工作:

c = pycurl.Curl()

#stupid GFW
if settings.CHINA:
    c.setopt(pycurl.PROXY, '127.0.0.1')
    c.setopt(pycurl.PROXYPORT, 8087)
    c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5)

In windows XP, python 2.5 and 2.6 I tested the following code:

import urllib2
proxy= urllib2.ProxyHandler({'http': '127.0.0.1:8080'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
urllib2.urlopen('http://www.google.com/')

In the above code I get a BadStatusLine exception from line 349 of httplib.py.

I have a proxy running at 127.0.0.1:8080 which works (I can set a browser to use it with proxyswitchy, and when it's on I can get to sites which are blocked when it's off [in China]).

if I change it to a socks proxy,

proxy= urllib2.ProxyHandler({'socks': '127.0.0.1:8080'})

Then the proxy is not used at all.

I got the code from the question at Proxy with urllib2 and it's almost exactly the same - what could be going wrong?

Update: urllib2 doesn't support socks proxies.

Eventually got it working with curl:

c = pycurl.Curl()

#stupid GFW
if settings.CHINA:
    c.setopt(pycurl.PROXY, '127.0.0.1')
    c.setopt(pycurl.PROXYPORT, 8087)
    c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5)

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

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

发布评论

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

评论(3

没有你我更好 2024-09-26 20:55:43

urllib2 ProxyHandler 并非设计用于支持 SOCKS 协议。也许这个答案会有所帮助。

The urllib2 ProxyHandler is not designed to support the SOCKS protocol. Perhaps this answer would help.

纵性 2024-09-26 20:55:43

假设您的本地代理是HTTP代理而不是socks代理。试试这个:

import urllib2
proxy= urllib2.ProxyHandler({'http': 'http://127.0.0.1:8080/'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
urllib2.urlopen('http://www.google.com/')

Assuming your local proxy is an HTTP proxy and not a socks proxy. Try this:

import urllib2
proxy= urllib2.ProxyHandler({'http': 'http://127.0.0.1:8080/'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
urllib2.urlopen('http://www.google.com/')
十秒萌定你 2024-09-26 20:55:43

更新:我位于中国的防火墙后面。这使问题变得更加复杂。 GFW 既破坏了连接,又造成了 DNS 中毒。

我还没有设法让任何 urllib2 解决方案正常工作。但 pycurl 似乎确实有效,并且它解决了“连接重置”问题。不过,fb/twitter 仍然被屏蔽。

将他们的 IPS 添加到我的主机文件中是可行的 - 因此对于更大规模的解决方案,设置 DNS 代理是必要的。

UPDATE: I am located behind the great firewall of china. This was compounding the problem. The gfw was both breaking connections and doing DNS poisoning.

I have not managed to get any of the urllib2 solutions working. But pycurl does seem to work and it gets around the "connection reset" problem. fb/twitter were still blocked though.

Adding their IPS to my hosts file works - so for a larger scale solution, setting up a dns proxy is necessary.

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