2015-01-07 50 views
0

Python的代理身份验证有没有人对为什么Urlib2版本返回网页的任何想法,而请求版本返回连接错误:与请求和Urlib2

[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.

urllib2的代码(工作):

import urllib2 

proxy = urllib2.ProxyHandler({'http': 'http://login:[email protected]:80'}) 
opener = urllib2.build_opener(proxy) 
urllib2.install_opener(opener) 
wPage = urllib2.urlopen('http://www.google.com/') 
print wPage.read(); 

请求的代码(不工作 - errno的10060):

import requests 

proxy = {"http": "http://login:[email protected]:80"} 
wPage = requests.get('http://www.google.com/', proxies=proxy) 
print wPage.text 

请求版本返回内联网网页,但在网页上出现错误。

我正在运行的Python 2.7

*编辑*

基于m170897017的建议,我看着在GET请求的差异。唯一的区别是在连接和代理连接。

的urllib2版本:

header: Connection: close

header: Proxy-Connection: close

请求版本:

header: Connection: Keep-Alive

header: Proxy-Connection: Keep-Alive

我强迫请求版本通过修改标头

header = { 
    "Connection": "close", 
    "Proxy-Connection": "close" 
} 

为GET请求,关闭两个那些连接两者现在都匹配,但Requests版本仍然不起作用。

+1

嗅包,看看HTTP GET是不同的。 –

回答

0

试试这个:通过不同的程序发送

import urllib2 

proxy = urllib2.ProxyHandler({'http': '1.1.1.1:9090'}) 
opener = urllib2.build_opener(proxy) 
urllib2.install_opener(opener) 
response = urllib2.urlopen('http://www.google.com/') 
datum = response.read().decode("UTF-8") 
response.close() 
print datum 
+0

嗨Dave,感谢您的回应,我很感激它,但它是在网页上不工作的请求代码 - 我可能应该做得更清楚。 – sarasimple