How to fill a form

S

Sulsa

I need to fill form from web site, the code for this form looks like
this:

<form action="login.php" method="post" target="_top">

<input type="text" name="username" size="25" maxlength="40"
class="post" />
<input type="password" name="password" size="25" maxlength="25"
class="post" />

<input type="hidden" name="redirect"
value="" />
<input type="submit" name="login" class="mainoption"
value="Login" />

</form>

I can of course connect to a web site and download the form, i do it
like this:

import urllib2
www = urllib2.urlopen("http://www.site.com/login.php")
form_data= stronka.read()

or using httplib:

import httplib
conn = httplib.HTTPConnection("www.site.com")
conn.request("GET", "/login.php")
data1 = conn.getresponse().read()


but i can't fill and send this form is there some simple way to do it?
 
S

Steve Holden

Sulsa said:
I need to fill form from web site, the code for this form looks like
this:

<form action="login.php" method="post" target="_top">

<input type="text" name="username" size="25" maxlength="40"
class="post" />
<input type="password" name="password" size="25" maxlength="25"
class="post" />

<input type="hidden" name="redirect"
value="" />
<input type="submit" name="login" class="mainoption"
value="Login" />

</form>

I can of course connect to a web site and download the form, i do it
like this:

import urllib2
www = urllib2.urlopen("http://www.site.com/login.php")
form_data= stronka.read()

or using httplib:

import httplib
conn = httplib.HTTPConnection("www.site.com")
conn.request("GET", "/login.php")
data1 = conn.getresponse().read()


but i can't fill and send this form is there some simple way to do it?

I'd recommend you take a look at mechanize, see

http://wwwsearch.sourceforge.net/mechanize/

and specifically at ClientForm, see

http://wwwsearch.sourceforge.net/ClientForm/

These make it easy to perform browser-like accesses to forms-based web
applications.

regards
Steve
 
G

Grant Edwards

I want to fill only one smiple form so i would like not to use
any non standard libraries.

Then just send the HTTP "POST" request containing the fields
and data you want to submit.
 
S

Steve Holden

Sulsa said:
I want to fill only one smiple form so i would like not to use any non
standard libraries.

That's your choice, but frankly I'd be very surprised if it wasn't
quicker to download and use mechanize/clientform than it was to put your
own solution together.

regards
Steve
 
S

Sulsa

Then just send the HTTP "POST" request containing the fields
and data you want to submit.

but i don't know how to post these data if i knew there there would
be no topic.
 
T

Tom Brown

but i don't know how to post these data if i knew there there would
be no topic.

When I need to learn the POST method for a form, I use wireshark
(www.wireshark.org) to capture the POST I do manually the first time.
However, I think I'm going to look into mechanize/clientform now that I
know about it.

Hope this helps,
Tom
 
J

John J. Lee

Sulsa said:
but i don't know how to post these data if i knew there there would
be no topic.

Something like this (UNTESTED, and I can never remember all the
details, which are fiddlier than they may look):

import urllib, urllib2
query = urllib.urlencode([
("username", "sulsa"), ("password", "sulsa"),
("redirect", ""), ("login", "Login"),
])
r = urllib2.urlopen("http://example.com/login.php", query)
print r.read()


Note that urllib and urllib2 both, as their main job in life, open
URLs. urllib also has miscellaneous functions related to URLs &c. I
use urllib2 above because I know it better and because it can handle
some stuff that urllib doesn't (it's designed more for extensibility
than is urllib).


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top