python 2.7 w7
proxy = 'http://%s:%s@%s:%s' % (user, passwd, proxyserver,port)
proxy_support = urllib2.ProxyHandler({'http':proxy})
opener = urllib2.build_opener(proxy_support )
urllib2.install_opener( opener )
sContent = urllib2.urlopen(url)
這種方式可行,親測可用,直接將用戶名密碼放入urllib2.ProxyHandler中不可行。
def url_build_proxy_opener(proxy_info):
passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
passmgr.add_password(None, proxy_info['server'] , proxy_info['user'], proxy_info['password'])
auth = urllib2.ProxyBasicAuthHandler(passmgr)
opener = urllib2.build_opener(urllib2.ProxyHandler({'http':proxy_info['server']}) , auth)
return opener
proxy_info = {'user':'xxxx', 'password':'xxxx' , 'server':'openproxy.xxxx.com:8080'}
opener = url_build_proxy_opener(proxy_info)
urllib2.install_opener(opener)