Urllib2在Windows下的pip错误

时间:2014-08-12 17:43:11

标签: python windows pip urllib2

我在Windows下运行pip格式的ActiveState Python 2.7.2时遇到了一些问题。我们使用代理,可能 成为问题的一部分。代理是非身份验证代理。来自系统的代理设置, 例如,手动Firefox或一些简单的Python代码可以正常工作:

这可以按预期工作:

urllib.urlopen('http://www.google.com', proxies={'http': 'http://proxy:port'})

它提供了来自google.com的标题信息的回复:

<addinfourl at 61539976L whose fp = <socket._fileobject object at 0x00000000042924F8>>

在我运行的http_proxy环境变量中设置代理

pip install loremipsum

我得到了

    Downloading/unpacking loremipsum
      Could not fetch URL http://pypi.python.org/simple/loremipsum: <urlopen error [Errno 11004] getaddrinfo
failed>
      Will skip URL http://pypi.python.org/simple/loremipsum when looking for download links for loremipsum
      Could not fetch URL http://pypi.python.org/simple/: <urlopen error [Errno 11004] getaddrinfo failed>
      Will skip URL http://pypi.python.org/simple/ when looking for download links for loremipsum
      Cannot fetch index base URL http://pypi.python.org/simple/
      Could not fetch URL http://pypi.python.org/simple/loremipsum/: <urlopen error [Errno 11004] getaddrinfo failed>
      Will skip URL http://pypi.python.org/simple/loremipsum/ when looking for download links for loremipsum
      Could not find any downloads that satisfy the requirement loremipsum
    No distributions at all found for loremipsum
    Exception information:
    Traceback (most recent call last):
      File "C:\ActiveState\ActivePython\lib\site-packages\pip\basecommand.py", line 126,  in main
    self.run(options, args)
  File "C:\ActiveState\ActivePython\lib\site-packages\pip\commands\install.py", line 222, in run
    requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
  File "C:\ActiveState\ActivePython\lib\site-packages\pip\req.py", line 954, in prepare_files
    url = finder.find_requirement(req_to_install, upgrade=self.upgrade)
  File "C:\ActiveState\ActivePython\lib\site-packages\pip\index.py", line 152, in find_requirement
    raise DistributionNotFound('No distributions at all found for %s' % req)
DistributionNotFound: No distributions at all found for loremipsum

错误11004似乎表明名称解析问题,在代理后面应该是一个问题 代理访问(或无知)。

我可以在Linux下测试类似的设置(http_proxy变量,相同的代理,不同的Python)。运行上面的命令很有效。 另外,在Windows机器上的浏览器中访问URL也可以工作(显示一组egg和zip文件)。

我走了pip代码,找到错过的地方,在交互式会话中运行代码。我找到 在C:\ActiveState\ActivePython\lib\site-packages\pip\downloads.py基本上所有下载准备 并且行动发生了。在setup()(第125行)中,ProxyHandler已准备就绪,opener已构建,即urllib2 存储在https中以供进一步调用使用。以交互方式运行时,我发现添加了一个条目 需要一个In [1]: import urllib2 In [2]: proxy='proxy:port' In [3]: proxy_support = urllib2.ProxyHandler({"http": proxy, "ftp": proxy, "https": proxy}) In [4]: opener = urllib2.build_opener(proxy_support, urllib2.CacheFTPHandler, urllib2.HTTPHandler(debugle vel=1), urllib2.HTTPSHandler(debuglevel=1)) In [5]: urllib2.install_opener(opener) 代理。我还添加了调试信息的打印。这给了我一个互动 在iPython中运行:

ftp

httphttpsdownloads.py的代理确实相同。我打算查看所有打印输出 代码处理命令行参数等没有乱用代理。代理以相同的方式存储 __call__()如上所述(从http_proxy变量中读取)。

确定从pip获取包的URL在第74行转到In [6]: url = urllib2.Request('http://pypi.python.org/simple/loremipsum', headers={'Accept-encoding': 'identity'})

首先使用以下命令构建请求:

urllib2.urlopen(url)

然后该请求与In [7]: response = urllib2.urlopen(url) send: 'GET http://pypi.python.org/simple/loremipsum HTTP/1.1\r\nHost: pypi.python.org\r\nUser-Agent: Pyth on-urllib/2.7\r\nConnection: close\r\nAccept-Encoding: identity\r\n\r\n' reply: 'HTTP/1.1 301 Moved Permanently\r\n' header: Server: Varnish header: Retry-After: 0 header: Location: https://pypi.python.org/simple/loremipsum header: Content-Length: 0 header: Accept-Ranges: bytes header: Date: Tue, 12 Aug 2014 16:41:39 GMT header: Via: 1.1 varnish header: X-Served-By: cache-fra1234-FRA header: X-Cache: MISS header: X-Cache-Hits: 0 header: X-Timer: S1407861699.491394,VS0,VE0 header: Connection: close header: Age: 0 send: 'CONNECT pypi.python.org:443 HTTP/1.0\r\n' send: '\r\n' send: 'GET /simple/loremipsum HTTP/1.1\r\nHost: pypi.python.org\r\nUser-Agent: Python-urllib/2.7\r\nConne ction: close\r\nAccept-Encoding: identity\r\n\r\n' reply: 'HTTP/1.1 301 Moved Permanently\r\n' header: Date: Tue, 12 Aug 2014 16:41:40 GMT header: Server: nginx/1.6.0 header: Location: /simple/loremipsum/ header: Cache-Control: max-age=600, public header: Strict-Transport-Security: max-age=31536000; includeSubDomains header: Via: 1.1 varnish header: Content-Length: 0 header: Accept-Ranges: bytes header: Via: 1.1 varnish header: Age: 44282 header: X-Served-By: cache-iad2135-IAD, cache-fra1231-FRA header: X-Cache: MISS, HIT header: X-Cache-Hits: 0, 1 header: X-Timer: S1407861700.831757,VS0,VE0 header: Connection: close send: 'CONNECT pypi.python.org:443 HTTP/1.0\r\n' send: '\r\n' send: 'GET /simple/loremipsum/ HTTP/1.1\r\nHost: pypi.python.org\r\nUser-Agent: Python-urllib/2.7\r\nConn ection: close\r\nAccept-Encoding: identity\r\n\r\n' reply: 'HTTP/1.1 200 OK\r\n' header: Date: Tue, 12 Aug 2014 16:41:41 GMT header: Server: nginx/1.6.0 header: Content-Type: text/html; charset=utf-8 header: X-PYPI-LAST-SERIAL: 794358 header: Cache-Control: max-age=600, public header: Strict-Transport-Security: max-age=31536000; includeSubDomains header: Via: 1.1 varnish header: Content-Length: 913 header: Accept-Ranges: bytes header: Via: 1.1 varnish header: Age: 67708 header: X-Served-By: cache-iad2121-IAD, cache-fra1231-FRA header: X-Cache: HIT, HIT header: X-Cache-Hits: 1, 1 header: X-Timer: S1407861701.174694,VS0,VE0 header: Vary: Accept-Encoding header: Connection: close

一起使用
{{1}}

这似乎是一个好的答案。我在pip中有相同的代码,但它失败了。

我错过了什么?为什么我的互动会话工作和点不是?

1 个答案:

答案 0 :(得分:0)

你没有specify the proxy来点?

编辑:评论摘要:ENV HTTP_PROXY指定了代理,但没有成功 起初,现在有效。

我看了一下urllib,并且有代码来处理Windows注册表设置 默认。有getproxies(),返回getproxies_environment() or getproxies_registry(),因此您已经确定无需任何修改 EMV 命令行。