[Tutor] login

linuxian iandsd pylinuxian at gmail.com
Sun Mar 16 22:37:09 CET 2008


well, you first get to the authenticating page, that is where you put your
username & pass ! & then you post them to be authenticated by server which
then sends you a session id, which is a cookie.

now to make the server think you're loged in to the site you have to send
him back the cookie.

now to send him the cookie you need the cookielib

so lets do it right now :


import urllib, urllib2, cookielib
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
url='http(s)://www.website_to_treat_nicely_and_not_abuse_using_python_scripts.com'
cj=cookielib.LWPCookieJar()
values={'username':'python_user', 'passwd':'elephant'}

# let me explain here what values stand for :
#  values will represent every one element of a form that is in the
authentication page.
#  yes you got it what is between the <form> and </form> & there might be
more
# so you ll have to add them.
#

headers={'User-Agent' : user_agent}
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)

# install_opener ? ???? what the hell is this ? ???
# well, now every request will take with it the cookie you got from server
# in other words you will be logged-in to this website from now on
#

data=urllib.urlencode(values)

# this is where actin starts (login page here)

req=urllib2.Request(url, data, headers)
response=urllib2.urlopen(req)


url2='http(s)://www.website_to_treat_nicely_and_not_abuse_using_python_scripts.com/second_page'
response2=urllib2.urlopen(url2)
response2.read()   #you can get the page because urlopen manages the
session/cookie for you for free.

url3='http(s)://www.website_to_treat_nicely_and_not_abuse_using_python_scripts.com/second_page'
response3=urllib2.urlopen(url3)
response3.read()


well, hope this helps
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080316/28474f75/attachment.htm 


More information about the Tutor mailing list