urllib2使用代理服務器
本代碼演示了python的urllib2模塊如何使用代理,以及需要登錄驗證的proxy
# The proxy address and port: proxy_info = { 'host' : 'proxy.myisp.com', 'port' : 3128 }We create a handler for the proxy
proxy_support = urllib2.ProxyHandler({"http" : "http://%(host)s:%(port)d" % proxy_info})
We create an opener which uses this handler:
opener = urllib2.build_opener(proxy_support)
Then we install this opener as the default opener for urllib2:
urllib2.install_opener(opener)
Now we can send our HTTP request:
htmlpage = urllib2.urlopen("http://sebsauvage.net/").read(200000)
如果代理需要驗證
proxy_info = { 'host' : 'proxy.myisp.com', 'port' : 3128, 'user' : 'John Doe', 'pass' : 'mysecret007' } proxy_support = urllib2.ProxyHandler({"http" : "http://%(user)s:%(pass)s@%(host)s:%(port)d" % proxy_info}) opener = urllib2.build_opener(proxy_support) urllib2.install_opener(opener) htmlpage = urllib2.urlopen("http://sebsauvage.net/").read(200000)</pre>
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!