Handler processors and custom opener
Opener is urllib2. Openerdirector example, we have been using the Urlopen, it is a special opener (that is, the module to help us build).
However, the basic Urlopen () method does not support other HTTP/HTTPS advanced features such as proxies, cookies, and so on. So to support these features:
- Use related
Handler处理器
to create processor objects for specific functions;
urllib2.build_opener()
These processor objects are then used by methods to create custom opener objects;
- Invokes a method to send a request using a custom opener object
open()
.
If all the requests in the program use custom opener, you can use the custom urllib2.install_opener()
opener object as the global opener, which means that if you call Urlopen later, you will use this opener (choose according to your own needs)
- Py3 is directly with Urllib.request.HTTPHandler
#_ *_ coding:utf-8 _*_ "Created on July 13, 2018 @author:sss function: Test custom http_handler ' import urllib.request# Build a HttpHandler Processor object that supports processing HTTP requests # Http_handler = Urllib.request.HTTPHandler () Http_handler = Urllib.request.HTTPHandler (debuglevel = 1) #打开调试 # Build a HttpHandler processor object that supports processing HTTPS requests # Http_handler = Urllib.request.HTTPHandler () #调用创建支持处理http请求的opener对象opener = Urllib.request.build_opener (http_handler) # Build the request requests with the urllib.request.Request (' http://www.baidu.com/') #调用自定义opener对象的open () method to send request requests response = Opener.open (Request) #获取服务器相应内容: print (Response.read ())
7-python Custom Opener