in python ProxyHandler processor (Agent)

ProxyHandler processor (agent):

1. Acting principle: the purpose of the site prior to the request, the first request to the proxy server, and then let the proxy server to request that the target site, the proxy server to get data purpose of the site, and then send us the code.

2. http://httpbin.org: This site can easily view some of the parameters of the http request.

 3. Use code Agent: * Use "urllib.request.ProxyHandler", pass a proxy, the proxy is a dictionary whose key is "http" or "https", the dictionary key depends on the proxy server is able to receive the type, value is 'ip: port' * the use of step to create a 'handler', and 'request.bulid_opener' to create a 'opener' * the use of step to create the 'opener', calls 'open' function, initiated the request. Example code:

from urllib import request url='http://www.httpbin.org/ip'

# 1 using proxyHanler, incoming agent construct a handler handler = request.ProxyHandler ({ "http": '39.137.2.242:8080'}). # 2 using a construct created above handler opener opener = request.build_opener (handler ) # 3 to send a request using the opener resp = opener.open (url) print (resp.read ())

© copyright reserved by the authors

Guess you like

Origin www.cnblogs.com/zhongxiaoyan/p/11829371.html