2014-02-14 58 views
0

背景:由於環境的限制,我堅持使用python 2.4。所以requests是不可能的。urllib2:如何安裝代理和http基本身份驗證處理程序作爲openers?

我希望能夠同時使用urllib2.HTTPBasicAuthHandlerProxyHandler打開一個url。

如果我做這樣的事情:

proxy = urllib2.ProxyHandler({'http': 'http://myproxy.local'}) 
proxy_opener = urllib2.build_opener(proxy) 

... 
passman = urllib2.HTTPPasswordMgrWithDefaultRealm() 
pass_handler = urllib2.HTTPBasicAuthHandler(passman) 
... 
urllib2.install_opener(urllib2.build_opener([proxy_opener, pass_handler])) 

的代碼將停留在這一行:

urllib2.urlopen(target_url) 

那麼,什麼是安裝兩個處理器的正確方法?

編輯:

我原來的版本有語法錯誤。該生產線

urllib2.install_opener(urllib2.build_opener(pass_handler), proxy_opener) 

應該

urllib2.install_opener(urllib2.build_opener(pass_handler, proxy_opener)) # note the parenthesis 

但作爲atupal表明,它應該是

urllib2.install_opener(urllib2.build_opener([proxy_opener, pass_handler])) 

回答

2

閱讀docs-install_openerdocs-build_opener

urllib2.install_opener(opener)

Install an OpenerDirector instance as the default global opener.

urllib2.build_opener([handler, ...])

Return an OpenerDirector instance, which chains the handlers in the order given. handlers can be either instances of BaseHandler, or subclasses of BaseHandler (in which case it must be possible to call the constructor without any parameters). Instances of the following classes will be in front of the handlers, unless the handlers contain them, instances of them or subclasses of them: ProxyHandler (if proxy settings are detected), UnknownHandler, HTTPHandler, HTTPDefaultErrorHandler, HTTPRedirectHandler, FTPHandler, FileHandler, HTTPErrorProcessor.

所以,你應該先建立一個開門紅使用代理處理程序,並權威性的處理程序。並安裝它globaly如果你想:

proxy_handler = urllib2.ProxyHandler({'http': 'http://myproxy.local'}) 

... 
passman = urllib2.HTTPPasswordMgrWithDefaultRealm() 
pass_handler = urllib2.HTTPBasicAuthHandler(passman) 
... 
urllib2.install_opener(urllib2.build_opener(proxy_handler, pass_handler)) 

更新: 我測試下面的代碼片段,它可以作爲預期。不要忘記用自己更換代理,URL,用戶名和密碼:

import urllib2 

proxyhandler = urllib2.ProxyHandler({'http': 'http://219.93.183.106:8080'}) 

url = "http://atupal.org:9001" 
passman = urllib2.HTTPPasswordMgrWithDefaultRealm() 
passman.add_password(None, url, "myusername", "mypassword") 
pass_handler = urllib2.HTTPBasicAuthHandler(passman) 

opener = urllib2.build_opener(
    proxyhandler, 
    pass_handler, 
    ) 
urllib2.install_opener(opener) 

print urllib2.urlopen(url).read() 
+0

感謝您的回答,但你的代碼將導致此異常: AttributeError的:OpenerDirector實例沒有屬性「add_parent」。如果我切換到urllib2.build_opener([proxy_opener,pass_handler]),異常消失,但代碼卡在urllib2.urlopen。我會更新我的問題以反映狀態。 –

+0

@AnthonyKong哦,我解決了錯誤。不需要列表,我們應該使用proxy_handler而不是proxy_opener。我添加一個例子,:),它適用於我。 – atupal