Python では POST 変数と GET 変数はどのように処理されますか? 質問する

Python では POST 変数と GET 変数はどのように処理されますか? 質問する

$_POSTPHP では、POST とGET (クエリ文字列) 変数を使用できます$_GET。Python で同等のものは何ですか?

ベストアンサー1

次のような HTML フォームを投稿するとします。

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

使用する場合生のCGI:

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

使用する場合ジャンゴパイロンフラスコまたはピラミッド:

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 を使用する場合は、パラメータを直接受け取るハンドラー関数を定義することもできます。

def index(self, username):
    print username

Google アプリエンジン:

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

したがって、実際にはこれらのフレームワークのいずれかを選択する必要があります。

おすすめ記事