Python 中如何处理 POST 和 GET 变量? [英] How are POST and GET variables handled in Python?
本文介绍了Python 中如何处理 POST 和 GET 变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 PHP 中,您可以只使用 $_POST
表示 POST 和 $_GET
表示 GET(查询字符串)变量.Python 中的等价物是什么?
In PHP you can just use $_POST
for POST and $_GET
for GET (Query string) variables. What's the equivalent in Python?
推荐答案
假设您要发布一个带有以下内容的 html 表单:
suppose you're posting a html form with this:
<input type="text" name="username">
如果使用 raw cgi:
import cgi
form = cgi.FieldStorage()
print form["username"]
如果使用 Django,Pylons、Flask 或 金字塔:
If using Django, Pylons, Flask or Pyramid:
print request.GET['username'] # for GET form method
print request.POST['username'] # for POST form method
from cherrypy import request
print request.params['username']
form = web.input()
print form.username
print request.form['username']
如果使用 Cherrypy 或 Turbogears,您还可以直接使用参数定义处理函数:
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.
这篇关于Python 中如何处理 POST 和 GET 变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文