From eab9009093ce706a2e4443844785416e36ecf02f Mon Sep 17 00:00:00 2001 From: Vukasin Toroman Date: Wed, 16 May 2012 13:04:45 +0200 Subject: [PATCH 1/3] support for additional headers add the possibility to add headers to the HTTP request. this is useful when sending an authentication header along with the request --- ws4py/client/__init__.py | 7 ++++--- ws4py/client/tornadoclient.py | 2 ++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/ws4py/client/__init__.py b/ws4py/client/__init__.py index aa4c8f2..6f5daf9 100644 --- a/ws4py/client/__init__.py +++ b/ws4py/client/__init__.py @@ -15,13 +15,14 @@ __all__ = ['WebSocketBaseClient'] class WebSocketBaseClient(WebSocket): - def __init__(self, url, protocols, extensions): + def __init__(self, url, protocols, extensions, custom_headers=[]): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) WebSocket.__init__(self, sock, protocols=protocols, extensions=extensions) self.stream.always_mask = True self.stream.expect_masking = False self.key = b64encode(os.urandom(16)) self.url = url + self.custom_headers = custom_headers def close(self, code=1000, reason=''): if not self.client_terminated: @@ -77,8 +78,8 @@ def handshake_headers(self): host = parts.netloc if ':' in host: host, port = parts.netloc.split(':') - - headers = [ + + headers = self.custom_headers + [ ('Host', host), ('Connection', 'Upgrade'), ('Upgrade', 'websocket'), diff --git a/ws4py/client/tornadoclient.py b/ws4py/client/tornadoclient.py index d527be6..17b771d 100644 --- a/ws4py/client/tornadoclient.py +++ b/ws4py/client/tornadoclient.py @@ -12,6 +12,8 @@ class TornadoWebSocketClient(WebSocketBaseClient): def __init__(self, url, protocols=None, extensions=None, io_loop=None): WebSocketBaseClient.__init__(self, url, protocols, extensions) + def __init__(self, url, protocols=None, extensions=None, io_loop=None,custom_headers=[]): + WebSocketBaseClient.__init__(self, url, protocols, extensions, custom_headers) parts = urlsplit(self.url) if parts.scheme == "wss": self.sock = ssl.wrap_socket(self.sock, From 933cfc94a48b51e7b6efcb98668c38e4273d3633 Mon Sep 17 00:00:00 2001 From: Vukasin Toroman Date: Wed, 16 May 2012 13:05:52 +0200 Subject: [PATCH 2/3] cleanup --- ws4py/client/tornadoclient.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/ws4py/client/tornadoclient.py b/ws4py/client/tornadoclient.py index 17b771d..2b7a46e 100644 --- a/ws4py/client/tornadoclient.py +++ b/ws4py/client/tornadoclient.py @@ -10,8 +10,6 @@ __all__ = ['TornadoWebSocketClient'] class TornadoWebSocketClient(WebSocketBaseClient): - def __init__(self, url, protocols=None, extensions=None, io_loop=None): - WebSocketBaseClient.__init__(self, url, protocols, extensions) def __init__(self, url, protocols=None, extensions=None, io_loop=None,custom_headers=[]): WebSocketBaseClient.__init__(self, url, protocols, extensions, custom_headers) parts = urlsplit(self.url) From 7b83d6b95c2d0a84d9357d12c879d4b9919bcf93 Mon Sep 17 00:00:00 2001 From: Vukasin Toroman Date: Wed, 16 May 2012 13:06:17 +0200 Subject: [PATCH 3/3] default port for wss set default port for was to 443 instead of 80 --- ws4py/client/tornadoclient.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ws4py/client/tornadoclient.py b/ws4py/client/tornadoclient.py index 2b7a46e..22da56e 100644 --- a/ws4py/client/tornadoclient.py +++ b/ws4py/client/tornadoclient.py @@ -24,7 +24,7 @@ def __init__(self, url, protocols=None, extensions=None, io_loop=None,custom_hea def connect(self): parts = urlsplit(self.url) - host, port = parts.netloc, 80 + host, port = parts.netloc, 443 if parts.scheme == "wss" else 80 if ':' in host: host, port = parts.netloc.split(':') self.io.set_close_callback(self.__connection_refused)