简体   繁体   English

Websocket 客户端不适用于使用 python 代码的 wss

[英]Websocket client not working for wss using python code

I am using python client to connect to a server hosted in azure.我正在使用 python 客户端连接到托管在 azure 中的服务器。 I am using websockets to connect.我正在使用 websockets 进行连接。 I am passing the auth header in the python client code.我在 python 客户端代码中传递了身份验证 header。 The code doesn't work when we give wss:// url for connection.当我们给wss:// url 进行连接时,代码不起作用。 The error says:错误说:

Handshake status 403 Forbidden

However same thing is working when tested using postman.然而,当使用 postman 进行测试时,同样的事情是有效的。

Here is the code snippet used to connect to the websocket server:下面是用于连接 websocket 服务器的代码片段:

def on_message(ws, message):
    print(message)

def on_error(ws, error):
    print(error)

def on_close(ws, close_status_code, close_msg):
    print("### closed ###")

def on_open(ws):
    print("Opened connection")

auth_str = "Authorization:Basic abgvrgfrbnfrfurfr"

if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("wss://uri.com/websocket",
                             header=[auth_str],
                              on_open=on_open,
                              on_message=on_message,
                              on_error=on_error,
                              on_close=on_close
                              )
    ws.run_forever(dispatcher=rel) 
    rel.signal(2, rel.abort)  
    rel.dispatch()

The request headers of the requests in postman and python client have no difference. postman和python客户端中请求的请求头没有区别。 Don't know why the python client says 403 forbidden .不知道为什么 python 客户端说403 forbidden disabled 。

It is only working under one condition: Setting HTTPSOnly as false in app service and passing ws:// uri instead of wss://它仅在一种情况下工作:在应用服务中将 HTTPSOnly 设置为 false 并传递 ws:// uri 而不是 wss://

As I see, your header parameter definition is wrong.如我所见,您的 header 参数定义错误。 It needs to be like它需要像

ws = websocket.WebSocketApp("wss://uri.com/websocket",
                             header={ 'Authorization': 'Basic abgvrgfrbnfrfurfr' },
                              on_open=on_open,
                              on_message=on_message,
                              on_error=on_error,
                              on_close=on_close
                              )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM