Python Tornado request.body
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16451660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tornado request.body
提问by Joel James
My Tornado application accepts POST data through http body request
我的 Tornado 应用程序通过 http 正文请求接受 POST 数据
In my handler I am able to get the request
在我的处理程序中,我能够收到请求
def post(self):
data = self.request.body
The data I am getting is in the from of str(dictionary)
我得到的数据来自 str(dictionary)
Is there a way to receive this data in the form of a Python dictionary?
有没有办法以 Python 字典的形式接收这些数据?
I don't want to use evalon the server side to convert this string to a Python dictionary.
我不想eval在服务器端使用将此字符串转换为 Python 字典。
回答by Eloims
You are receiving a JSON string. Decode it with the JSON module
您收到一个 JSON 字符串。使用 JSON 模块解码
import json
def post(self):
data = json.loads(self.request.body)
For more info: http://docs.python.org/2/library/json.html
回答by Mitch
I think I had a similar issue when I was parsing requests in Tornado. Try using the urllib.unquote_plus module:
我想我在 Tornado 中解析请求时遇到了类似的问题。尝试使用 urllib.unquote_plus 模块:
import urllib
try:
import simplejson as json
except ImportError:
import json
data = json.loads(urllib.unquote_plus(self.request.body))
My code had to be prepared for both different formats of request, so I did something like:
我的代码必须为两种不同格式的请求做好准备,所以我做了类似的事情:
try:
json.loads(self.request.body)
except:
json.loads(urllib.unquote_plus(self.request.body))
回答by Farray
As an alternative to Eloim's answer, Tornado provides tornado.escapefor "Escaping/unescaping HTML, JSON, URLs, and others". Using it should give you exactly what you want:
作为 Eloim 答案的替代方案,Tornado为“转义/取消转义 HTML、JSON、URL 和其他”提供了tornado.escape。使用它应该给你你想要的东西:
data = tornado.escape.json_decode(self.request.body)
回答by Max Marchuk
Best way for me to parse body in tornado built-in httputil
Good work with multi input (like checkbox, tables, etc.).
If submit elements have same name in dictionary returning list of values.
我在 Tornado 内置解析身体的最佳方式httputil
多输入(如复选框、表格等)的良好工作。如果提交元素在返回值列表的字典中具有相同的名称。
Working sample:
工作样本:
import tornado.httputil
def post(self):
file_dic = {}
arg_dic = {}
tornado.httputil.parse_body_arguments('application/x-www-form-urlencoded', self.request.body, arg_dic, file_dic)
print(arg_dic, file_dic) # or other code`
回答by ADL
If you are using WebApp2, it uses its own json extras. (Decode) http://webapp2.readthedocs.io/en/latest/_modules/webapp2_extras/json.html
如果您使用的是 WebApp2,它会使用它自己的 json extras。(解码) http://webapp2.readthedocs.io/en/latest/_modules/webapp2_extras/json.html
data = json.decode(self.request.body)
v = data.get(key)
self.response.write(v)
For example my post key is 'postvalue'
例如,我的帖子键是“postvalue”
data = json.decode(self.request.body)
v = data.get('postvalue')
self.response.write(v)
回答by suger
how about
怎么样
bind_args = dict((k,v[-1] ) for k, v in self.request.arguments.items())

