How to add more headers to python client for websocket

I am trying to send a session id (I received it after authentication against the http server) through a connection to websocket (I use the python websocket client), I need to pass it as a header the parameter where the server (Tornado Websocket server) will read all the headers and check them.

The question is: how can I add headers to using one of the existing client python web application implementations, I believe that none of them can do this, or am I not mistaken for authentication first of all?

- Update . Below is the template of the code used:

def on_message(ws, message): print 'message received ..' print message def on_error(ws, error): print 'error happened .. ' print error def on_close(ws): print "### closed ###" def on_open(ws): print 'Opening Websocket connection to the server ... ' ## This session_key I got, need to be passed over websocket header isntad of ws.send. ws.send(session_key) if __name__ == "__main__": websocket.enableTrace(True) ws = websocket.WebSocketApp("ws://localhost:9999/track", on_open = on_open, on_message = on_message, on_error = on_error, on_close = on_close, ) ws.on_open = on_open ws.run_forever() 
+8
python tornado websocket
source share
2 answers

It seems that the websocket client has been updated to include websocket headers as this question has been asked. Now you can simply pass the list of header options as strings:

 custom_protocol = "your_protocol_here" protocol_str = "Sec-WebSocket-Protocol: " + custom_protocol ws = websocket.WebSocketApp("ws://localhost:9999/track", on_open = on_open, on_message = on_message, on_error = on_error, on_close = on_close, header = [protocol_str] ) 

If you are interested in a complete list of valid headers, see websocket RFC6455: http://tools.ietf.org/html/rfc6455#section-4.3

GitHub Source: https://github.com/liris/websocket-client/blob/master/websocket.py

+4
source share

Nothing more fun than reading the source code :))

I monkey fixed the source code of the Websocket client library so that it can receive the header as a normal parameter in the initializer, for example:

 ws = websocket.WebSocketApp("ws://localhost:9999/track", on_open = on_open, on_message = on_message, on_error = on_error, on_close = on_close, header = {'head1:value1','head2:value2'} ) 

This can be done by editing 3 lines in the source code of the websocket.py library:

1- Add a header parameter:

  ## Line 877 class WebSocketApp(object): """ Higher level of APIs are provided. The interface is like JavaScript WebSocket object. """ def __init__(self, url, on_open = None, on_message = None, on_error = None, on_close = None, keep_running = True, get_mask_key = None, header = None): self.url = url self.on_open = on_open self.on_message = on_message self.on_error = on_error self.on_close = on_close self.keep_running = keep_running self.get_mask_key = get_mask_key self.sock = None self.header = header 

2- Then pass self.header to the websocket connection method as a header parameter, for example:

 ## Line 732 self.sock.connect(self.url, header = self.header) 

Actually, I tried to import the WebSocketApp class, but it did not work, since the whole websocket.py module is interdependent, that I found that I import a lot of things to make it work, fixing monkeys is easier and more reliable in this case.

For everything, enjoy using the revised library with all the necessary headers.

+7
source share

All Articles