使用urllib.request.urlopen()时出现“HTTP错误409:冲突”

时间:2010-02-11 20:16:12

标签: python http proxy python-3.x

在Python 3.1下,尝试运行此代码时:

from urllib import request

def test():
    request.urlopen("http://www.google.com")

test()

我收到HTTP 409错误。堆栈跟踪是:

Traceback (most recent call last):
  File "C:\Users\Beau\Python\pokescrape.py", line 6, in <module>
    test()
  File "C:\Users\Beau\Python\pokescrape.py", line 4, in test
    request.urlopen("http://www.google.com")
  File "C:\Program Files\Python\lib\urllib\request.py", line 119, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Program Files\Python\lib\urllib\request.py", line 353, in open
    response = meth(req, response)
  File "C:\Program Files\Python\lib\urllib\request.py", line 465, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Program Files\Python\lib\urllib\request.py", line 385, in error
    result = self._call_chain(*args)
  File "C:\Program Files\Python\lib\urllib\request.py", line 325, in _call_chain
    result = func(*args)
  File "C:\Program Files\Python\lib\urllib\request.py", line 560, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "C:\Program Files\Python\lib\urllib\request.py", line 353, in open
    response = meth(req, response)
  File "C:\Program Files\Python\lib\urllib\request.py", line 465, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Program Files\Python\lib\urllib\request.py", line 391, in error
    return self._call_chain(*args)
  File "C:\Program Files\Python\lib\urllib\request.py", line 325, in _call_chain
    result = func(*args)
  File "C:\Program Files\Python\lib\urllib\request.py", line 473, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)

打印出来的实际错误是:

urllib.error.HTTPError: HTTP Error 409: Conflict

我通过配置脚本支持代理集;我没有关于互联网连接的问题。

为什么我收到HTTP 409错误?

3 个答案:

答案 0 :(得分:1)

您看到的HTTP错误是远程端(或代理)提供给您的错误。 HTTP错误409确实是“冲突”,这通常意味着发生了冲突的请求。如果您确实在使用代理,我会怀疑它是409的来源,但更多的调试将是有序的。使用wireshark之类的工具分析实际流量,或直接使用http.client.HTTPConnection并启用其调试。

答案 1 :(得分:1)

我也遇到了这个问题(同样来自Lancaster),并且发现如果我设置环境变量http_proxy,Python会使用它。在这种情况下(在Windows上)它将是:

set http_proxy=http://wwwcache.lancs.ac.uk:8080

和* nix:

export http_proxy=http://wwwcache.lancs.ac.uk:8080/

编辑:感谢BeauMartínez的* nix修复。

答案 2 :(得分:0)

事实证明我必须在代码中手动设置代理。我假设这是因为我使用的是自动代理脚本。

对于任何有类似问题的人,这是我使用的代码:

from urllib import request
import random

PROXY_URL = "http://wwwcache-{}.lancs.ac.uk:8080/"

def setLancsProxy():
    httpProxy = PROXY_URL.format(random.randrange(4))
    proxy = request.ProxyHandler({"http" : httpProxy})
    opener = request.build_opener(proxy)
    request.install_opener(opener)

我发现http://groups.google.com/group/comp.lang.python/browse_thread/thread/a9db4a2f398ee3a4http://www.wkoorts.com/wkblog/2008/10/27/python-proxy-client-connections-requiring-authentication-using-urllib2-proxyhandler/对处理这个模糊的问题最有帮助。