Advertisement
Guest User

Untitled

a guest
Oct 30th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. import urllib2
  2.  
  3. theurl = 'http://www.someserver.com/toplevelurl/somepage.htm'
  4. username = 'johnny'
  5. password = 'XXXXXX'
  6. # a great password
  7.  
  8. passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
  9. # this creates a password manager
  10. passman.add_password(None, theurl, username, password)
  11. # because we have put None at the start it will always
  12. # use this username/password combination for urls
  13. # for which `theurl` is a super-url
  14.  
  15. authhandler = urllib2.HTTPBasicAuthHandler(passman)
  16. # create the AuthHandler
  17.  
  18. opener = urllib2.build_opener(authhandler)
  19.  
  20. urllib2.install_opener(opener)
  21. # All calls to urllib2.urlopen will now use our handler
  22. # Make sure not to include the protocol in with the URL, or
  23. # HTTPPasswordMgrWithDefaultRealm will be very confused.
  24. # You must (of course) use it when fetching the page though.
  25.  
  26. pagehandle = urllib2.urlopen(theurl)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement