Passing arguments to python from URL

C

Casey Bralla

I've got a python cgi-bin application which produces an apache web page. I
want to pass arguments to it on the URL line, but the parameters are not
getting passed along to python properly.

I've been using sys.argv to pick up command line arguments, and it works
fine when I call the python program from the command line. Unfortunately,
when I pass data to the program from the URL, many of the parameters are
being clobbered and **NOT** passed to python.


For example: "http://www.nobody.com/cgi-bin/program.py?sort=ascending" only
passes the parameter "/usr/lib/cgi-bin/program.py".

However, "http://www.nobody.com/cgi-bin/program.py?sort ascending" passes
a 2-place tuple of ("/usr/lib/cgi-bin/program.py", "sort ascending").

Somehow, adding the "=" in the argument list prevents **ANY** parameters
from being passed to python. I could re-write the python program to work
around this, but I sure would like to understand it first.


Can anybody explain this weird behavior?

(Please reply to the news group... my eMail address is a phony to prevent
spam.)
 
D

David M. Cooke

Casey Bralla said:
I've got a python cgi-bin application which produces an apache web page. I
want to pass arguments to it on the URL line, but the parameters are not
getting passed along to python properly.

I've been using sys.argv to pick up command line arguments, and it works
fine when I call the python program from the command line. Unfortunately,
when I pass data to the program from the URL, many of the parameters are
being clobbered and **NOT** passed to python.

For example: "http://www.nobody.com/cgi-bin/program.py?sort=ascending" only
passes the parameter "/usr/lib/cgi-bin/program.py".

This is expected.
However, "http://www.nobody.com/cgi-bin/program.py?sort ascending" passes
a 2-place tuple of ("/usr/lib/cgi-bin/program.py", "sort
ascending").

I don't know why this actually works, it's not (AFAIK) defined behaviour.
Somehow, adding the "=" in the argument list prevents **ANY** parameters
from being passed to python. I could re-write the python program to work
around this, but I sure would like to understand it first.

You're going to have to rewrite. CGI scripts get their arguments
passed to them through the environment, not on the command line.
QUERY_STRING, for instance, will hold the query string (the stuff
after the ?).

Use Python's cgi module to make things easier on yourself; the
documentation has a good overview:
http://www.python.org/doc/2.4/lib/module-cgi.html

In this case, your script would look something like this:

import cgi
form = cgi.FieldStorage()
if form.getvalue('sort') == 'ascending':
... sort in ascending order ...

etc.
 

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

No members online now.

Forum statistics

Threads
474,222
Messages
2,571,142
Members
47,775
Latest member
MadgeMatti

Latest Threads

Top