Python urllib2 Module Learning notes

Source: Internet
Author: User

2015.3.6
 The use of URLLIB2 is roughly as follows # Custom handler processing function opener = Urllib2.build_opener (Proxyhandler, HttpHandler) Urllib2.install_opener ( Opener) # custom URL parameter request = Urllib2. Request () request.add_headers (XXX) # Open URL, return file-like object response = Urllib2.urlopen (req) # Get results Print response.read () Analysis of key processing mechanisms  

Urlopen ()

Is the entry function for processing.
Gets the Openerdirector object, calling Opener.open ().
The default Openerdirector object is saved in the variable _open.
The singleton mode is used.
Build_opener ()is an object factory function that creates a Openerdirector object. The default handler list is provided internally, and the function passes in the handler parameter instead of the default handlers. Call the Openerdirector.add_handler () function to register the handlers. Register the default handler first, then register the custom handler. Register according to the order in the parameters.
Install_opener ()Saves the Openerdirector object to the variable _opener, which is used as the default opener. class OpenerdirectorThe composite mode is used. Add_handler (): Registers the handler object into the internal list. Open (): Invokes the Open method of each object in the handler list, triggering the processing operation. class RequestAs an information object, save and URL related parameters, including headers, data, proxy and so on. Used for the delivery of URL parameters.
class HttpHandlerClass inheritance relationship: Basehandler, AbstracthttphandlerHttpHandlerCall Httplib. Httpconnection completes the HTTP processing.

Python urllib2 Module Learning notes

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.