解析tornado查詢參數:html
解析tornado的post參數:python
get,post解析參數均可:web
原始的post參數:json
headers參數:self.request.headers,對象(字典對象)app
在項目中解析post參數時,發現如下問題:python2.7
request.body_arguments能夠獲取到form數據,但不能獲取到curl過來的json數據
request.body.decode('utf-8')能夠獲取到全部的數據,可是是原始數據。
若是原始數據是curl過來json數據:直接json.loads(***)便可。
若是原始數據是form數據:直接json.loads(*)將拋出異常,由於數據格式是: username=abc%E5%8C%97%E4%BA%AC&email=&website=abc&language=%E4%B8%AD%E5%9B%BDcurl
解決方法:tornado
post_data = self.request.body_arguments post_data = {x: post_data.get(x)[0].decode("utf-8") for x in post_data.keys()} if not post_data: post_data = self.request.body.decode('utf-8') post_data = simplejson.loads(post_data)
示例:oop
index.htmlpost
<!DOCTYPE html> <html> <head> <title>sign in your name</title> <link rel="icon" href="#" type="image/x-icon" /> <link rel="shortcut icon" href="#" type="image/x-icon" /> </head> <body> <h2>Please sing in.</h2> <form method="post" action="/user"> <p>Name:<br><input type="text" name="username"></p> <p>Email:<br><input type="text" name="email"></p> <p>Website:<br><input type="text" name="website"></p> <p>Language:<br><input type="text" name="language"></p> <input type="submit" value="ok,submit my information"> </form> </body> </html>
user.html
<!DOCTYPE html> <html> <head> <title>sign in your name</title> <link rel="icon" href="#" type="image/x-icon" /> <link rel="shortcut icon" href="#" type="image/x-icon" /> </head> <body> <h2>Your Information</h2> <p>Your name is {{username}}</p> <p>Your email is {{email}}</p> <p>Your website is {{website}}, it is very good. This website is make by {{language}}</p> </body> </html>
server.py
#!/usr/bin/env python #coding:utf-8 import json import tornado.web import tornado.httpserver import tornado.ioloop import os from tornado.options import define, options define("port", default=9000, help="server port", type=int) class IndexHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs): # 顯示index.html模板,可是此時並無向模板網頁傳遞任何數據,僅僅顯示罷了 self.render("index.html") class CreateUserHandler(tornado.web.RequestHandler): def post(self, *args, **kwargs): # json post_data = self.request.body_arguments post_data = {x: post_data.get(x)[0].decode("utf-8") for x in post_data.keys()} if not post_data: post_data = self.request.body.decode('utf-8') post_data = json.loads(post_data) # 不單單是要引用模板網頁user.html,還要向這個網頁傳遞一些數據, # 例如username = user_name,含義就是, # 在模板中,某個地方是用username來標示獲得的數據, # 而user_name是此方法中的一個變量,也就是對應一個數據, # 那麼模板中的username也就對應了user_name的數據, # 這是經過username = user_name完成的 self.render("user.html", **post_data) # user_name的數據是哪裏來的呢?就是在index.html頁面的表單中提交上來的。 # 注意觀察路徑的設置,r"/user", CreateUserHandler,也就是在form中的action = '/user', # 就是要將數據提交給UserHandler處理,而且是經過post方法。因此,在UserHandler類中,有post() # 方法來處理這個問題。 handlers = [ (r"/", IndexHandler), (r"/user", CreateUserHandler) ] # 獲取存放模板目錄template的路徑 template_path = os.path.join(os.path.dirname(__file__), "template") if __name__ == '__main__': options.parse_command_line() app = tornado.web.Application(handlers=handlers, template_path=template_path) http_server = tornado.httpserver.HTTPServer(app) http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start()
運行:
python2.7 plat.py --port=8000
訪問index.html:
http://127.0.0.1:8000/
填寫form表單, 提交數據,自動跳轉到user.html
使用curl提交post數據:
curl -H "Content-Type:application/json" -X POST --data '{"username": "北京1245", "website": "3222w", "email": "adsf@sdfa.com", "language": "方言"}' http://127.0.0.1:8000/user