如何在Python中处理POST和GET变量?

Cli*_*ote 134 python post get

在PHP中,您可以使用$_POSTPOST和$_GETGET(查询字符串)变量.什么是Python中的等价物?

nos*_*klo 237

假设你发布了一个html表单:

<input type="text" name="username">
Run Code Online (Sandbox Code Playgroud)

如果使用原始cgi:

import cgi
form = cgi.FieldStorage()
print form["username"]
Run Code Online (Sandbox Code Playgroud)

如果使用Django,Pylons,FlaskPyramid:

print request.GET['username'] # for GET form method
print request.POST['username'] # for POST form method
Run Code Online (Sandbox Code Playgroud)

使用Turbogears,Cherrypy:

from cherrypy import request
print request.params['username']
Run Code Online (Sandbox Code Playgroud)

Web.py:

form = web.input()
print form.username
Run Code Online (Sandbox Code Playgroud)

Werkzeug:

print request.form['username']
Run Code Online (Sandbox Code Playgroud)

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

def index(self, username):
    print username
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

所以你真的必须选择其中一个框架.

  • 使用`import cgi`,不是'print form ["username"].value`? (18认同)
  • FieldStorage在python3中被破坏,你可能会遇到问题.http://bugs.python.org/issue6234 (2认同)
  • 请允许我澄清我先前的评论; 本页:http://lucumr.pocoo.org/2013/7/2/the-updated-guide-to-unicode/更好地解释了它.**由于编码问题,请不要在python 3中使用FieldStorage().** (2认同)

Tom*_*Tom 31

我发现nosklo的答案非常广泛而有用!对于像我这样的人,可能会发现直接访问原始请求数据也很有用,我想添加方法来做到这一点:

import os, sys

# the query string, which contains the raw GET data
# (For example, for http://example.com/myscript.py?a=b&c=d&e
# this is "a=b&c=d&e")
os.getenv("QUERY_STRING")

# the raw POST data
sys.stdin.read()
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢.获取原始数据始终是我的第一步 (7认同)

Sch*_*ien 31

我知道这是一个老问题.但令人惊讶的是,没有给出好的答案.

首先,问题是完全有效的,没有提到框架.CONTEXT是PHP语言等价.虽然有很多方法可以在Python中获取查询字符串参数,但框架变量只是方便地填充.在PHP中,$ _GET和$ _POST也是便利变量.它们分别从QUERY_URI和php://输入解析.

在Python中,这些函数将是os.getenv('QUERY_STRING')和sys.stdin.read().记得导入os和sys模块.

我们在这里必须小心使用"CGI"这个词,尤其是在谈论两种语言时,以及在与Web服务器连接时的共性.1. CGI作为协议定义了HTTP协议中的数据传输机制.2. Python可以配置为在Apache中作为CGI脚本运行.3. Python中的cgi模块提供了一些便利功能.

由于HTTP协议是与语言无关的,并且Apache的CGI扩展也与语言无关,因此获取GET和POST参数应仅具有跨语言的语法差异.

这是填充GET字典的Python例程:

GET={}
args=os.getenv("QUERY_STRING").split('&')

for arg in args: 
    t=arg.split('=')
    if len(t)>1: k,v=arg.split('='); GET[k]=v
Run Code Online (Sandbox Code Playgroud)

并为POST:

POST={}
args=sys.stdin.read().split('&')

for arg in args: 
    t=arg.split('=')
    if len(t)>1: k, v=arg.split('='); POST[k]=v
Run Code Online (Sandbox Code Playgroud)

您现在可以访问以下字段:

print GET.get('user_id')
print POST.get('user_name')
Run Code Online (Sandbox Code Playgroud)

我还必须指出cgi模块不能正常工作.考虑这个HTTP请求:

POST / test.py?user_id=6

user_name=Bob&age=30
Run Code Online (Sandbox Code Playgroud)

使用cgi.FieldStorage().getvalue('user_id')将导致空指针异常,因为模块盲目地检查POST数据,忽略了POST请求也可以携带GET参数的事实.


Eva*_*ark 27

它们存储在CGI fieldstorage对象中.

import cgi
form = cgi.FieldStorage()

print "The user entered %s" % form.getvalue("uservalue")
Run Code Online (Sandbox Code Playgroud)

  • 我不确定你为什么做-1.我的意思是,我给了什么作品.也许他无法使用框架.另外,大多数框架都没有在后台使用它吗? (10认同)
  • 是愚蠢做-1,我有+1来平衡它,加上我认为这是最好的方法,因为它返回一个刺(这是要求的) (4认同)
  • -1.根据所使用的libs/framework,有很多表示请求对象. (2认同)