解析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