此python 3代码按原样工作。
import tornado.web
import tornado.websocket
import tornado.httpserver
import tornado.ioloop
class WebSocketHandler(tornado.websocket.WebSocketHandler):
connections = set()
def open(self):
self.connections.add(self)
print ('new connection was opened')
pass
def on_message(self, message):
print ('from WebSocket: ', message)
messageTooSockets.put(message)
def on_close(self):
self.connections.remove(self)
print ('connection closed')
pass
class IndexPageHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html")
class webApplication(tornado.web.Application):
def __init__(self):
handlers = [
(r'/', IndexPageHandler),
(r'/websocket', WebSocketHandler),
(r'/(.*)', tornado.web.StaticFileHandler, {'path': './root'})
]
settings = {
'template_path': 'templates'
}
tornado.web.Application.__init__(self, handlers, **settings)
if __name__ == '__main__':
ws_app = webApplication()
server = tornado.httpserver.HTTPServer(ws_app)
server.listen(9090)
tornado.ioloop.IOLoop.instance().start()
提取处理程序对于请求或静态文件处理程序而言不是问题。但是,将websockets处理程序提取到子文件夹中的程序包时,龙卷风会抱怨。
ERROR:tornado.application:Uncaught exception
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/tornado/http1connection.py",
line 238, in _read_message
delegate.finish()
File "/usr/lib/python3/dist-packages/tornado/routing.py", line 251,
in finish
self.delegate.finish()
File "/usr/lib/python3/dist-packages/tornado/web.py", line 2097, in
finish
self.execute()
File "/usr/lib/python3/dist-packages/tornado/web.py", line 2117, in
execute
**self.handler_kwargs)
TypeError: __init__() takes 1 positional argument but 3 were given
导入tornado.websocket.WebSocketHandler处理程序时,关于龙卷风套接字有什么专长吗?
答案 0 :(得分:0)
messageTooSockets的格式有误...愚蠢的想法