如何用Python处理POST和GET变量?
在PHP中,你只能将其$_POST
用于POST
和$_GETGET
(查询字符串)变量。Python中的等效功能是什么?
-
假设你正在发布带有以下内容的html表单:
<input type="text" name="username">
如果使用原始
cgi
:import cgi form = cgi.FieldStorage() print form["username"]
如果使用
Django,Pylons,Flask
或Pyramid
:print request.GET['username'] # for GET form method print request.POST['username'] # for POST form method
使用
Turbogears,Cherrypy
:from cherrypy import request print request.params['username']
Web.py
:form = web.input() print form.username
Werkzeug
:print request.form['username']
如果使用
Cherrypy
或Turbogears
,还可以直接使用参数定义处理程序函数:def index(self, username): print username
Google App Engine:
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
因此,你实际上必须选择这些框架之一。