logging into a website using python

leo davis leo_365 at yahoo.com
Wed Jul 23 12:23:59 CEST 2008


Hi,
 
I''m trying achieve the following by running python programs
-login to a website 
-automate filling up web forms 
 
So far I 've tried out the following codes:
import cookielib, urllib, urllib2

login = 'cod45'
password = 'mell'

# Enable cookie support for urllib2
cookiejar = cookielib.CookieJar()
urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))

# Send login/password to the site and get the session cookie
values = {'login':login, 'password':password }
data = urllib.urlencode(values)
request = urllib2.Request("https://student-webmail.surrey.ac.uk/webmail/", data)
url = urlOpener.open(request)  # Our cookiejar automatically receives the cookies
page = url.read(500000)

# Make sure we are logged in by checking the presence of the cookie "id".
# (which is the cookie containing the session identifier.)
if not 'id' in [cookie.name for cookie in cookiejar]:
    raise ValueError, "Login failed with login=%s, password=%s" % (login,password)

print "We are logged in !"
 
 
***I get this error msg:Login failed with login cod45' &  password
 
 
Code 2:
 
import urllib2
theurl = 'www.tek-tips.com'
protocol = 'http://'
username = 'ub007'
password = 'with'

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(protocol + theurl)
# authentication is now handled automatically for us
the_page=pagehandle.read()
print the_page
 
**** This code prints out the html for  the home page ''www.tek-tips.com'.I was expecting it would log me in and take me to the members page.....What am i missing here?
Why is it that the script doesnt attempt a login?Thanks in advance
 
Cheers
David



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080723/373feebb/attachment.html>


More information about the Python-list mailing list