I am new to dealing with this so I am sorry if these are dumb questions.
How do you know if python was successful when logging into a website.
for now I am using stock code in their guides, so for example.
say i run this with my gmail information, and nothing happens, does that mean it succeeded. Or does the page need a pop up authentication?Code:import urllib2
import sys
import re
import base64
from urlparse import urlparse
theurl = 'http://www.someserver.com/somepath/somepage.html'
# if you want to run this example you'll need to supply
# a protected page with your username and password
username = 'johnny'
password = 'XXXXXX'
req = urllib2.Request(theurl)
try:
handle = urllib2.urlopen(req)
except IOError, e:
pass
else:
if not hasattr(e, 'code') or e.code != 401:
authline = e.headers['www-authenticate']
authobj = re.compile(
r'''(?:\s*www-authenticate\s*:)?\s*(\w*)\s+realm=['"]([^'"]+)['"]''',
re.IGNORECASE)
if not matchobj:
print 'The authentication header is badly formed.'
print authline
sys.exit(1)
scheme = matchobj.group(1)
realm = matchobj.group(2)
if scheme.lower() != 'basic':
print 'This example only works with BASIC authentication.'
sys.exit(1)
base64string = base64.encodestring(
'%s:%s' % (username, password))[:-1]
authheader = "Basic %s" % base64string
req.add_header("Authorization", authheader)
try:
handle = urllib2.urlopen(req)
except IOError, e:
print "It looks like the username or password is wrong."
sys.exit(1)
thepage = handle.read()
Thank you for any input.