12

プロキシサーバーを使用する企業ネットワークで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>
4

5 に答える 5

15

Windows 7では、PowerShellを使用すると、上記のプロキシ設定は無視され、ツールは機能しません。しかし、私は解決策を見つけました。

ルーチンdownload_file_powershellを追加して変更しました

[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials;

WebClientクラスを介してダウンロードするために使用されるスクリプトレット内。これが完全な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)
于 2013-09-13T15:29:42.260 に答える
7

すでにhttp_proxy/https_proxy環境変数が設定されている場合は、PowerShellを使用しないようにez_setup.pyに指示するだけです。PowerShellはHTTP_PROXY/HTTPS_PROXY環境変数を使用しません。この応答の最初のセクションに従ってください。

環境変数の設定方法がわからない場合は、セクション2+を参照してください。

ez_setup.pyによるPowerShellの使用を停止します

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

注:これを行う場合は、これらのコマンドを実行したのと同じコマンドウィンドウで「pythonez_setup.py」を実行する必要があります。

永続的なコマンドライン(ユーザーのみ):

setx HTTP_PROXY "http://proxy.example.com"
setx HTTPS_PROXY "https://proxy.example.com"

永続的なコマンドライン(マシン、別名すべてのユーザー):

setx HTTP_PROXY "http://proxy.example.com" /M
setx HTTPS_PROXY "https://proxy.example.com" /M

コントロールパネル経由で永続的:

  1. [スタート]->[コントロールパネル]->[ユーザーアカウント]
  2. 左側のパネルで、[環境変数を変更する]をクリックします
  3. 「ユーザー変数」または「システム変数」の「新規..」をクリックします(必要に応じて)
  4. 変数名:HTTP_PROXYおよび変数値:http:/proxy.example.comを設定します
  5. 「ユーザー変数」または「システム変数」の「新規..」をクリックします(必要に応じて)
  6. 変数名:HTTPS_PROXYおよび変数値:https:/proxy.example.comを設定します
  7. [OK]をクリックします
于 2013-11-20T15:37:13.263 に答える
6

どうやら、あなたは単に環境変数を設定することができます:

export http_proxy = http:// <user>:<password> @ <proxy_host_name>:<port>

例えば:

エクスポートhttp_proxy=http:// admin:password@proxy.example.com:80

于 2012-12-05T22:08:37.490 に答える
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)
于 2012-12-06T04:41:45.883 に答える
0

私はちょうど同じ問題に遭遇しました、そしてこれは私が見つけた解決策です。私はそれが理想的ではないことを認めますが、それは私がWindowsでこの問題を回避するために見つけた唯一の方法です。

  1. ダウンロード。ez_setup.py。
  2. 次の行を編集して、ファイルが圧縮されたパッケージをダウンロードする場所を示します。

保存先を印刷

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> 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「DownloadFile 」を「2」で呼び出すと例外が発生する"argument(s):"リモートサーバーがエラーを返しました:(407)ProxyAuthenticationRequired。 " 行:1 char:47 +(new-object System.Net.WebClient).DownloadFile <<<<(' 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

  1. 上記のhttpsリンクからパックをダウンロードし、私の場合は「C:\ Python27\"」と予想される場所に配置します。

このファイルがその場所に配置されると、次の論理ステートメントがトリガーされます。

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)

まるで魔法のように、パッケージがインストールされます。

于 2013-12-05T18:11:45.750 に答える