Just like to add an alternate method to accessing the QUERY_STRING
value if you're running a cgi script, you could just do the following:
import os
print "content-type: text/html\n" # so we can print to the webpage
print os.environ['QUERY_STRING']
My testing and understanding is that this also works when there aren't any query strings in the URL, you'd just get an empty string.
This is confirmed to be working on 2.7.6
, view all environment variables like so:
#!/usr/bin/python
import os
print "Content-type: text/html\r\n\r\n";
print "<font size=+1>Environment</font><\br>";
for param in os.environ.keys():
print "<b>%20s</b>: %s<\br>" % (param, os.environ[param])
This snippet of code was obtained from a TutorialsPoint tutorial on CGI Programming with Python.
Although, as zombie_raptor_jesus mentioned, it's probably better to use Python's CGI module, with FieldStorage to make things easier.
Again from the above tutorial:
# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
first_name = form.getvalue('first_name')
last_name = form.getvalue('last_name')
Will save values from the Query String first_name=Bobby&last_name=Ray