如何用Python处理POST和GET变量?

发布于 2021-02-02 23:16:32

在PHP中,你只能将其$_POST用于POST$_GETGET(查询字符串)变量。Python中的等效功能是什么?

关注者
0
被浏览
114
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    假设你正在发布带有以下内容的html表单:

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

    如果使用原始cgi

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

    如果使用Django,Pylons,FlaskPyramid

    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']
    

    如果使用CherrypyTurbogears,还可以直接使用参数定义处理程序函数:

    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
    

    因此,你实际上必须选择这些框架之一。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看