Connecting Tech Pros Worldwide Forums | Help | Site Map

Get parameters from URL using CGI

abcd
Guest
 
Posts: n/a
#1: Feb 18 '06
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....me=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


Ian Leitch
Guest
 
Posts: n/a
#2: Feb 18 '06

re: Get parameters from URL using CGI


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

abcd wrote:[color=blue]
> 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....me=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[/color]
[color=blue][color=green][color=darkred]
>>> from urlparse import urlparse
>>> dict([n for n in [i.split('=') for i in[/color][/color][/color]
urlparse('http://www.somesite.com/cgi-bin/foo.cgi?name=john&age=90')[4].split('&')]])
{'age': '90', 'name': 'john'}[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFD94zrefZ4eWAXRGIRAlEhAJ49x7kaIe/VcU5DUbt8bv9tQCNrmQCfWOED
tmkTISLqzIKDz2c4mfR6+k8=
=OMOX
-----END PGP SIGNATURE-----
abcd
Guest
 
Posts: n/a
#3: Feb 19 '06

re: Get parameters from URL using CGI


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


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
Expand|Select|Wrap|Line Numbers
  1. url = getTheFullURL()
  2. parse(url)
  3.  
  4. def parse(url):
  5. # parse the parameter names and values
  6.  
John Zenger
Guest
 
Posts: n/a
#4: Feb 19 '06

re: Get parameters from URL using CGI


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[i].value


abcd wrote:[color=blue]
> 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....me=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
>[/color]
Closed Thread