gpt4 book ai didi

json - 如何从 Tornado 返回列表?

转载 作者:行者123 更新时间:2023-12-04 19:57:38 25 4
gpt4 key购买 nike

我试图让 Tornado 返回一个列表。

@tornado.gen.coroutine
@tornado.web.asynchronous
def get(self, param):

...
someList = [bla,bla,bla]

self.finish(someList)

但无论何时我都会得到
HTTPRequest(protocol='http', host='tornado', method='GET', uri='/orders/submitted', version='HTTP/1.0', remote_ip='127.0.0.1', headers={'Accept-Language': 'en-US,en;q=0.8', 'Accept-Encoding': 'gzip,deflate,sdch', 'Host': 'tornado', 'Accept': 'application/json, text/plain, */*', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36', 'Connection': 'close', 'Referer': 'admin/', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache', 'Cookie': 'login="dHJ1ZQ==|1392074972|f4942e30293413717b380d1f362dd549548c404d"'})
Traceback (most recent call last):
File "/Users/ruahman/swipe-tech-apps/rpm-tornado/env/lib/python2.7/site-packages/tornado/web.py", line 1221, in _when_complete
if result.result() is not None:
File "/Users/ruahman/swipe-tech-apps/rpm-tornado/env/lib/python2.7/site-packages/tornado/concurrent.py", line 129, in result
raise_exc_info(self.__exc_info)
File "/Users/ruahman/swipe-tech-apps/rpm-tornado/env/lib/python2.7/site-packages/tornado/stack_context.py", line 302, in wrapped
ret = fn(*args, **kwargs)
File "/Users/ruahman/swipe-tech-apps/rpm-tornado/env/lib/python2.7/site-packages/tornado/gen.py", line 574, in inner
self.set_result(key, result)
File "/Users/ruahman/swipe-tech-apps/rpm-tornado/env/lib/python2.7/site-packages/tornado/gen.py", line 500, in set_result
self.run()
File "/Users/ruahman/swipe-tech-apps/rpm-tornado/env/lib/python2.7/site-packages/tornado/gen.py", line 531, in run
yielded = self.gen.send(next)
File "/Users/ruahman/swipe-tech-apps/rpm-tornado/fabfile/server/request_handlers/orders.py", line 26, in get
self.finish((u"hi",))
File "/Users/ruahman/swipe-tech-apps/rpm-tornado/env/lib/python2.7/site-packages/tornado/web.py", line 810, in finish
self.write(chunk)
File "/Users/ruahman/swipe-tech-apps/rpm-tornado/env/lib/python2.7/site-packages/tornado/web.py", line 610, in write
chunk = utf8(chunk)
File "/Users/ruahman/swipe-tech-apps/rpm-tornado/env/lib/python2.7/site-packages/tornado/escape.py", line 203, in utf8
"Expected bytes, unicode, or None; got %r" % type(value)
TypeError: Expected bytes, unicode, or None; got <type 'tuple'>
ERROR:tornado.access:500 GET /orders/submitted (127.0.0.1) 8424.93ms

当我返回字典时,我没有这个问题。它只在我返回数组时发生。
是的,我可以将列表嵌入到一个对象中,但我更喜欢结果是一个数组。

最佳答案

由于复杂的原因,在Web上使用数组作为JSON对象的顶层结构存在安全风险。因此,Tornado 不会自动将列表转换为 JSON,只会将 dicts 转换为 JSON。我建议您将列表嵌入到 dict 中:

self.finish({'someList': someList})

如果你坚持直接返回列表,你可以自己进行JSON编码:
self.finish(json.dumps(someList))

以下是与在 Web API 中使用顶级 JSON 数组相关的安全漏洞的解释:
  • Anatomy of a Subtle JSON Vulnerability
  • JSON Hijacking
  • 关于json - 如何从 Tornado 返回列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21691133/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com