Python to PHP Login System (HTTP Post)

John J. Lee jjlee at
Sat Jun 24 03:28:29 CEST 2006

test at test.test writes:

> On 22 Jun 2006 16:19:50 -0700, "Justin  Azoff"
> <justin.azoff at> wrote:
> >Jeethu Rao wrote:
> >> You need to use httplib.
> >>
> >>
> >> Jeethu Rao
> >
> >Not at all.  They need to read the documentation for urrlib:
> >
> >
> >
> >"The following example uses the "POST" method instead:"....
> >
> >Additionally, they probably need to use cookielib, otherwise the logged
> >in state will not be persistant.

Or you may not be able to log in at all, for an everyday meaning of
"log in".

> Here's what's strange... I tried using urllib like this:
> ----------------------------------------------------------------------------------
> try:
> 	msparams = urllib.urlencode({'user':
> self.components.User.text, 'pass':
> self.components.MagnetSharePassword.text, 'sublogin': 1})
> 	 f = urllib.urlopen("",
> msparams)
> 	fc =
> 	fc.close()
> 	print fc
> except:
> 	self.statusBar.text = "Disconnected"
> 	result = dialog.alertDialog(self, 'Couldn\'t connect to
>! Please check your Internet connection, and then try
> again.')
> else:
> 	print fc
> -----------------------------------------------------------------------------------
> ...and then I visited to see if I
> was logged in. Sure enough I was logged in, but the exception was

That's not how it works (assuming you visited that URL in a browser,
not using Python).  The "logged-in-ness" comes from a "session ID"
cookie that is stored in your browser (or in your Python code).  The
server sends a cookie when you log in (and usually stores your cookie
in a database).  The browser keeps the cookie.  When you come back
later using the same browser (maybe even after you've closed the
browser, if it's the right kind of cookie), your browser sends the
cookie back and the server looks up the session ID from that cookie in
the database, and sees it's you.

If you come back using a different browser (and your Python program is
effectively just a different browser than your copy of Firefox or IE
or whatever), then the server won't remember who you are, so you're
not logged in *in that browser session*, even if the server has you
recorded in its database as logged in from a different browser

So, the fact that you saw yourself as logged in when you looked using
your web browser doesn't really help your Python program -- it's still
out in the cold.

> thrown anyway. I commented out the urlopen, f, and fc  lines and
> tested it again. This time I made it to "else:"
> I'm stumped. I'm glad that the user can log in; however, the
> MagnetShare application needs to read in the response from the server,
> and then decide what to do with the information.

Here's one way:

easy_install mechanize

(install easy_install first if you don't have that:


import mechanize


br = mechanize.Browser()
    cj = mechanize.CookieJar()
br["user"] = "joe"
br["pass"] = "password"
r = br.submit()
assert "Logged In" in r.get_data()
    for cookie in cj:
        print cj

(note the cookiejar is always there; you only need to create one and
pass it in in order to get at it to e.g. print out the cookies you've


More information about the Python-list mailing list