URLLIB2 Custom Opener Detailed _python

Source: Internet
Author: User
Tags auth

The Urllib2.urlopen () function does not support authentication, cookies, or other HTTP advanced features. To support these features, you must use the Build_opener () function to create a custom opener object.

Copy Code code as follows:

Build_opener ([Handler1 [Handler2, ...]])

Parameter handler is a handler example, commonly used in Httpbasicauthhandler, Httpcookieprocessor, Proxyhandler and so on.

The object returned by Build_opener () has the open () method, which is the same as the function of the Urlopen () function.

If you want to modify the HTTP header, you can use:

Copy Code code as follows:

Import Urllib2
Opener = Urllib2.build_opener ()
Opener.addheaders = [(' User-agent ', ' mozilla/5.0 ')]
Opener.open (' http://www.example.com/')

2. Install_opener (opener)

Installs a different opener object as the global opener used by Urlopen ().

3. Password verification (Httpbasicauthhandler)

The Httpbasicauthhandler () handler can use Add_password () to set the password.

Copy Code code as follows:

H.add_password (REALM,URI,USER,PASSWD)

Realm is the name or descriptive information associated with validation, depending on the remote server. The URI is the base URL. User and passwd specify the username and password, respectively.

Copy Code code as follows:

Import Urllib2
Auth=urllib2. Httpbasicauthhandler ()
Auth.add_password (' Administrator ', ' http://www.example.com ', ' Dave ', ' 123456 ')
Opener=urllib2.build_opener (auth)
U=opener.open (' http://www.example.com/evilplan.html ')

4. Cookie processing (httpcookieprocessor)

Copy Code code as follows:

Import Urllib2,cookielib
Cookie=cookielib. Cookiejar ()
Cookiehand=urllib2. Httpcookieprocessor (Cookie)
Opener=urllib2.build_opener (Cookiehand)

5. Agent (Proxyhandler)

The Proxyhandler (proxies) parameter proxies is a dictionary that maps the protocol name (HTTP,FTP) to the URL of the appropriate proxy server.

Copy Code code as follows:

Proxy=proxyhandler ({' http ': ' http://someproxy.com:8080 '})
Auth=httpbasicauthhandler ()
Auth.add_password ()
Opener=build_opener (Auth,proxy)

You can also use proxies in Urlopen

Copy Code code as follows:

Import Urllib2
Proxy = ' http://%s:%s@%s '% (' userName ', ' Password ', ' proxy ')
Information = Urllib2.urlopen ("http://www.example.com", proxies={' http ':p Roxy})

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.