天天看點

10-穿牆代理的設定 | 01.資料抓取 | Python10-穿牆代理的設定

下面是普通http proxy的設定方式:

_proxy_connect = "http://127.0.0.1:1984"

c.setopt(pycurl.proxy, _proxy_connect)

req = urllib2.request(link) 

proxy = urllib2.proxyhandler({'http':'http://127.0.0.1:1984'}) 

opener = urllib2.build_opener(proxy,urllib2.httphandler) 

urllib2.install_opener(opener) 

req.add_header('user-agent', urllib2_user_agent) 

urllib2.urlopen(link)

… 

opener.close()

conn = httplib.httpconnection("127.0.0.1",1984) 

conn.request("get", "http://www.python.org/index.html") 

r = conn.getresponse() 

print r.status, r.reason

更底層的socket代理如下所示:

import base64

# proxy credentials

proxyauth = base64.encodestring('%s:%s' % (proxy_username,proxy_password))

proxy_authheader = "basic " + proxyauth.strip()

factory = proxyclientfactory('http://url_you_want')

factory.headers={'proxy-authenticate': proxy_authheader}

reactor.connecttcp('http://proxy_host', proxy_port, factory)