为什么我会收到这些错误以及如何修复它们?

时间:2014-08-30 15:48:11

标签: python proxy handler

当我运行此脚本时,我遇到了大量错误。     import 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 个答案:

答案 0 :(得分:0)

您是否正在运行本地代理?

如果你是,它可能没有在端口80上运行。默认端口是3128。

proxy = urllib2.ProxyHandler({
    'http': 'http://127.0.0.1:3128/',
    'https': 'http://127.0.0.1:3128/'
})
相关问题