2009-09-21 115 views
3

從Python開始,我希望通過基本身份驗證通過HTTPS從網站檢索內容。我需要磁盤上的內容。我在內部網上,信任HTTPS服務器。平臺是Windows上的Python 2.6.2。通過代理進行基本身份驗證的Python HTTPS客戶端

我一直在玩urllib2,但到目前爲止還沒有成功。

我有一個解決方案運行,要求通過使用os.system的wget():

wget_cmd = r'\path\to\wget.exe -q -e "https_proxy = http://fqdn.to.proxy:port" --no-check-certificate --http-user="username" --http-password="password" -O path\to\output https://fqdn.to.site/content' 

我想擺脫使用os.system的()。在Python中可能嗎?

回答

3

試試這個(請注意,你必須填寫你的服務器的境界也):

import urllib2 
authinfo = urllib2.HTTPBasicAuthHandler() 
authinfo.add_password(realm='Fill In Realm Here', 
         uri='https://fqdn.to.site/content', 
         user='username', 
         passwd='password') 
proxy_support = urllib2.ProxyHandler({"https" : "http://fqdn.to.proxy:port"}) 
opener = urllib2.build_opener(proxy_support, authinfo) 
fp = opener.open("https://fqdn.to.site/content") 
open(r"path\to\output", "wb").write(fp.read()) 
相關問題