Python 如何获取Tornado web套接字请求的客户端IP?
如何获取Tornadowebsocket请求的客户端IP 我有一个用于传入连接()的RequestHandler对象。如何找到刚刚连接的客户端的IPPython 如何获取Tornado web套接字请求的客户端IP?,python,websocket,ip,tornado,Python,Websocket,Ip,Tornado,如何获取Tornadowebsocket请求的客户端IP 我有一个用于传入连接()的RequestHandler对象。如何找到刚刚连接的客户端的IP def open(self): ChatSocketHandler.clients.add(self) i2c.write_byte_data(0x70, 0x00, 0xa5) IR_on = True print "Connection initiated" Cha
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
实例将对象设置为Handler
的request
属性。您可以使用该属性获取远程连接的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
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':'oajpf4kp26krjymrgw='})
::1
当我从网络外部(即3g)连接时,它会显示调制解调器的ip,如何显示真正的原始ipplease@Ossama在这种情况下,路由器就像一个反向代理——客户端连接到您的公共IP(即您的路由器),路由器将请求转发到您的本地IP(192.168.x.x
例如)。如果路由器在传递给tornado
的头中未包含真实的客户端IP,则您可能无法获取真实的客户端IP。您可以尝试使用,并查看路由器是否包含X-real-IP
/X-Forwarded-for
。如何获取此信息?我的头如下,HTTPServerRequest(protocol='http',host='192.168.0.6:8888',method='GET',uri='/ws',version='http/1.1',remote_ip='192.168.0.3',headers={'Origin':''Upgrade':'websocket','Sec websocket Extensions':'x-webkit-deflate-frame','Sec websocket version':'13','Connection':'Upgrade','Sec websocket Key':'klb25dphtcvrri6uxqxew=','User-Agent':'Mozilla/5.0(iPhone;CPU iPhone OS 8_0_2,如Mac OS X)AppleWebKit/600.1.4(KHTML,如Gecko)版本/8.0 Mobile/12A405 Safari/600.1.4,“主机”:“192.168.0.6:8888”,“Pragma”:“无缓存”,“缓存控制”:“无缓存”})