python---urllib2使用代理

# coding:utf-8
import urllib2
import sys

var_proxyswitch= True

var_httpproxy_handler = urllib2.ProxyHandler({"http" : "帐号:密码@101.251.198.244:8080"})
var_nohttpproxy_handler = urllib2.ProxyHandler({})

if var_proxyswitch:
    var_opener = urllib2.build_opener(var_httpproxy_handler)
else:
    var_opener = urllib2.build_opener(var_nohttpproxy_handler)

urllib2.install_opener(var_opener)

var_quest = urllib2.Request("http://www.baidu.com")
var_response = urllib2.urlopen(var_quest)

print(var_response.read().decode("utf8"))

你可能感兴趣的:(python)