使用HTTP代理-Python

2024-05-28 11:17:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我熟悉这样一个事实:我应该将HTTP-RPOXY环境变量设置为代理地址。

通常urllib工作正常,问题是处理urllib2。

>>> urllib2.urlopen("http://www.google.com").read()

回报

urllib2.URLError: <urlopen error [Errno 10061] No connection could be made because the target machine actively refused it>

或者

urllib2.URLError: <urlopen error [Errno 11004] getaddrinfo failed>

额外信息:

urllib.urlopen(…..)工作正常!只是urllib2在耍花招……

我试过@Fenikso答案,但现在我得到了这个错误:

URLError: <urlopen error [Errno 10060] A connection attempt failed because the 
connected party did not properly respond after a period of time, or established
connection failed because connected host has failed to respond>      

有什么想法吗?


Tags: thehttperrorurllib2urllibconnection事实urlopen
3条回答

只是想提一下,如果需要访问https url,您可能还必须设置https_proxyOS环境变量。 在我的情况下,这对我来说并不明显,我花了几个小时试图发现这一点。

我的用例:Win 7,jython-standalone-2.5.3.jar,通过ez_setup.py安装setuptools

即使不使用HTTP_PROXY环境变量,也可以执行此操作。尝试此示例:

import urllib2

proxy_support = urllib2.ProxyHandler({"http":"http://61.233.25.166:80"})
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)

html = urllib2.urlopen("http://www.google.com").read()
print html

在您的情况下,代理服务器似乎确实在拒绝连接。


还有更多的尝试:

import urllib2

#proxy = "61.233.25.166:80"
proxy = "YOUR_PROXY_GOES_HERE"

proxies = {"http":"http://%s" % proxy}
url = "http://www.google.com/search?q=test"
headers={'User-agent' : 'Mozilla/5.0'}

proxy_support = urllib2.ProxyHandler(proxies)
opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler(debuglevel=1))
urllib2.install_opener(opener)

req = urllib2.Request(url, None, headers)
html = urllib2.urlopen(req).read()
print html

编辑2014: 这似乎是一个很受欢迎的问题/答案。不过,今天我将使用第三方^{}模块。

对于一个请求,请执行以下操作:

import requests

r = requests.get("http://www.google.com", 
                 proxies={"http": "http://61.233.25.166:80"})
print(r.text)

对于多个请求,请使用Session对象,这样就不必在所有请求中添加proxies参数:

import requests

s = requests.Session()
s.proxies = {"http": "http://61.233.25.166:80"}

r = s.get("http://www.google.com")
print(r.text)

我建议您使用“请求”模块。

它比内置的http客户端容易得多: http://docs.python-requests.org/en/latest/index.html

示例用法:

r = requests.get('http://www.thepage.com', proxies={"http":"http://myproxy:3129"})
thedata = r.content

相关问题 更多 >