使用HTTP PROXY - Python [英] Using an HTTP PROXY - Python
问题描述
我很熟悉我应该将HTTP_RPOXY环境变量设置为代理地址。
I familiar with the fact that I should set the HTTP_RPOXY environment variable to the proxy address.
通常urllib工作正常,问题是处理urllib2。
Generally urllib works fine, the problem is dealing with 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回答但我现在收到此错误:
Extra info:
urllib.urlopen(....) works fine! It is just urllib2 that is playing tricks...
I tried @Fenikso answer but I'm getting this error now:
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>
任何想法?
Any ideas?
推荐答案
即使没有HTTP_PROXY环境变量,也可以这样做。试试这个样本:
You can do it even without the HTTP_PROXY environment variable. Try this sample:
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
In您的情况似乎代理服务器似乎拒绝连接。
In your case it really seems that the proxy server is refusing the connection.
还有更多尝试:
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:
这似乎是一个受欢迎的问题/答案。但是今天我会使用第三方 requests
而不是模块。
Edit 2014:
This seems to be a popular question / answer. However today I would use third party requests
module instead.
对于一个请求,只需执行:
For one request just do:
import requests
r = requests.get("http://www.google.com",
proxies={"http": "http://61.233.25.166:80"})
print(r.text)
对于多个请求,请使用 Session
object,因此您不必在所有请求中添加 proxies
参数:
For multiple requests use Session
object so you do not have to add proxies
parameter in all your requests:
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 PROXY - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!