CGI question

S

sophie_newbie

I was wondering if there was a way to extract everyting in the url
after the "?" question mark in one go.

I have a search page and a results page, and I want the results page to
be able to keep a history of what searches have been performed, but
there is always a different number of search terms so there is always a
differnet number of parameters, ie.

localhost/cgi-bin/results.cgi?TERM1=hello&FIELD1=3&TERM2=python&FIELD2=43&&TERM3=Yallo&FIELD3=21.

Is there any easy way of getting the results.cgi program to read this
entire URL (or even just the bit after the question mark) or is it a
case of writing a comlex bit of code to extract the parameters one by
one and create the URL that way?
 
D

Dan Sommers

I was wondering if there was a way to extract everyting in the url
after the "?" question mark in one go.
[snip]

Is there any easy way of getting the results.cgi program to read this
entire URL (or even just the bit after the question mark) ...

import os
query_string = os.environ['QUERY_STRING']

HTH,
Dan
 
F

Fuzzyman

An easier way is to use the cgi module.

form = cgi.FieldStorage()

form is a dictionary like object where each key is the parameter and
each entry ahs a 'value' attribute that represents the value.

You can use the ``getform`` function from cgiutils to turn it straight
into a dictionary. See http://www.voidspace.org.uk/python/cgiutils.html

The cgi module handles parsing hte query string for you.

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
 
L

LordLaraby

sophie,

If you have the URL in a variable, it's easy to parse it into it's
components (one of which is the query string) like so:
"http://[email protected]:8080/mytext.php;hellothere?this=test+value&and=that+one#anchor")
('http', '(e-mail address removed):8080', '/mytext.php', 'hellothere',
'this=test+value&and=that+one', 'anchor')
print _[4] this=test+value&and=that+one
print _.split('&')
['this=test+value','and=that+one']

Then just use unquote_plus() from urllib to get the original query
arguments.

Hope that fills in some of the missing links. :)

LL
 

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
DewittMill
Top