Python 如何从WebSocket处理程序获取Tornado请求的客户端IP?
上面的链接告诉我们如何为请求处理程序派生客户端IP。使用Websocket处理程序时会发生什么情况Python 如何从WebSocket处理程序获取Tornado请求的客户端IP?,python,websocket,tornado,Python,Websocket,Tornado,上面的链接告诉我们如何为请求处理程序派生客户端IP。使用Websocket处理程序时会发生什么情况 谢谢。类WebSocketHandler扩展了RequestHandler http://stackoverflow.com/questions/3110919/how-do-i-get-the-client-ip-of-a-tornado-request 因此,您可以通过以下方式获得ip: class WebSocketHandler(tornado.web.RequestHandler):
谢谢。类WebSocketHandler扩展了
RequestHandler
http://stackoverflow.com/questions/3110919/how-do-i-get-the-client-ip-of-a-tornado-request
因此,您可以通过以下方式获得ip:
class WebSocketHandler(tornado.web.RequestHandler):
如果使用nginx作为代理服务器,情况会稍微复杂一些,有两种解决方案: 选项1:使用self.request.remote\u ip
如果您坚持使用此方法,则需要同时配置nginx和tornado应用程序。
步骤1:在nginx服务器块中添加以下任一FLOWING行:
class SocketHandler(tornado.websocket.WebSocketHandler):
def open(self):
logging.info('Client IP:' + self.request.remote_ip)
或
步骤2:创建tornado httpserver时,添加xheader选项
proxy_set_header X-Real-Ip $remote_addr;
然后您可以使用self.request.remote_ip立即获取您的远程ip地址
选项2:从HttpRequest头获取X-Real-Ip以下代码将直接为您提供远程真实ip:
self.request.headers.get('X-Real-Ip','')
我不明白,您需要来自web socketHandler的远程Ip吗?我只需要一份通过websocket访问的用户的Ip记录
proxy_set_header X-Real-Ip $remote_addr;
server = httpserver.HTTPServer(application, xheaders=True)