如何在websocket python客户端中添加更多标头

时间:2013-01-22 09:10:07

标签: python websocket tornado

我正在尝试通过websocket连接发送会话ID(我在对http服务器进行身份验证后得到它)(我正在使用python websocket客户端),我需要将其作为标头传递参数,服务器(Tornado Websocket服务器)将读取所有标头并检查它们。

问题是:如何使用现有的客户端python Websocket实现之一添加标头,我发现它们都不能做到这一点,或者我是否首先采用了错误的方法进行身份验证?

- 更新 - ,在我使用的代码模板下面:

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()

2 个答案:

答案 0 :(得分:8)

没有什么比阅读源代码更有趣了:))

我修补了Websocket客户端库的源代码,使其能够在初始化程序中接收标头作为普通参数,如下所示:

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'} 
                                )

这可以通过编辑库的websocket.py源代码中的3行来完成:

1-添加标题参数:

   ## 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-然后,将self.header作为header参数传递给websocket connect方法,如下所示:

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

实际上我试图导入WebSocketApp类,但它不起作用,因为整个websocket.py模块是相互依赖的,我发现自己导入了很多东西以使其工作,猴子修补更容易,更实用在这种情况下。

就是这样,尽情使用带有所需标题的修补库。

答案 1 :(得分:4)

似乎websocket-client已更新为包含websocket标题,因为这个问题已被提出。现在,您只需将标题参数列表作为字符串传递:

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]
                            )

如果您对有效标头的完整列表感兴趣,请参阅websocket RFC6455文档:http://tools.ietf.org/html/rfc6455#section-4.3

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