2014-08-30 43 views
0

当我运行这个脚本时,我得到了很多错误。 进口的urllib,urllib2的为什么我会收到这些错误以及如何解决这些错误?

proxy = urllib2.ProxyHandler({ 
    'http': '127.0.0.1', 
    'https': '127.0.0.1' 
}) 
opener = urllib2.build_opener(proxy) 
urllib2.install_opener(opener) 
# this way both http and https requests go through the proxy 
urllib2.urlopen('http://www.google.com') 
urllib2.urlopen('https://www.google.com') 

我真的不明白这些错误,因此为什么我问。在这里,他们是:

Traceback (most recent call last): 
    File "C:\Python27\Craig.py", line 10, in <module> 
    urllib2.urlopen('http://www.google.com') 
    File "C:\Python27\lib\urllib2.py", line 127, in urlopen 
    return _opener.open(url, data, timeout) 
    File "C:\Python27\lib\urllib2.py", line 410, in open 
    response = meth(req, response) 
    File "C:\Python27\lib\urllib2.py", line 523, in http_response 
    'http', request, response, code, msg, hdrs) 
    File "C:\Python27\lib\urllib2.py", line 448, in error 
    return self._call_chain(*args) 
    File "C:\Python27\lib\urllib2.py", line 382, in _call_chain 
    result = func(*args) 
    File "C:\Python27\lib\urllib2.py", line 531, in http_error_default 
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) 
urllib2.HTTPError: HTTP Error 501: Not Implemented 

更新: 后,我加入我得到这些错误的端口:

Traceback (most recent call last): 
    File "C:\Python27\Craig.py", line 10, in <module> 
    urllib2.urlopen('http://www.google.com') 
    File "C:\Python27\lib\urllib2.py", line 127, in urlopen 
    return _opener.open(url, data, timeout) 
    File "C:\Python27\lib\urllib2.py", line 404, in open 
    response = self._open(req, data) 
    File "C:\Python27\lib\urllib2.py", line 422, in _open 
    '_open', req) 
    File "C:\Python27\lib\urllib2.py", line 382, in _call_chain 
    result = func(*args) 
    File "C:\Python27\lib\urllib2.py", line 1214, in http_open 
    return self.do_open(httplib.HTTPConnection, req) 
    File "C:\Python27\lib\urllib2.py", line 1184, in do_open 
    raise URLError(err) 
urllib2.URLError: <urlopen error [Errno 10061] No connection could be made because the target machine actively refused it> 
+1

你的代理服务器不处理代理请求。 – 2014-08-30 15:50:11

+0

那么我能做些什么来解决这个问题? – winders 2014-08-30 15:50:32

+1

确保您实际上在localhost端口80上使用代理而不是其他内容?或者,如果您的代理正在另一个端口上运行,请使用该端口?这是**不**,但是,一个Python问题。 – 2014-08-30 15:54:39

回答

0

你运行的本地代理?

如果你是,它可能不是80端口上的默认端口上运行是3128

proxy = urllib2.ProxyHandler({ 
    'http': 'http://127.0.0.1:3128/', 
    'https': 'http://127.0.0.1:3128/' 
}) 
+0

好的,它可能已经工作了,但我得到更多的错误,因为你可以看到顶部。 – winders 2014-08-30 15:55:31

+0

您是否正在运行本地代理? – MattH 2014-08-30 15:56:57

+0

是的,我是。还有相同的错误:( – winders 2014-08-30 15:59:27