argument to python cgi script

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Apr 10 09:28:43 CEST 2008


En Wed, 09 Apr 2008 11:14:33 -0300, syed mehdi <smmehadi at gmail.com>  
escribió:

> Hi Guys,
> If someone can help me in telling how can i pass arguments to python cgi
> script then that will be good.
> like if i want to pass "some argument" to my remote python script, by
> calling something like:
> http://localhost/cgi-bin/test.py?some\ argument.
> What is the correct way of sending arguments in this way, and how can i
> decode/receive them in test.py

Encoding:

py> args = {'some': 'argument', 'x': 1, 'z': 23.4}
py> import urllib
py> urllib.urlencode(args)
'x=1&z=23.4&some=argument'

You can then use urllib.urlopen or urllib2.urlopen to send the HTTP  
request and retrieve the response.
http://docs.python.org/lib/module-urllib.html

In the server side, use cgi.FieldStorage:

form = cgi.FieldStorage()
x = form.getfirst('x', 0) # '1'
y = form.getfirst('y', 0) # '0'
z = form.getfirst('z', 0) # '23.4'

http://docs.python.org/lib/module-cgi.html

-- 
Gabriel Genellina




More information about the Python-list mailing list