Accessing $_GET and $_POST query string or form value equivalents in Python
In PHP, I can retrieve the value in a query string like so:
So, if the request URI is http://example.com/index.php?foo=bar
<?php echo $_GET['foo']; //bar ?>
How can I emulate the above code in Python? (and not using a heavy web framework)
I cannot find simple documentation for the easiest way to do this in Python. Is there a standard Python library for handling incoming HTTP requests? I know Python is not a templating language, but its wide usage on the web suggests there should be a simple 开发者_如何学Pythonway of handling this.
All the web frameworks do it differently. As you say, Python is not a templating language, so there is no automatic way to handle HTTP requests.
There is a cgi
module which is part of the standard library, and which you can use to access POSTed data via cgi.FieldStorage()
- but serving an app via standard CGI is horribly inefficient and only suitable for very small-scale stuff.
A much better idea is to use a simple WSGI framework (WSGI is the standard for serving web applications with Python). There are quite a few - my current favourite is flask although I hear good things about bottle too.
Your "non heavy web framework" should typically pass a request parameter to you, and you can access the get strings from the request.GET
or something to that effect.
精彩评论