From 66cbc50124205727e42a569b2b409543e5f3a99f Mon Sep 17 00:00:00 2001 From: Maik Hoepfel Date: Fri, 28 Apr 2017 11:04:42 +0200 Subject: [PATCH] Python 2 fix for host address This is a copy of https://github.com/django/daphne/pull/91/commits/57051a48cd485c2dbb4a4c09d8c47f294ba75f06 for the Websocket protocol. In Python 2, Twisted returns a byte string for the host address, while the spec requires a unicode string. A simple cast gives us consistency. --- daphne/ws_protocol.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/daphne/ws_protocol.py b/daphne/ws_protocol.py index 5e26737..6b630a2 100755 --- a/daphne/ws_protocol.py +++ b/daphne/ws_protocol.py @@ -50,9 +50,11 @@ class WebSocketProtocol(WebSocketServerProtocol): # Tell main factory about it self.main_factory.reply_protocols[self.reply_channel] = self # Get client address if possible - if hasattr(self.transport.getPeer(), "host") and hasattr(self.transport.getPeer(), "port"): - self.client_addr = [self.transport.getPeer().host, self.transport.getPeer().port] - self.server_addr = [self.transport.getHost().host, self.transport.getHost().port] + peer = self.transport.getPeer() + host = self.transport.getHost() + if hasattr(peer, "host") and hasattr(peer, "port"): + self.client_addr = [six.text_type(peer.host), peer.port] + self.server_addr = [six.text_type(host.host), host.port] else: self.client_addr = None self.server_addr = None