Python 2.7 用python诊断代理问题
因此,我尝试使用Python2.7来完成各种需要从internet提取数据的工作。我不是很成功,我正在寻找帮助来诊断我做错了什么 首先,我通过像这样定义代理来让pip工作,Python 2.7 用python诊断代理问题,python-2.7,proxy,urllib2,urllib3,Python 2.7,Proxy,Urllib2,Urllib3,因此,我尝试使用Python2.7来完成各种需要从internet提取数据的工作。我不是很成功,我正在寻找帮助来诊断我做错了什么 首先,我通过像这样定义代理来让pip工作,pip安装--proxy=http://username:password@someproxy.com:8080numpy因此python必须能够通过它 然而,当涉及到实际编写一个可以实现同样功能的.py脚本时,我没有成功。我首先尝试在urllib2中使用以下代码: import urllib2 uri = "http://
pip安装--proxy=http://username:password@someproxy.com:8080numpy
因此python必须能够通过它强>
然而,当涉及到实际编写一个可以实现同样功能的.py脚本时,我没有成功。我首先尝试在urllib2中使用以下代码:
import urllib2
uri = "http://www.python.org"
http_proxy_server = "someproxyserver.com"
http_proxy_port = "8080"
http_proxy_realm = http_proxy_server
http_proxy_user = "username"
http_proxy_passwd = "password"
# Next line = "http://username:password@someproxyserver.com:8080"
http_proxy_full_auth_string = "http://%s:%s@%s:%s" % (http_proxy_user,
http_proxy_passwd,
http_proxy_server,
http_proxy_port)
def open_url_no_proxy():
urllib2.urlopen(uri)
print "Apparent success without proxy server!"
def open_url_installed_opener():
proxy_handler = urllib2.ProxyHandler({"http": http_proxy_full_auth_string})
opener = urllib2.build_opener(proxy_handler)
urllib2.install_opener(opener)
urllib2.urlopen(uri)
print "Apparent success through proxy server!"
if __name__ == "__main__":
open_url_no_proxy()
open_url_installed_opener()
然而,我只是得到了这个错误:
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>
raise MaxRetryError(_pool, url, error or ResponseError(cause)) MaxRetryError: HTTPSConnectionPool(host='www.python.org', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', error('Tunnel connection failed: 407 Proxy Authorization Required',)))
我得到了这个错误:
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>
raise MaxRetryError(_pool, url, error or ResponseError(cause)) MaxRetryError: HTTPSConnectionPool(host='www.python.org', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', error('Tunnel connection failed: 407 Proxy Authorization Required',)))
如果您能为我诊断此问题提供帮助,我将不胜感激。我的问题的解决方案是使用“请求”模块,请参阅下面的线程: mtt2p列出这个代码,它为我工作
import requests
import time
class BaseCheck():
def __init__(self, url):
self.http_proxy = "http://user:pw@proxy:8080"
self.https_proxy = "http://user:pw@proxy:8080"
self.ftp_proxy = "http://user:pw@proxy:8080"
self.proxyDict = {
"http" : self.http_proxy,
"https" : self.https_proxy,
"ftp" : self.ftp_proxy
}
self.url = url
def makearr(tsteps):
global stemps
global steps
stemps = {}
for step in tsteps:
stemps[step] = { 'start': 0, 'end': 0 }
steps = tsteps
makearr(['init','check'])
def starttime(typ = ""):
for stemp in stemps:
if typ == "":
stemps[stemp]['start'] = time.time()
else:
stemps[stemp][typ] = time.time()
starttime()
def __str__(self):
return str(self.url)
def getrequests(self):
g=requests.get(self.url,proxies=self.proxyDict)
print g.status_code
print g.content
print self.url
stemps['init']['end'] = time.time()
#print stemps['init']['end'] - stemps['init']['start']
x= stemps['init']['end'] - stemps['init']['start']
print x
test=BaseCheck(url='http://google.com')
test.getrequests()
您的代理是否在https://或http://上?在pip示例中是http://,但在urllib3示例中是https://.If 这不起作用,您可以尝试使用请求(构建在urllib3上,也被pip使用):是的,我使用了http和https,实际上,当我使用urllib3将其设置为http时,它没有任何错误,但是,它返回一个页面,告诉我代理需要身份验证。我尝试了一个带有请求的脚本,但我得到了类似的错误。我开始认为这与提供认证细节有关。可能是。奇怪的是皮普能工作。你确定pip实际上是在攻击代理而不是以某种方式忽略它吗?您可以使用tcpdump/ngrep之类的工具来监视流量,并查看它实际在做什么。例如。