In PHP you can just use $_POST
for POST and $_GET
for GET (Query string) variables. What's the equivalent in Python?

- 31,277
- 10
- 71
- 76

- 261,656
- 265
- 575
- 769
-
Are you writing a CGI script, mod_python, or Django (or other framework) application? The answer hinges on a bit more info... – Rob Jan 21 '09 at 04:03
-
can you provide sample code for each of these? – Ali Jan 21 '09 at 04:26
-
2CGI, mod_python, Django, CherryPy and Pylons aren't code samples. They're Python Web Frameworks that handle GET and POST. See http://wiki.python.org/moin/WebFrameworks for information. Your question -- as asked -- cannot be answered. – S.Lott Jan 21 '09 at 11:19
6 Answers
suppose you're posting a html form with this:
<input type="text" name="username">
If using raw cgi:
import cgi
form = cgi.FieldStorage()
print form["username"]
If using Django, Pylons, Flask or Pyramid:
print request.GET['username'] # for GET form method
print request.POST['username'] # for POST form method
Using Turbogears, Cherrypy:
from cherrypy import request
print request.params['username']
form = web.input()
print form.username
print request.form['username']
If using Cherrypy or Turbogears, you can also define your handler function taking a parameter directly:
def index(self, username):
print username
class SomeHandler(webapp2.RequestHandler):
def post(self):
name = self.request.get('username') # this will get the value from the field named username
self.response.write(name) # this will write on the document
So you really will have to choose one of those frameworks.

- 129,958
- 22
- 279
- 321

- 217,122
- 57
- 293
- 297
-
20
-
I am using `Bottle` which I believe uses `wsgi`, could anyone post the equivalent to use in that scenario? The above response is the clearest explanation I have come across, it just doesn't include my scenario. – user1063287 Sep 21 '13 at 12:16
-
2FieldStorage is broken in python3, you may experience issues with it. http://bugs.python.org/issue6234 – NuclearPeon Oct 01 '13 at 00:09
-
2Allow me to clarify on my previous comment; this page: http://lucumr.pocoo.org/2013/7/2/the-updated-guide-to-unicode/ better explains it. **Do not use FieldStorage() in python 3 because of encoding issues.** – NuclearPeon Jul 07 '14 at 20:44
-
For Flask it's a little different: request.args.get('username') – Damjan Pavlica Jan 03 '15 at 22:58
-
@NuclearPeon I am facing trouble in python 2.7.6 too. When I print value returned from cgi.FeildStorage, this is what output is FieldStorage(None, None, []). I have posted a full question here http://stackoverflow.com/questions/38024516/python-cgi-script-for-http-server-post-variables-are-being-received-as-none – Chor Sipahi Jun 25 '16 at 03:44
-
For CGI, `form["username"]` had to be `form.getvalue("username")` for me as described [here](https://stackoverflow.com/a/464087/6243352). – ggorlen Dec 08 '22 at 01:35
I know this is an old question. Yet it's surprising that no good answer was given.
First of all the question is completely valid without mentioning the framework. The CONTEXT is a PHP language equivalence. Although there are many ways to get the query string parameters in Python, the framework variables are just conveniently populated. In PHP, $_GET
and $_POST
are also convenience variables. They are parsed from QUERY_URI and php://input respectively.
In Python, these functions would be os.getenv('QUERY_STRING')
and sys.stdin.read()
. Remember to import os and sys modules.
We have to be careful with the word "CGI" here, especially when talking about two languages and their commonalities when interfacing with a web server. 1. CGI, as a protocol, defines the data transport mechanism in the HTTP protocol. 2. Python can be configured to run as a CGI-script in Apache. 3. The CGI module in Python offers some convenience functions.
Since the HTTP protocol is language-independent, and that Apache's CGI extension is also language-independent, getting the GET and POST parameters should bear only syntax differences across languages.
Here's the Python routine to populate a GET dictionary:
GET={}
args=os.getenv("QUERY_STRING").split('&')
for arg in args:
t=arg.split('=')
if len(t)>1: k,v=arg.split('='); GET[k]=v
and for POST:
POST={}
args=sys.stdin.read().split('&')
for arg in args:
t=arg.split('=')
if len(t)>1: k, v=arg.split('='); POST[k]=v
You can now access the fields as following:
print GET.get('user_id')
print POST.get('user_name')
I must also point out that the CGI module doesn't work well. Consider this HTTP request:
POST / test.py?user_id=6
user_name=Bob&age=30
Using CGI.FieldStorage().getvalue('user_id')
will cause a null pointer exception because the module blindly checks the POST data, ignoring the fact that a POST request can carry GET parameters too.

- 797
- 2
- 16
- 38

- 3,855
- 1
- 16
- 29
-
I got this error: `AttributeError: 'NoneType' object has no attribute 'split'` using `CGI` with `Python` 2.7 – candlejack Jul 17 '16 at 20:02
-
1About the error @candlejack said, try getting the arguments like this `POST.get('user_name', 'default_value')`. – Georgios Syngouroglou Mar 20 '18 at 16:31
-
@GeorgeSiggouroglou The 'default_value' thing solved my issue. Thanks! – umbe1987 Mar 12 '19 at 13:35
-
1
-
I've found nosklo's answer very extensive and useful! For those, like myself, who might find accessing the raw request data directly also useful, I would like to add the way to do that:
import os, sys
# the query string, which contains the raw GET data
# (For example, for http://example.com/myscript.py?a=b&c=d&e
# this is "a=b&c=d&e")
os.getenv("QUERY_STRING")
# the raw POST data
sys.stdin.read()

- 4,910
- 5
- 33
- 48
-
I am trying to **call a method** in a cgi file. An example url is `http://www.myserver.com/cgi-bin/cgi.py/ThisIsMyMethod`. The following environmental variables are related: `os.environ.get('PATH_INFO')` which gets the method name (eg: /ThisIsMyMethod) and `os.environ.get('SCRIPT_NAME')` which provides the path to the script from the web host's root folder (eg: /cgi-bin/cgi.py). In my case, QUERY_STRING is blank, as I am using POST. – NuclearPeon Jul 07 '14 at 20:52
They are stored in the CGI fieldstorage object.
import cgi
form = cgi.FieldStorage()
print "The user entered %s" % form.getvalue("uservalue")

- 98,895
- 36
- 105
- 117
-
2-1. there are quite a few representation of the request object, depending on the libs/framework used. – bruno desthuilliers Jan 21 '09 at 09:42
-
10I'm not sure why you did -1. I mean, what I gave works. Perhaps he is unable to use a framework. ALso, don't most frameworks just use this in the background? – Evan Fosmark Jan 21 '09 at 20:37
-
4Was stupid to do -1, I've +1 to balance it, plus I think this is the best method as it returns a sting (which is what's asked for) – joedborg Sep 24 '12 at 15:55
-
1@Liam Not sure why you're getting None, but this post is over 6 years old, so I wouldn't doubt if things have changed. – Evan Fosmark Jun 17 '15 at 16:41
-
@EvanFosmark I am getting None values by using this. When I print value returned from cgi.FeildStorage, this is what output is FieldStorage(None, None, []). I have posted a full question here http://stackoverflow.com/questions/38024516/python-cgi-script-for-http-server-post-variables-are-being-received-as-none Any help will be appreciated. – Chor Sipahi Jun 25 '16 at 03:50
It somewhat depends on what you use as a CGI framework, but they are available in dictionaries accessible to the program. I'd point you to the docs, but I'm not getting through to python.org right now. But this note on mail.python.org will give you a first pointer. Look at the CGI and URLLIB Python libs for more.
Update
Okay, that link busted. Here's the basic wsgi ref

