Python to PHP Login System (HTTP Post)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • test@test.test

    #1

    Python to PHP Login System (HTTP Post)

    Hi everyone,

    I'm creating a desktop Python application that requires web-based
    authentication for accessing additional application features.

    HTTP GET is really simple.
    HTTP POST is not (at least for me anyway) ;)

    I have tried a few different sources, but I cannot get HTTP POST to
    successfully log in. I can login using FireFox at


    I suggest you register a dummy login to see what I mean (don't enter
    your real e-mail address).

    Now here's some code:
    ------------------------------------------------------------------------------
    msparams = urllib.urlencod e({'user': self.components .User.text,
    'pass': self.components .MagnetSharePas sword.text, 'sublogin': '1'})
    try:
    f = urllib() ***What should go here?***
    fc = f.read()
    fc.close()
    except:
    self.statusBar. text = "Disconnect ed"
    result = dialog.alertDia log(self, 'Couldn\'t connect to
    MagnetShare.com ! Please check your Internet connection, and then try
    again.')
    else:
    print fc
    -------------------------------------------------------------------------------

    Also, could you let us know what modules we should import?
    Thanks for checking this out!

    By the way, the PHP system I'm using is super easy to set up:

    Founded in 1998, evolt.org was one of the oldest Web development communities in existence. Through our mailing lists, articles, and archives, we have strived to work together in sharing our collective knowledge to improve the Web for all of us.

  • Jeethu Rao

    #2
    Re: Python to PHP Login System (HTTP Post)

    You need to use httplib.


    Jeethu Rao

    Comment

    • Justin  Azoff

      #3
      Re: Python to PHP Login System (HTTP Post)

      Jeethu Rao wrote:[color=blue]
      > You need to use httplib.
      > http://docs.python.org/lib/httplib-examples.html
      >
      > Jeethu Rao[/color]

      Not at all. They need to read the documentation for urrlib:

      Source code: Lib/urllib/ urllib is a package that collects several modules for working with URLs: urllib.request for opening and reading URLs, urllib.error containing the exceptions raised by urlli...


      "The following example uses the "POST" method instead:"....

      Additionally, they probably need to use cookielib, otherwise the logged
      in state will not be persistant.

      --
      - Justin

      Comment

      • test@test.test

        #4
        Re: Python to PHP Login System (HTTP Post)

        On 22 Jun 2006 16:19:50 -0700, "Justin Azoff"
        <justin.azoff@g mail.com> wrote:
        [color=blue]
        >Jeethu Rao wrote:[color=green]
        >> You need to use httplib.
        >> http://docs.python.org/lib/httplib-examples.html
        >>
        >> Jeethu Rao[/color]
        >
        >Not at all. They need to read the documentation for urrlib:
        >
        >http://docs.python.org/lib/module-urllib.html
        >http://docs.python.org/lib/node483.html
        >"The following example uses the "POST" method instead:"....
        >
        >Additionally , they probably need to use cookielib, otherwise the logged
        >in state will not be persistant.[/color]

        Here's what's strange... I tried using urllib like this:
        ----------------------------------------------------------------------------------
        try:
        msparams = urllib.urlencod e({'user':
        self.components .User.text, 'pass':
        self.components .MagnetSharePas sword.text, 'sublogin': 1})
        f = urllib.urlopen( "http://www.magnetshare .com/process.php",
        msparams)
        fc = f.read()
        fc.close()
        print fc
        except:
        self.statusBar. text = "Disconnect ed"
        result = dialog.alertDia log(self, 'Couldn\'t connect to
        MagnetShare.com ! Please check your Internet connection, and then try
        again.')
        else:
        print fc
        -----------------------------------------------------------------------------------
        ....and then I visited http://www.magnetshare.com/main.php to see if I
        was logged in. Sure enough I was logged in, but the exception was
        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.

        Comment

        • John J. Lee

          #5
          Re: Python to PHP Login System (HTTP Post)

          test@test.test writes:
          [color=blue]
          > On 22 Jun 2006 16:19:50 -0700, "Justin Azoff"
          > <justin.azoff@g mail.com> wrote:
          >[color=green]
          > >Jeethu Rao wrote:[color=darkred]
          > >> You need to use httplib.
          > >> http://docs.python.org/lib/httplib-examples.html
          > >>
          > >> Jeethu Rao[/color]
          > >
          > >Not at all. They need to read the documentation for urrlib:
          > >
          > >http://docs.python.org/lib/module-urllib.html
          > >http://docs.python.org/lib/node483.html
          > >"The following example uses the "POST" method instead:"....
          > >
          > >Additionally , they probably need to use cookielib, otherwise the logged
          > >in state will not be persistant.[/color][/color]

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

          [color=blue]
          > Here's what's strange... I tried using urllib like this:
          > ----------------------------------------------------------------------------------
          > try:
          > msparams = urllib.urlencod e({'user':
          > self.components .User.text, 'pass':
          > self.components .MagnetSharePas sword.text, 'sublogin': 1})
          > f = urllib.urlopen( "http://www.magnetshare .com/process.php",
          > msparams)
          > fc = f.read()
          > fc.close()
          > print fc
          > except:
          > self.statusBar. text = "Disconnect ed"
          > result = dialog.alertDia log(self, 'Couldn\'t connect to
          > MagnetShare.com ! Please check your Internet connection, and then try
          > again.')
          > else:
          > print fc
          > -----------------------------------------------------------------------------------
          > ...and then I visited http://www.magnetshare.com/main.php to see if I
          > was logged in. Sure enough I was logged in, but the exception was[/color]

          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
          session.

          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.

          [color=blue]
          > 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.[/color]

          Here's one way:

          easy_install mechanize

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

          http://peak.telecommunity.com/DevCen...g-easy-install

          )

          #-------------------------------
          import mechanize

          SHOW_COOKIES = True

          br = mechanize.Brows er()
          if SHOW_COOKIES:
          cj = mechanize.Cooki eJar()
          br.set_cookieja r(cj)
          br.open("http://www.magnetshare .com/main.php")
          br.select_form( nr=0)
          br["user"] = "joe"
          br["pass"] = "password"
          r = br.submit()
          assert "Logged In" in r.get_data()
          if SHOW_COOKIES:
          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
          collected)


          John

          Comment

          • test@test.test

            #6
            Re: Python to PHP Login System (HTTP Post)

            On Sat, 24 Jun 2006 01:28:29 GMT, jjlee@reportlab .com (John J. Lee)
            wrote:
            [color=blue]
            >test@test.te st writes:
            >[color=green]
            >> On 22 Jun 2006 16:19:50 -0700, "Justin Azoff"
            >> <justin.azoff@g mail.com> wrote:
            >>[color=darkred]
            >> >Jeethu Rao wrote:
            >> >> You need to use httplib.
            >> >> http://docs.python.org/lib/httplib-examples.html
            >> >>
            >> >> Jeethu Rao
            >> >
            >> >Not at all. They need to read the documentation for urrlib:
            >> >
            >> >http://docs.python.org/lib/module-urllib.html
            >> >http://docs.python.org/lib/node483.html
            >> >"The following example uses the "POST" method instead:"....
            >> >
            >> >Additionally , they probably need to use cookielib, otherwise the logged
            >> >in state will not be persistant.[/color][/color]
            >
            >Or you may not be able to log in at all, for an everyday meaning of
            >"log in".
            >
            >[color=green]
            >> Here's what's strange... I tried using urllib like this:
            >> ----------------------------------------------------------------------------------
            >> try:
            >> msparams = urllib.urlencod e({'user':
            >> self.components .User.text, 'pass':
            >> self.components .MagnetSharePas sword.text, 'sublogin': 1})
            >> f = urllib.urlopen( "http://www.magnetshare .com/process.php",
            >> msparams)
            >> fc = f.read()
            >> fc.close()
            >> print fc
            >> except:
            >> self.statusBar. text = "Disconnect ed"
            >> result = dialog.alertDia log(self, 'Couldn\'t connect to
            >> MagnetShare.com ! Please check your Internet connection, and then try
            >> again.')
            >> else:
            >> print fc
            >> -----------------------------------------------------------------------------------
            >> ...and then I visited http://www.magnetshare.com/main.php to see if I
            >> was logged in. Sure enough I was logged in, but the exception was[/color]
            >
            >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
            >session.
            >
            >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.
            >
            >[color=green]
            >> 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.[/color]
            >
            >Here's one way:
            >
            >easy_install mechanize
            >
            >(install easy_install first if you don't have that:
            >
            >http://peak.telecommunity.com/DevCen...g-easy-install
            >
            >)
            >
            >#-------------------------------
            >import mechanize
            >
            >SHOW_COOKIES = True
            >
            >br = mechanize.Brows er()
            >if SHOW_COOKIES:
            > cj = mechanize.Cooki eJar()
            > br.set_cookieja r(cj)
            >br.open("htt p://www.magnetshare .com/main.php")
            >br.select_form (nr=0)
            >br["user"] = "joe"
            >br["pass"] = "password"
            >r = br.submit()
            >assert "Logged In" in r.get_data()
            >if SHOW_COOKIES:
            > 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
            >collected)
            >
            >
            >John[/color]

            Thanks a lot John! This "mechanize" was exactly what I was looking
            for. There are some key improvements over urllib2 and also, cookies
            are turned on by default.

            Just an FYI for others, PHP can set $SESSIONID when the user refuses
            cookies. I haven't decided whether the application will use cookies or
            not, but luckily I got the login page response I was looking for. Now,
            I just parse the HTML using Python, and then go to the next screen in
            the MagnetShare application.

            Here's the test code I used.
            ---------------------------------------------------------------------------------------
            import mechanize

            br = mechanize.Brows er()
            br.open("http://www.magnetshare .com/main.php")
            br.select_form( nr=0)
            br["user"] = "test2"
            br["pass"] = "test2"
            response1 = br.submit()
            fc = response1.read( )
            print fc
            ----------------------------------------------------------------------------------------

            Cheers!

            Ben

            Comment

            • John J. Lee

              #7
              Re: Python to PHP Login System (HTTP Post)

              jjlee@reportlab .com (John J. Lee) writes:
              [...][color=blue]
              > #-------------------------------
              > import mechanize
              >
              > SHOW_COOKIES = True
              >
              > br = mechanize.Brows er()
              > if SHOW_COOKIES:
              > cj = mechanize.Cooki eJar()
              > br.set_cookieja r(cj)
              > br.open("http://www.magnetshare .com/main.php")
              > br.select_form( nr=0)
              > br["user"] = "joe"
              > br["pass"] = "password"
              > r = br.submit()
              > assert "Logged In" in r.get_data()
              > if SHOW_COOKIES:
              > for cookie in cj:
              > print cj
              > #-------------------------------[/color]

              That last line should of course have been:

              print cookie

              and not:

              print cj


              John

              Comment

              Working...