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