【发布时间】:2014-03-14 12:47:14
【问题描述】:
在这种情况下,我使用的是 Tornado 网络
我只想使用一些简单的方法将 json 数据从 mongodb 发送到 javascript。
我只是在互联网上查看一些示例。我很困惑
最后我得到了结果,Python 2 网络允许你通过字符串发送消息
Python 3 必须是字节
实际上这个原始代码来自互联网,但由 python 2 编写,无法运行 python3
from tornado.web import RequestHandler # this Tornado standard
class JSONPHandler(RequestHandler):
CALLBACK = 'jsonp' # define callback argument name <== this Javascript send to python callback name, java script send msg look like ?jsonp=? check it
def finish(self, chunk=None):
assert not self._finished
if chunk: self.write(chunk)
# get client callback method
print(type(self.CALLBACK)) <==show string class
callbacka = self.get_argument(self.CALLBACK)
callback=bytes(callbacka+'(','utf-8') <== from this part to new
# format output with jsonp
self._write_buffer.insert(0,callback ) <== write some json head
self._write_buffer.append(bytes(')','utf-8')) <== all msg must be bytes
super(JSONPHandler, self).finish() <== must do finished step
# chunk must be None
【问题讨论】:
标签: javascript python json mongodb tornado