473,609 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
http://www.magnetshare.com/main.php

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:

http://www.evolt.org/article/PHP_Log...384/index.html
Jun 22 '06 #1
6 3435
You need to use httplib.
http://docs.python.org/lib/httplib-examples.html

Jeethu Rao
Jun 22 '06 #2
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.

--
- Justin

Jun 22 '06 #3
On 22 Jun 2006 16:19:50 -0700, "Justin Azoff"
<ju**********@g mail.com> wrote:
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.


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.
Jun 23 '06 #4
te**@test.test writes:
On 22 Jun 2006 16:19:50 -0700, "Justin Azoff"
<ju**********@g mail.com> wrote:
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.


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

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:

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
Jun 24 '06 #5
On Sat, 24 Jun 2006 01:28:29 GMT, jj***@reportlab .com (John J. Lee)
wrote:
te**@test.te st writes:
On 22 Jun 2006 16:19:50 -0700, "Justin Azoff"
<ju**********@g mail.com> wrote:
>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.


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


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.

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:

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


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
Jun 24 '06 #6
jj***@reportlab .com (John J. Lee) writes:
[...]
#-------------------------------
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
#-------------------------------


That last line should of course have been:

print cookie

and not:

print cj
John
Jun 24 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1653
by: Dfenestr8 | last post by:
Hi. I've written a cgi messageboard script in python, for an irc chan I happen to frequent. Bear with me, it's hard for me to describe what the bug is. So I've divided this post into two sections: HOW MY SCRIPTS WORKS, and WHAT THE BUG IS.
1
7892
by: Tom Jones | last post by:
Hi, I am using the HttpWebRequest and HttpWebResponse classes to pull information from a web server on the internet. I have an account on one of the webservers that I need to log into programatically. The connection is not secure (not https, etc.). The login page just has simple name & password textboxes on it. Can someone please tell me how I can programatically login using the
0
2447
by: Alimah | last post by:
My objective is to log onto a wiki account (specifically wikipedia) using the http proxies provided by them (145.97.39.130 - 145.97.39.140:80). The operating system is Windows XP/Windows Server 2003. Or more conveniently, what do I need to change in the standard python library so that all python>>internet interaction would be done through a proxy? (I'm guessing urlib2.py?) I have a program login.py (full script:...
28
2616
by: H J van Rooyen | last post by:
Hi, I want to write a small system that is transaction based. I want to split the GUI front end data entry away from the file handling and record keeping. Now it seems almost trivially easy using the sockets module to communicate between machines on the same LAN, so that I want to do the record keeping on one machine.
6
3341
by: AppleBag | last post by:
I'm having the worst time trying to login to myspace through code. Can someone tell me how to do this? Please try it yourself before replying, only because I have asked this a couple of times in the past in other places, and while the help was much appreciated, it seemed everyone just wanted to 'theoretically' explain how to do it, but when I tried to do it myself, I couldn't login. I want to simply pass the email address and password to...
0
12765
by: barrybevel | last post by:
Hi, I'm trying to login to the www.vodafone.ie website using HttpWebRequest. It works fine with IE/Firefox and the .NET Web Control too, just not with my code. I think it's a redirect 302 problem. I'm using this code in a ASP.NET 2.0 application just in case that matters, maybe someone knows a better way to do this?
19
3283
by: klenwell | last post by:
Another request for comments here. I'd like to accomplish something like the scheme outlined at this page here: http://tinyurl.com/3dtcdr In a nutshell, the form uses javascript to hash (md5) the password field using a random one-time salt (nonce) -- generated by php and pasted in the form -- that is then posted with the hashed password
0
8129
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8571
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8404
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6997
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4017
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
1386
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.