在使用代理服务器的公司网络上,有没有办法使用ez_setup.py安装Python的easy_install?目前,我收到连接超时:
Downloading http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg
Traceback (most recent call last):
File "C:\jsears\python\ez_setup.py", line 278, in <module>
main(sys.argv[1:])
File "C:\jsears\python\ez_setup.py", line 210, in main
egg = download_setuptools(version, delay=0)
File "C:\jsears\python\ez_setup.py", line 158, in download_setuptools
src = urllib2.urlopen(url)
File "C:\jsears\Python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "C:\jsears\Python27\lib\urllib2.py", line 400, in open
response = self._open(req, data)
File "C:\jsears\Python27\lib\urllib2.py", line 418, in _open
'_open', req)
File "C:\jsears\Python27\lib\urllib2.py", line 378, in _call_chain
result = func(*args)
File "C:\jsears\Python27\lib\urllib2.py", line 1207, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "C:\jsears\Python27\lib\urllib2.py", line 1177, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>
答案 0 :(得分:15)
在Windows 7上,使用PowerShell,上面的代理设置将被忽略,该工具将无法使用。但我找到了解决方案。
我通过添加
修改了例程download_file_powershell[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials;
用于通过WebClient类下载的scriptlet中的。这是完整的download_file_powershell函数:
def download_file_powershell(url, target):
"""
Download the file at url to target using Powershell (which will validate
trust). Raise an exception if the command cannot complete.
"""
target = os.path.abspath(target)
cmd = [
'powershell',
'-Command',
"[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials; (new-object System.Net.WebClient).DownloadFile(%(url)r, %(target)r)" % vars(),
]
subprocess.check_call(cmd)
答案 1 :(得分:7)
如果您已经设置了http_proxy / https_proxy环境变量,则可以告诉ez_setup.py不要使用PowerShell。 PowerShell不使用HTTP_PROXY / HTTPS_PROXY环境变量。请按照此回复中的第一部分进行操作。
对于可能不知道如何设置环境变量的人,请参阅第2节。
部分停止使用PowerShell的ez_setup.py
进入ez_install.py并找到以下部分:
def has_powershell():
if platform.system() != 'Windows':
return False
cmd = ['powershell', '-Command', 'echo test']
devnull = open(os.path.devnull, 'wb')
try:
try:
subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
except:
return False
finally:
devnull.close()
return True
并将其更改为
def has_powershell():
return False
ez_install.py将使用您的环境HTTP_PROXY / HTTPS_PROXY,可以从命令行或通过控制面板进行设置。
临时命令行:
set HTTP_PROXY=http://proxy.example.com
set HTTPS_PROXY=https://proxy.example.com
注意:如果这样做,你必须在运行这些命令的同一命令窗口中运行'python ez_setup.py'。
永久命令行(仅限用户):
setx HTTP_PROXY "http://proxy.example.com"
setx HTTPS_PROXY "https://proxy.example.com"
永久命令行(Machine aka All Users):
setx HTTP_PROXY "http://proxy.example.com" /M
setx HTTPS_PROXY "https://proxy.example.com" /M
永久通过控制面板:
答案 2 :(得分:6)
显然,您只需设置一个环境变量:
导出http_proxy = http://&lt; user&gt;:&lt; password&gt; @&lt; proxy_host_name&gt;:&lt; port&gt;
例如:
export http_proxy = http:// admin:password@proxy.example.com:80
答案 3 :(得分:4)
您也可以在代码中设置:
import urllib2
proxy = urllib2.ProxyHandler({'http':'http://username:password@proxy_host:port'})
auth = urllib2.HTTPBasicAuthHandler()
opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler)
urllib2.install_opener(opener)
答案 4 :(得分:0)
我遇到了同样的问题,这是我找到的解决方案。我承认这并不理想,但这是我在Windows上找到这个问题的唯一方法。
print saveto
def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
to_dir=os.curdir, delay=15,
downloader_factory=get_best_downloader):
# making sure we use the absolute path
to_dir = os.path.abspath(to_dir)
tgz_name = "setuptools-%s.tar.gz" % version
url = download_base + tgz_name
saveto = os.path.join(to_dir, tgz_name)
print saveto
if not os.path.exists(saveto): # Avoid repeated downloads
log.warn("Downloading %s", url)
downloader = downloader_factory()
downloader(url, saveto)
return os.path.realpath(saveto)
在我的情况下执行脚本时,这提供了以下输出:“C:\ Python27&gt; python.exe ez_setup.py”
输出:
C:\ Python27 \ setuptools的-1.4.2.tar.gz 正在下载https://pypi.python.org/packages/source/s/setuptools/setuptools-1.4.2.tar.gz 使用“2”参数调用“DownloadFile”的异常:“远程服务器返回错误:(407)需要代理验证。” 在行:1字符:47 +(new-object System.Net.WebClient).DownloadFile&lt;&lt;&lt;&lt; ('https://pypi.python.org/packages/source/s/setuptools/setuptools-1.4.2.tar.gz','C:\ Python27 \ setuptools-1.4.2.tar.gz') + CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId:DotNetMethodException
此文件放在该位置会触发此逻辑语句:
if not os.path.exists(saveto): # Avoid repeated downloads
log.warn("Downloading %s", url)
downloader = downloader_factory()
downloader(url, saveto)
return os.path.realpath(saveto)
好像通过魔法,将安装包。