7-python Custom Opener

Source: Internet
Author: User

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:

    1. Use related Handler处理器 to create processor objects for specific functions;
    2. urllib2.build_opener()These processor objects are then used by methods to create custom opener objects;
    3. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.