Get parameters from URL using CGI

I

Ian Leitch

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
i want to create a CGI script which simply prints out values given via
the URL (such as when a GET is performed).

So if I have a script named, foo.cgi ....and I access it by going to:

http://www.somesite.com/cgi-bin/foo.cgi?name=john&age=90

I want foo.cgi to print out:
name: john
age: 90


how do i get the values from the URL like that?

thanks
from urlparse import urlparse
dict([n for n in [i.split('=') for i in
urlparse('http://www.somesite.com/cgi-bin/foo.cgi?name=john&age=90')[4].split('&')]])
{'age': '90', 'name': 'john'}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFD94zrefZ4eWAXRGIRAlEhAJ49x7kaIe/VcU5DUbt8bv9tQCNrmQCfWOED
tmkTISLqzIKDz2c4mfR6+k8=
=OMOX
-----END PGP SIGNATURE-----
 
A

abcd

Ian said:
from urlparse import urlparse
dict([n for n in [i.split('=') for i in
urlparse('http://www.somesite.com/cgi-bin/foo.cgi?name=john&age=90')[4].split('&')]])
{'age': '90', 'name': 'john'}


Ian, thanks for the reply, but that is not what I need to do. Inside
foo.cgi how can I parse out the values given in the URL?

So if you visit: http://www.somesite.com/cgi-bin/foo.cgi?valA=1&valB=2
......I want the script, foo.cgi to print out the parameters and their
values. It's dynamic so the values are url wont be the same all the
time. How do i get the URL from the script? does this make sense?

So I'm thinking foo.cgi will look like
Code:
url = getTheFullURL()
parse(url)

def parse(url):
    # parse the parameter names and values
 
D

Dennis Lee Bieber

Ian, thanks for the reply, but that is not what I need to do. Inside
foo.cgi how can I parse out the values given in the URL?
Have you studied the cgi module documentation?
--
 
J

John Zenger

import cgi
import cgitb; cgitb.enable() # Optional; for debugging only

print "Content-Type: text/html"
print

f = cgi.FieldStorage()
for i in f.keys():
print "<p>",i,":",f.value
 

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
474,285
Messages
2,571,416
Members
48,108
Latest member
AmeliaAmad

Latest Threads

Top