- 110,348
- 25
- 193
- 263
-
If you're not ambitious enough to follow a link, I'm not ambitious enough to cut and paste if from the link. – Charlie Martin Jan 21 '09 at 17:37
-
1and now the link is dead and that's why everyone hates answers like this and downvotes them. Congratulations. – John Tyree Oct 05 '14 at 19:02
-
2You know, every time I get a comment from someone bitching that a five -- nearly six -- year old answer now has a broken link, without, say, adding a replacement link, I can bet it's someone with rep < 1000. – Charlie Martin Oct 05 '14 at 20:19
-
If I knew where to go for the replacement link (hint, I didn't because your link didn't work) I wouldn't have been looking this question up. – John Tyree Oct 05 '14 at 23:56
-
You *could* always try the hint about 'CGI and URLLIB python libs' I suppose. – Charlie Martin Oct 06 '14 at 16:34
Python is only a language, to get GET and POST data, you need a web framework or toolkit written in Python. Django is one, as Charlie points out, the cgi and urllib standard modules are others. Also available are Turbogears, Pylons, CherryPy, web.py, mod_python, fastcgi, etc, etc.
In Django, your view functions receive a request argument which has request.GET and request.POST. Other frameworks will do it differently.

- 364,293
- 75
- 561
- 662
-
14"to get GET and POST data, you need a web framework or toolkit written in Python" - Simply not true – James Tomasino Jul 30 '13 at 15:42
-
2I agree with James, if they are written in Python they can clearly be written again to your own preference. – Robin Sandström Aug 18 '14 at 19:53
-
Ok I am a new comer in python and I thought it can't be possible that I have to use a framework. My question is what is the way?I am not using any framework and I had apache2 serve a python script. It will be nice showing a dead simple way as the usage of $_GET or $_POST in php. – black sensei Sep 08 '14 at 11:07
-
1