Httpbasicauthhandler (Basic authentication)

Source: Internet
Author: User

The client then requests the domain again with the correct account and password contained in the header. This is "Basic Authentication". To simplify this process, we can create a
Examples of Httpbasicauthhandler and opener to use this handler.

Httpbasicauthhandler uses a map called password-managed objects to handle the domain of URLs and user names and passwords. If you know what the domain is (from the authentication header sent from the server), you can use a httppasswordmgr. Many

Number of cases people don't care what the domain is. It is convenient to use Httppasswordmgrwithdefaultrealm. It allows you to specify a specific user name and password for a URL. This will be provided to you when you do not provide an optional password lock for a particular domain.

We point this out by providing none as a parameter of the Add_password method domain.

The highest-level URL is the first URL that needs to be authentication. URLs that are deeper than the URLs you pass to. Add_password () will also match.

#  Create the password manager password_mgr = urllib2. Httppasswordmgrwithdefaultrealm () #  Add user name and password .#  if you know realm, use it instead of none.top_level_url =  "HTTP// www.163.com/"Password_mgr.add_password (None, top_level_url, username, password) handler =  urllib2. Httpbasicauthhandler (password_mgr) #创建openeropener  = urllib2.build_opener (handler) #  Open a Urlopener.open (a_url) #  install opener, which will be used later urllib2.urlopen. Urllib2.install_opener (opener) 

Sometimes we need python to parse some pages for automated monitoring. These pages typically require user input Username,password for basic validation, which requires us to use Python's Basic authentication capabilities. The Python Crawl dubbo-admin service page is now available for example:

  1. # Create a password manager

  2. Password_mgr = Urllib2. Httppasswordmgrwithdefaultrealm ()

  3. # ADD the username and password.

  4. Top_level_url = "Http://1000.906.400.1620:88889/dubbo-admin/governance/services"

  5. Username = "root"

  6. Password = "Testroot"

  7. Password_mgr.add_password (None, top_level_url, username, password)

  8. Handler = Urllib2. Httpbasicauthhandler (Password_mgr)

  9. # create "opener" (Openerdirector instance)

  10. Opener = Urllib2.build_opener (handler)

  11. # Use the opener to fetch a URL

  12. Opener.open (Top_level_url)

  13. # Install the opener

  14. Urllib2.install_opener (opener)

  15. # Now all calls to use Urllib2.urlopen with our opener.

  16. print urllib2.urlopen (top_level_url). Read ()

Ming: This authentication solves the need to enter the user name, password authentication, but there is no specific login Request page authentication method. (unlike Qzone, everyone has a POST request similar to login.do).

Httpbasicauthhandler (Basic authentication)

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.