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:
# Create a password manager
Password_mgr = Urllib2. Httppasswordmgrwithdefaultrealm ()
# ADD the username and password.
Top_level_url = "Http://1000.906.400.1620:88889/dubbo-admin/governance/services"
Username = "root"
Password = "Testroot"
Password_mgr.add_password (None, top_level_url, username, password)
Handler = Urllib2. Httpbasicauthhandler (Password_mgr)
# create "opener" (Openerdirector instance)
Opener = Urllib2.build_opener (handler)
# Use the opener to fetch a URL
Opener.open (Top_level_url)
# Install the opener
Urllib2.install_opener (opener)
# Now all calls to use Urllib2.urlopen with our opener.
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)