如何获取Tornadowebsocket请求的客户端IP?
我有一个用于传入连接()的RequestHandler对象。如何查找刚刚连接的客户端的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
对象设置为Handler
的request
属性。您可以使用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