gpt4 book ai didi

python - 打开 websocket 时 Tornado 403 GET 警告

转载 作者:IT老高 更新时间:2023-10-28 20:39:04 27 4
gpt4 key购买 nike

我发现这个 python 脚本应该允许我打开一个 WebSocket。但是,当我尝试打开实际的 WebSocket(使用旧 WebSocket终端 Chrome 插件)。消息“连接打开”、“连接关闭”和“收到消息”永远不会在终端窗口中打印。

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.websocket

class MyHandler(tornado.websocket.WebSocketHandler):
def open(self):
print "connection opened"
self.write_message("connection opened")

def on_close(self):
print "connection closed"

def on_message(self,message):
print "Message received: {}".format(message)
self.write_message("message received")

if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(handlers=[(r"/",MyHandler)])
server = tornado.httpserver.HTTPServer(app)
server.listen(8888)
tornado.ioloop.IOLoop.instance().start()

最佳答案

请补充

def check_origin(self, origin):
return True

像这样在 MyHandler 类中

class MyHandler(tornado.websocket.WebSocketHandler):

def check_origin(self, origin):
return True

def open(self):
print "connection opened"
self.write_message("connection opened")

def on_close(self):
print "connection closed"

def on_message(self,message):
print "Message received: {}".format(message)
self.write_message("message received")

来自文档:

By default, [check_origin] rejects all requests with an origin on a host other than this one.

This is a security protection against cross site scripting attacks on browsers, since WebSockets are allowed to bypass the usual same-origin policies and don’t use CORS headers.

再说一遍:

This is an important security measure; don’t disable it without understanding the security implications. In particular, if your authentication is cookie-based, you must either restrict the origins allowed by check_origin() or implement your own XSRF-like protection for websocket connections. See these articles for more.

Link .

关于python - 打开 websocket 时 Tornado 403 GET 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24851207/

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