[Tutor] login

Kent Johnson kent37 at tds.net
Sat Mar 15 17:44:34 CET 2008

SwartMumba snake wrote:
> I am trying to get the cookies from the site when I login, though 
> html.info(). The problem is that when I check the source ( html.read() ) 
> it shows that it does not recognize me as logged in. Also, if I chech 
> html.info(), the cookies that I am suppose to receive, if I logged in 
> successfully, are not there. What am I doing wrong?

urllib2 does not support cookies by default, you have to configure it 
with a CookieJar:

import cookielib, urllib2

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

Then use this opener as below. I think you will have to look at the 
CookieJar to see the cookies.


> opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 
> 6.0; en-US; rv: Gecko/20080201 Firefox/'),
>                      ('Referer', 'http://www.site.org/index.php')
>                      ]
> values = urllib.urlencode({'user_name': 'MyUsername', 'user_pass': 
> 'MyPass^^', 'login' : 'Login'})
> req = urllib2.Request('http://www.site.org/index.php', values)
> html = opener.open(req)

More information about the Tutor mailing list