如何在WebSocket客户端中添加更多头文件

tzcvj98z  于 2022-11-11  发布在  其他
关注(0)|答案(2)|浏览(264)

我尝试通过WebSocket连接(我使用python websocket客户端)发送会话ID(我在对http服务器进行身份验证后获得),我需要将其作为一个header参数传递,服务器将在此读取所有的报头并对其进行检查。
提出的问题是:我如何使用现有的客户机WebSocket实现之一向添加头,我发现它们都不能做到这一点,或者我首先使用了错误的方法进行身份验证?

-- Update --,下面是我使用的代码模板:

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

nxowjjhe1#

似乎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 RFC 6455文档:https://www.rfc-editor.org/rfc/rfc6455#section-4.3
GitHub来源:https://github.com/liris/websocket-client/blob/master/websocket.py

os8fio9y

os8fio9y2#

没有什么比阅读源代码更有趣的了:))
我用monkey修补了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'} 
                                )

这可以通过编辑该库的www.example.com源代码中的3行代码来完成websocket.py:
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作为头参数传递给WebSocket connect方法,如下所示:


## Line 732

self.sock.connect(self.url, header = self.header)

实际上,我尝试导入WebSocketApp类,但它没有工作,因为整个websocket.py模块是相互依赖的,我发现自己导入了很多东西才使它工作,在这种情况下,猴子修补更容易,更可靠。
这就是全部,享受使用您的补丁库与所有的头文件,你需要的。

相关问题