简体   繁体   中英

Get IP address on Django-channels - on receive handler

We get the IP address of web socket client as message.content["client"] in the connect handler. but this code fails in receive handler. Is there any way we can get the IP address of client in receive handler ?

Edit:

In 'dphane/ws_protocol.py', in class WebSocketProtocol I see that in onconnect , the code is

        self.request_info = {
            "path": self.unquote(self.path),
            "headers": self.clean_headers,
            "query_string": self._raw_query_string,  # Passed by HTTP protocol
            "client": self.client_addr,
            "server": self.server_addr,
            "reply_channel": self.reply_channel,
            "order": 0,
        }

        ...
        ...

        self.channel_layer.send("websocket.connect", self.request_info)

where as in onmessage , the code is

        if isBinary:
            self.channel_layer.send("websocket.receive", {
                "reply_channel": self.reply_channel,
                "path": self.unquote(self.path),
                "order": self.packets_received,
                "bytes": payload,
            })
        else:
            self.channel_layer.send("websocket.receive", {
                "reply_channel": self.reply_channel,
                "path": self.unquote(self.path),
                "order": self.packets_received,
                "text": payload.decode("utf8"),
            })

We are not getting the client ip because Dphane is not giving that information in message handler.

Is there any way other than inheriting class WebSocketProtocol & overriding onmessage

您可以使用request.META ['REMOTE_ADDR']进行修复

Not sure if that is still a problem, but you should look into getting the message.content["client"] into the session. So when you get your message you can retrieve the IP in the session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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