In PHP you can just use $_POST
for POST and $_GET
for GET (Query string) variables. What's the equivalent in Python?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
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 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:
and for POST:
You can now access the fields as following:
I must also point out that the cgi module doesn't work well. Consider this HTTP request:
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.
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
suppose you're posting a html form with this:
If using raw cgi:
If using Django, Pylons, Flask or Pyramid:
Using Turbogears, Cherrypy:
Web.py:
Werkzeug:
If using Cherrypy or Turbogears, you can also define your handler function taking a parameter directly:
Google App Engine:
So you really will have to choose one of those frameworks.
They are stored in the CGI fieldstorage object.
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:
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.