繁体   English   中英

如何获取Tornado Web套接字请求的客户端IP?

[英]How do I get the client IP of a Tornado web socket request?

如何获取Tornado websocket请求的客户端IP?

我有一个RequestHandler对象用于传入的connection()。 如何找到刚刚连接的客户端的IP?

def open(self):
        ChatSocketHandler.clients.add(self)
        i2c.write_byte_data(0x70, 0x00, 0xa5)
        IR_on = True
        print "Connection initiated"
        ChatSocketHandler.send_updates("IR on")

与普通的RequestHandler实例一样, WebsocketHandler实例将HTTPServerRequest对象设置为Handlerrequest属性。 您可以使用HTTPServerRequest.remote_ip属性获取远程连接的IP。 例如:

class EchoWebSocket(websocket.WebSocketHandler):
    def initialize(self):
        self._closed = False

    def open(self):
        print(type(self.request))
        print(self.request)
        print(self.request.remote_ip)

收到请求时的输出:

<class 'tornado.httputil.HTTPServerRequest'>
HTTPServerRequest(protocol='http', host='localhost:8888', method='GET', uri='/ws', version='HTTP/1.1', remote_ip='::1', headers={'Connection': 'Upgrade', 'Upgrade': 'websocket', 'Accept-Encoding': 'gzip', 'Sec-Websocket-Version': '13', 'Host': 'localhost:8888', 'Sec-Websocket-Key': 'oAJpF4f4kp26b2KRjYmRGw=='})
::1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM