Proxy

Pip + virtualenv + Redhat SCL + proxy = 不工作

  • December 6, 2016

我在 Redhat Enterprise 6 中設置了一個基於 Python 2.7 的 virtualenv(預設情況下附帶 2.6.x)。

我安裝了 Python 的 SCL 2.7 版本,然後創建了 virtualenv:

$ scl enable python27 bash
$ cd /my/project
$ virtualenv -v --clear --extra-search-dir=/opt/rh/python27/root/usr/bin/ --python=/opt/rh/python27/root/usr/bin/python virtualenv
$ source virtualenv/bin/acticvate

到現在為止還挺好。當我嘗試在我機構的代理後面執行 pip 時,問題就開始了:

$ pip install Werkzeug --proxy proxy.example.org:3128                       
Downloading/unpacking Werkzeug                                                 
Cleaning up...                                                                 
Exception:                                                                     
Traceback (most recent call last):                                             
 File "/my/project/virtualenv/lib/python2.7/site-packages/pip/basecommand.py", line 134, in main
   status = self.run(options, args)                                           
 File "/my/project/virtualenv/lib/python2.7/site-packages/pip/commands/install.py", line 236, in run
   requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
 File "/my/project/virtualenv/lib/python2.7/site-packages/pip/req.py", line 1085, in prepare_files
   url = finder.find_requirement(req_to_install, upgrade=self.upgrade)        
 File "/my/project/virtualenv/lib/python2.7/site-packages/pip/index.py", line 201, in find_requirement
   page = self._get_page(main_index_url, req)                                 
 File "/my/project/virtualenv/lib/python2.7/site-packages/pip/index.py", line 554, in _get_page
   return HTMLPage.get_page(link, req, cache=self.cache)                      
 File "/my/project/virtualenv/lib/python2.7/site-packages/pip/index.py", line 671, in get_page
   resp = urlopen(url)                                                        
 File "/my/project/virtualenv/lib/python2.7/site-packages/pip/download.py", line 176, in __call__
   response = self.get_opener(scheme=scheme).open(url)                        
 File "/opt/rh/python27/root/usr/lib64/python2.7/urllib2.py", line 404, in open
   response = self._open(req, data)                                           
 File "/opt/rh/python27/root/usr/lib64/python2.7/urllib2.py", line 422, in _open
   '_open', req)                                                              
 File "/opt/rh/python27/root/usr/lib64/python2.7/urllib2.py", line 382, in _call_chain
   result = func(*args)                                                       
 File "/my/project/virtualenv/lib/python2.7/site-packages/pip/download.py", line 155, in https_open
   return self.do_open(self.specialized_conn_class, req)                      
 File "/opt/rh/python27/root/usr/lib64/python2.7/urllib2.py", line 1183, in do_open
   h.request(req.get_method(), req.get_selector(), req.data, headers)         
 File "/opt/rh/python27/root/usr/lib64/python2.7/httplib.py", line 1001, in request
   self._send_request(method, url, body, headers)                             
 File "/opt/rh/python27/root/usr/lib64/python2.7/httplib.py", line 1035, in _send_request
   self.endheaders(body)                                                      
 File "/opt/rh/python27/root/usr/lib64/python2.7/httplib.py", line 997, in endheaders
   self._send_output(message_body)                                            
 File "/opt/rh/python27/root/usr/lib64/python2.7/httplib.py", line 850, in _send_output
   self.send(msg)                                                             
 File "/opt/rh/python27/root/usr/lib64/python2.7/httplib.py", line 812, in send
   self.connect()                                                             
 File "/my/project/virtualenv/lib/python2.7/site-packages/pip/download.py", line 139, in connect
   match_hostname(self.sock.getpeercert(), self.host)                         
 File "/my/project/virtualenv/lib/python2.7/site-packages/pip/backwardcompat/ssl_match_hostname.py", line 61, in match_hostname
   % (hostname, ', '.join(map(repr, dnsnames))))                              
CertificateError: hostname 'proxy.example.org' doesn't match either of 'www.python.org', 'python.org', 'pypi.python.org', 'docs.python.org', 'testpypi.python.org', 'bug>>>s.python.org', 'wiki.python.org', 'hg.python.org', 'mail.python.org', 'packaging.python.org', 'pythonhosted.org', 'www.pythonhosted.org', 'test.pythonhosted.org', 'us.py>>>con.org', 'id.python.org', 'pypi.io'

我在其他問題中看到了這個問題,但這些似乎都不適用於我的情況。

編輯:

例如,我嘗試在https://stackoverflow.com/questions/21468550/pip-not-working-behind-firewall中設置環境變數。其他案例正在解決 Windows 問題。

值得一提的是,如果我不在 scl 環境中,這個 virtualenv 將不起作用,例如,如果我呼叫python啟用 virtualenv 而不是 scl,它會抱怨找不到 libpython2.7.so.1.0。我不知道這是否是 bny 設計,但我希望 Python 2.7 依賴的所有庫都在 virtualenv 中進行符號連結或複制。也許我在 virtualenv 安裝中做錯了什麼?

您現在可能已經解決了它,但似乎您需要使用受信任的主機選項:

pip install Werkzeug --proxy proxy.example.org:3128 --trusted-host proxy.example.org

引用自:https://serverfault.com/questions/782726