Https login with urllib2

I currently have a little script that loads a webpage and retrieves some data that interests me. Nothing unusual.

I am currently loading the page as follows:

import commands
command = 'wget --output-document=- --quiet --http-user=USER --http-password=PASSWORD https://www.example.ca/page.aspx'
status, text = commands.getstatusoutput(command)

Although this works just fine, I thought it made sense to remove the wget dependency. I thought it should be trivial to convert above to urllib2, but so far I have had zero success. The Internet are complete examples of urllib2, but I did not find anything that matched my need for simple authentication of an HTTP username and password with an HTTPS server.

+5
source share
3 answers

requests API HTTP/HTTPS.

import requests

url = 'https://www.someserver.com/toplevelurl/somepage.htm'

res = requests.get(url, auth=('USER', 'PASSWORD'))

status = res.status_code
text   = res.text
+1

,

[as], Python SSL.

HTTP, , .

:

import urllib2

theurl = 'http://www.someserver.com/toplevelurl/somepage.htm'
username = 'johnny'
password = 'XXXXXX'
# a great password

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
# this creates a password manager
passman.add_password(None, theurl, username, password)
# because we have put None at the start it will always
# use this username/password combination for  urls
# for which `theurl` is a super-url

authhandler = urllib2.HTTPBasicAuthHandler(passman)
# create the AuthHandler

opener = urllib2.build_opener(authhandler)

urllib2.install_opener(opener)
# All calls to urllib2.urlopen will now use our handler
# Make sure not to include the protocol in with the URL, or
# HTTPPasswordMgrWithDefaultRealm will be very confused.
# You must (of course) use it when fetching the page though.

pagehandle = urllib2.urlopen(theurl)
# authentication is now handled automatically for us

, , SSL. Google python + urllib2 + http + digest.

,

+6

All Articles