[Tutor] urllib2 and php authentication

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Sun Jul 27 10:50:53 CEST 2008


W W schreef:
> On Sat, Jul 26, 2008 at 7:31 PM, Daniele <d.conca at gmail.com 
> <mailto:d.conca at gmail.com>> wrote:
> 
>     Hi,
>     I'm trying to connect to a site that requires a php login. I used the
>     urllib2 module in the following way:
> 
> 
> It requires a php login, or php handles the login?
> 
>      >>> import urllib2
>      >>> user='xxx'
>      >>> password='yyy'
>      >>> hp = urllib2.HTTPPasswordMgr()
>      >>> uri =
>     'http://s2.ikariam.it/index.php?action=loginAvatar&function=login
>     <http://s2.ikariam.it/index.php?action=loginAvatar&function=login>'
>      >>> hp.add_password(None, uri, user, password)
>      >>> opener = urllib2.build_opener(urllib2.HTTPBasicAuthHandler( hp))
>      >>> req = urllib2.Request(uri)
>      >>> opener.open(req).readlines()
> 
>     Needless to say, it doesn't work. I get an html page back from the
>     opener saying username and/or password are invalid.
>     Can anyone point me in the right direction?
> 
> 
> If php is handling the login, my guess is that it's receiving the info 
> via either POST or GET.
> 
> And if you don't have access to the script, and you're not sure what the 
> names of the values are, that might require some trial and error.

I suggest you use something like the Live HTTP Headers plugin in Firefox 
to see exactly what happens when you log in manually. That should give 
you enough information to do the same thing from your program.

-- 
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
   -- Isaac Asimov

Roel Schroeven



More information about the Tutor mailing list