2012-08-27 4 views
3

easy_install을 사용하는 몇 개의 파이썬 패키지를 설치하고 싶습니다. 그들은 설치 스크립트에서 urrlib2 모듈을 사용합니다. 필자는 회사 프록시를 사용하여 easy_install이 필수 패키지를 다운로드하도록 시도했다. 그래서 프록시 conn을 테스트하기 위해 다음 코드를 시도했다. IE에서 프록시에 대한 자격 증명을 제공 할 필요가 없습니다. 내 코드에 문제가프록시를 통해 파이썬 모듈 설치하기

proxy = urllib2.ProxyHandler({"http":"http://mycompanyproxy-as-in-IE:8080"}) 
opener = urllib2.build_opener(proxy) 
urllib2.install_opener(opener) 
site = urllib2.urlopen("http://google.com") 

Error: 
Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
File "C:\Python27\lib\urllib2.py", line 126, in 
return _opener.open(url, data, timeout) 
File "C:\Python27\lib\urllib2.py", line 406, in 
    response = meth(req, response) 
File "C:\Python27\lib\urllib2.py", line 519, in 
    'http', request, response, code, msg, hdrs) 
File "C:\Python27\lib\urllib2.py", line 444, in 
return self._call_chain(*args) 
File "C:\Python27\lib\urllib2.py", line 378, in 
    result = func(*args) 
File "C:\Python27\lib\urllib2.py", line 527, in 
    raise HTTPError(req.get_full_url(), code, msg 
    urllib2.HTTPError: HTTP Error 407: AuthorizedOnly 

인가? 또는 파이썬 프로세스에서 연결을 허용하지 않는 프록시입니까? 프록시를 설정하여 R 패키지를 설치할 수 있습니다.

+0

당신이 당신의 회사 프록시 자격 증명을 필요로하지 않는다는 것을 확신합니까? 어쩌면 IE는 오래전에 당신이나 당신 회사의 IT 직원이 입력 한 신임 정보를 기억했을까요? – Lior

+0

내 PC 또는 인트라넷에 대한 로그인 자격 증명과 동일한 것인지 확실하지 않을 수 있습니다. R에서 패키지를 업데이트하는 경우 --internet2 옵션을 사용합니다. – dasman

+0

컴퓨터가 도메인의 구성원이고 프록시가 Windows 서버 인 경우 프록시에서 거의 확실하게 도메인 로그인을 원합니다. 더 나쁜 것은 NTLM에서 원할 수도 있습니다. – kindall

답변

12

설정 다음과 같은 환경 변수 :

HTTP_PROXY=http://user:[email protected]:8080 

뿐만 아니라

HTTPS_PROXY=http://user:[email protected]:8080 

프록시 포트가 8080이 아닌 경우, 당신은 너무 적절한 포트 번호 8080을 변경해야합니다.
전역 시스템 변수를 수정할 권한이없는 경우 (로컬 관리자 권한이있는 경우에만 수행 할 수 있음) 사용자 수준 변수에 추가하십시오.

설정을 My Computer > Properties > Advanced > Environment Variables에서 당신이 변수 세트를 모두 닫습니다 cmd 창을 다시 명령 프롬프트를 실행하는 것이 일단

(또는 "고급"Windows 7의 경우). 그런 다음 일반 setuptools easy_installpip을 사용하여 Python 패키지를 다운로드하고 설치할 수 있습니다.

파이썬을 통해 사용해야하는 경우; requests 라이브러리는 httpliburllib의 단점을 처리합니다.

requests은 자동으로 HTTP_PROXY을 읽고 프록시를 사용합니다. 그러나 여기에서 수동으로합니다 (docs에서 예를) 어떻게 할 것입니다 : setx HTTP_PROXY와 https_proxy를이 일을 사용하여

import requests 

proxies = { 
    "http": "http://user:[email protected]:8080", 
    "https": "http://user:[email protected]:8080", 
} 

requests.get("http://example.org", proxies=proxies) 
+0

음, 수동 옵션이 ​​작동했습니다 ... – dasman

-1

오류는 권한 부여도 필요하다고 말합니다. 다음 코드를보십시오 :

proxy = urllib2.ProxyHandler({"http":"http://mycompanyproxy-as-in-IE:8080"}) 
proxy_auth_handler = urllib2.HTTPBasicAuthHandler() 
proxy_auth_handler.add_password('realm', 'host', 'username', 'password') 
opener = build_opener(proxy, proxy_auth_handler) 
urllib2.install_opener(opener) 
site = urllib2.urlopen("http://google.com") 

나는이 일을한다고 생각합니다.

+0

'realm'은 내가 로그인 한 도메인입니까? '호스트'란 무엇입니까? – dasman

+0

- add_password (realm, uri, user, passwd) - 'realm'은 정보 일뿐입니다. 예 : "XYZ Application", URI는 단일 URI 또는 ​​URI 시퀀스 일 수 있습니다. realm, user 및 passwd는 문자열이어야합니다. 이렇게하면 realm에 대한 인증과 주어진 URI 중 하나의 super-URI가 주어질 때 (user, passwd)가 인증 토큰으로 사용됩니다. – deep

관련 문제