using cgi form via http and extracting results

B

bmgx

I would like to use an already existing online service (currency
converter) basically consisting of a html form with a few options that
is submitted and returns the results. I really don't know where to start
but I have assumed the following steps need to be taken:

1) Make an http connection to the remote script (http://server/script.cgi)

2) Fill out the html form (1x TEXT and 2x SELECT input fields)

3) Submit the form

4) extract the actual returned result. (using regex or something..)

Any help on which modules to use would be much appreciated!
 
T

Tim Roberts

bmgx said:
I would like to use an already existing online service (currency
converter) basically consisting of a html form with a few options that
is submitted and returns the results. I really don't know where to start
but I have assumed the following steps need to be taken:

1) Make an http connection to the remote script (http://server/script.cgi)

2) Fill out the html form (1x TEXT and 2x SELECT input fields)

3) Submit the form

4) extract the actual returned result. (using regex or something..)

You don't actually need steps 1 and 2 at all. HTTP transactions are all
completely separate. The results of a form submission are just a URL. If
the form has fields "FromCurrency", "ToCurrency", and "Amount", all you
have to do is this:


http://www.currencyservice.com?FromCurrency=dollars&ToCurrency=pounds&Amount=15

You don't have to HAVE the form source in order to submit a request.
 
B

Ben Finney

You don't actually need steps 1 and 2 at all.
True.

HTTP transactions are all completely separate.

True (ignoring cookies for now).
The results of a form submission are just a URL.

Usually false.

The result of most form submissions is an HTTP POST request, which
contains the URL and form content as separate data.
If the form has fields "FromCurrency", "ToCurrency", and "Amount", all
you have to do is this:

http://www.currencyservice.com?FromCurrency=dollars&ToCurrency=pounds&Amount=15

This is only true if the form action is an HTTP GET request, which is a
rather insecure and ugly way to submit form data, and makes it
impossible to submit many kinds of data. HTTP POST is the recommended
method for submitting data to a CGI script.
You don't have to HAVE the form source in order to submit a request.

True. You just need to construct the HTTP POST request correctly.
 
B

bmgx

How would one "create" post data so that I can skip the html form and
access the submission script (in <form method="POST" action="SCRIPT">)
directly. This is simple if the script uses GET as mentioned, but
unfortunately this is not the case which is the very reason for my dilemma..
 
B

bmgx

Ok it's done right..

import urllib
a = urllib.urlopen("http://www.suckerservice.com/convert.cgi",
"Amount=1&From=EUR&To=USD")
b = a.readlines()
c = open("tempresult.html","w")

i = 0
d = "********* local copy ******\n\n"
while i < len(b):
d = d + b
i = i + 1

c.write(d)
c.close()
a.close()
 
J

Joe Francia

bmgx said:
Ok it's done right..

import urllib
a = urllib.urlopen("http://www.suckerservice.com/convert.cgi",
"Amount=1&From=EUR&To=USD")
b = a.readlines()
c = open("tempresult.html","w")

i = 0
d = "********* local copy ******\n\n"
while i < len(b):
d = d + b
i = i + 1

c.write(d)
c.close()
a.close()


Instead of constructing the data string by hand, you can pass a
dictionary or list of 2-item tuples to urllib.urlencode() and get a
properly formatted string:

my_data = urllib.urlencode({'Amount':1,'From':'EUR','To':'USD'}) # or...
my_data = urllib.urlencode([('Amount',1),('From','EUR'),('To','USD')])
a = urllib.urlopen('http://www.suckerservice.com/convert.cgi', my_data)
 
T

Tim Roberts

Ben Finney said:
True (ignoring cookies for now).


Usually false.

The result of most form submissions is an HTTP POST request, which
contains the URL and form content as separate data.

You are correct. However, in my experience, most handlers that expect POST
data will also accept the same information via GET. That is fortunate,
since it makes it easy to do things the way I suggested.
This is only true if the form action is an HTTP GET request, which is a
rather insecure and ugly way to submit form data, and makes it
impossible to submit many kinds of data. HTTP POST is the recommended
method for submitting data to a CGI script.

There is no such "recommendation". GET is entirely appropriate for many
kinds of information. A currency conversion service such as the one the
O.P. described is a perfect example. There is no need for security in such
a service, and it makes it easy to do canned requests like the one above.
 
P

Paul Rubin

Basically you have to examine the html source for the page you want to
automate, and figure out how the form works. To do that, you need to
understand html and http, there is no way around it. Maybe someone
here can suggest a good reference for that.

Once you've figured out the form, just use the urllib module to build
and send the appropriate http request.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top