如何通过Python龙卷风JSON发送一个报头的WebSocket的?(how to send js

2019-10-30 09:45发布

#!C:/Python27/python.exe -u 
import tornado
import tornado.websocket
import tornado.wsgi
import tornado.web
import tornado.httpserver
import tornado.tcpserver
import json

from py2neo import neo4j, cypher
graph_db = neo4j.GraphDatabaseService()
class request(tornado.web.RequestHandler):
    def  sr():
        self.set_header('Content-type','application/json')
class ChatWebSocket(tornado.websocket.WebSocketHandler):
    clients = []
    def open(self):
        ChatWebSocket.clients.append(self)
    def on_message(self, message):
         query = "START a=node:node_auto_index(uid='twitter') CREATE a-[:ch]-(t{con:'"+message+"'}) RETURN a"
     data, metadata = cypher.execute(graph_db, query)
         for client in ChatWebSocket.clients:
         print(client)
         t=json.loads(message)
         client.write_message('Content-type:application/json\n') 
         client.write_message(json.dumps({'a':t['b']}))
         print(t['b'])
    def on_close(self):
         ChatWebSocket.clients.remove(self)
    tornado_app = tornado.web.Application([
    (r'/websocket', ChatWebSocket),
    (r'.*', tornado.web.FallbackHandler)
    ])
 tornado_app.listen(5094)
tornado.ioloop.IOLoop.instance().start()

这里是我的代码示例我正在接受消息中JSON获取其实例b,并再次JSON发送到client.but客户端头处理正常字符串。

Answer 1:

的WebSockets不需要头。 只需发送您的JSON作为字符串。



文章来源: how to send json header via python tornado in a websocket?