导入urllib2_file库后,我的代码不适用于代理处理

时间:2014-03-05 05:45:25

标签: python python-2.7 urllib2

没有导入urllib2_file我的代码工作正常。

import urllib2
import urllib
import random
import mimetypes
import string
import urllib2_file

proxy = urllib2.ProxyHandler({'http': '10.200.1.26'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
u = urllib2.urlopen("http://127.0.0.1:3333/command/core/create-importing-job",data=urllib.urlencode({"test":""}))
print u.read()

导入urllib2_file库之后抱怨:

Traceback (most recent call last):
  File "C:/hari/latest refine code/trialrefine.py", line 11, in <module>
    u = urllib2.urlopen("http://127.0.0.1:3333/command/core/create-importing-job",data=urllib.urlencode({"test":""}))
  File "C:\Python27\lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 391, in open
    response = self._open(req, data)
  File "C:\Python27\lib\urllib2.py", line 409, in _open
    '_open', req)
  File "C:\Python27\lib\urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "C:\Python27\urllib2_file.py", line 207, in http_open
    return self.do_open(httplib.HTTP, req)
  File "C:\Python27\urllib2_file.py", line 298, in do_open
    return self.parent.error('http', req, fp, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 435, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 518, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 404: Not Found

1 个答案:

答案 0 :(得分:0)

你得到404 error。这意味着网址错误/服务器已关闭。请注意urllib2_file会覆盖urllib2的默认HTTP处理程序:

urllib2._old_HTTPHandler = urllib2.HTTPHandler
urllib2.HTTPHandler = newHTTPHandler

你可以做的一件事就是明确地将urllib2._old_HTTPHandler传递给开场白。除此之外,您真的应该使用调试器进入urllib2_file以了解哪里出错了。