[Tutor] using url component as arg in called script?
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Sat Jul 24 03:25:07 CEST 2004
On Fri, 23 Jul 2004, elle yoko suzuki wrote:
>
> let's say that i have a script, someScript.py, that displays
> several words on a web page, as extracted from some external
> text file. each of the words when displayed, is displayed
> while associated with some hypertext link that when
> selected, runs another python script, doAction.py. the link
> is essentially the same, except that a unique value attached
> to a variable name gets attached to the end of the url.
>
> resulting output of someScript.py, html source fragment:
>
> <a href="http://www.someplace.net/cgi-bin/doAction.py?hostname=host1">host1</a>
> <a href="http://www.someplace.net/cgi-bin/doAction.py?hostname=host2">host2</a>
> ...
> <a href="http://www.someplace.net/cgi-bin/doAction.py?hostname=host100">host100</a>
>
> i'd like to be able to take the value (say, hostN,
> N=1,...,100) associated with 'hostname' in the url to be
> passed as an argument to the script that gets called,
> doAction.py. can this be done, and if so, how?
Hi Elle,
Ah! It sounds like you're looking for the 'cgi' module:
http://www.python.org/doc/lib/module-cgi.html
Just for reference: the environmental variable that holds the key/value
parameters is
os.environ.get('QUERY_STRING')
However, you should probably try to avoid parsing QUERY_STRING by hand,
since the 'cgi' module has a well-tested implementation.
There are more details on doing CGI programming on the 'CGI Tools for
Python' topic guide:
http://www.python.org/topics/web/basic-cgi.html
If you have more questions, please feel free to ask. Good luck!
More information about the Tutor
mailing list