问题描述
我有一个 perl 程序,可以从我大学图书馆的数据库中检索数据,并且运行良好.现在我想用python重写它但遇到了问题
I have a perl program that retrieves data from the database of my university library and it works well. Now I want to rewrite it in python but encounter the problem<urlopen error [errno 104] connection reset by peer>
perl 代码为:
my $ua = LWP::UserAgent->new;
$ua->cookie_jar( HTTP::Cookies->new() );
$ua->timeout(30);
$ua->env_proxy;
my $response = $ua->get($url);
我写的python代码是:
The python code I wrote is:
cj = CookieJar();
request = urllib2.Request(url); # url: target web page
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj));
opener = urllib2.install_opener(opener);
data = urllib2.urlopen(request);
我在家使用VPN(虚拟专用网络)登录我大学的图书馆,我尝试了perl代码和python代码.perl 代码按我的预期工作,但 python 代码总是遇到urlopen 错误".
I use VPN(virtual private network) to log in my university's library at home, and I tried both the perl code and python code. The perl code works as I expected, but the python code always encountered "urlopen error".
我用谷歌搜索了这个问题,似乎 urllib2 无法加载环境代理.但是根据 urllib2 的文档,urlopen() 函数与不需要身份验证的代理透明地工作.现在我觉得很困惑.有人能帮我解决这个问题吗?
I googled for the problem and it seems that the urllib2 fails to load the environmental proxy. But according to the document of urllib2, the urlopen() function works transparently with proxies which do not require authentication. Now I feels quite confusing. Can anybody help me with this problem?
推荐答案
我尝试按照 Uku Loskit 和 Mikko Ohtamaa 的建议伪造 User-Agent 标头,并解决了我的问题.代码如下:
I tried faking the User-Agent headers as Uku Loskit and Mikko Ohtamaa suggested, and solved my problem. The code is as follows:
proxy = "YOUR_PROXY_GOES_HERE"
proxies = {"http":"http://%s" % proxy}
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
希望对其他人有用!
这篇关于python urllib2:对等方重置连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!