在Python中如何处理POST和GET变量? [英] How are POST and GET variables handled in Python?

查看:124
本文介绍了在Python中如何处理POST和GET变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,您可以使用 $ _ POST 作为POST和 $ _ GET 作为GET(查询字符串)变量。

解决方案

假设您使用以下方式发布html表单:

 < input type =textname =username> 

如果使用 raw cgi

  import cgi 
form = cgi.FieldStorage ()
print form [username]

如果使用 Django 主塔 Flask 金字塔

pre $ print request.GET ['username']#for GET表单方法
打印request.POST ['username']#用于POST表单方法

使用 Turbogears Cherrypy

 来自cherrypy import request 
print request.params ['username']

Web.py

  form = web.input()
print form.username

Werkzeug

 打印请求。 form ['username'] 

如果使用Cherrypy或Turbogears,还可以定义处理函数直接参数:

  def index(self,username):
打印用户名

Google App Engine

  class SomeHandler(webapp2.RequestHandler):
def post(self) :
name = self.request.get('username')#这将从名为username的字段获得值
self.response.write(name)#这将写入文档

lly将不得不选择其中一个框架。


In PHP you can just use $_POST for POST and $_GET for GET (Query string) variables. What's the equivalent in Python?

解决方案

suppose you're posting a html form with this:

<input type="text" name="username">

If using raw cgi:

import cgi
form = cgi.FieldStorage()
print form["username"]

If using Django, Pylons, Flask or Pyramid:

print request.GET['username'] # for GET form method
print request.POST['username'] # for POST form method

Using Turbogears, Cherrypy:

from cherrypy import request
print request.params['username']

Web.py:

form = web.input()
print form.username

Werkzeug:

print request.form['username']

If using Cherrypy or Turbogears, you can also define your handler function taking a parameter directly:

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

So you really will have to choose one of those frameworks.

这篇关于在Python中如何处理POST和GET变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