[python-uk] automate logging

leo davis leo_365 at yahoo.com
Tue Jul 22 12:18:05 CEST 2008


I'm trying to write a code to automate logging into a website.I have chosen the forum 'www.tek-tips.com' as an example and tried this script on it....But it doesnt seem to work...What am i missing here..plz help

Code:import urllib2

theurl = 'www.tek-tips.com'
protocol = 'http://'
username = 'johnny'
password = 'yesyes'

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)

# 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(protocol + theurl)
# authentication is now handled automatically for us
print the_page

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-uk/attachments/20080722/2d4470d5/attachment.htm>

More information about the python-uk mailing list