如何将Python Websocket客户端连接到我的弹簧端点?

发布于 2025-01-19 05:33:50 字数 1313 浏览 2 评论 0原文

我是Python的新手,我正在尝试连接到目前在春季运行的服务。

在服务器端,我通过扩展DefaulthandShakeHandler并覆盖确定使用者方法,在该方法中使用自定义的握手处理程序,然后在此提取用户信息。但是,当请求进来时,没有什么可提取的。

我想知道的是,如何通过请求发送此信息?

这是我的基本Python客户端。

import  websocket
import stomper

headers = {}
#### have also tried with headers = Authorization : Bearer + token ####

uri = "ws://localhost:[port]/ws"

def on_msg(ws,msg):
    print(msg)

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

def on_closed(ws):
    print(#Closed#)

def on_open(ws):
    ws.send("CONNECT\naccept-version:1.0,1.1,2.0\n\nx00n")
    sub = stomper.subscribe("/user/topic/my-end-point", "username", ack="auto")
    ws.send(sub)

ws = websocket.WebSocketApp(uri, header=headers, on_message=on_msg, on_error=on_error, on_close=on_closed)
ws.on_open = on_open
ws.run.forever()

当我运行此功能时,我会得到以下内容:

\--- request header ---
GET /ws HTTP/1.1
Upgrade: websocket
Host: localhost:\[port\]
Origin: http://localhost\[port\]
Sec-WebSocket-Key: key
Sec-WebSocket-version: 13
Connection: Upgrade

\--- response header ---
HTTP/1.1 101 Switching Protocols
Date: date
Connection: Upgrade
Sec-WebSocket-Accept: key
Server: server-info
Upgrade: WebSocket

send: b'\\x81...
CONNECTED
version: 1.1
session: session_id
server: server-info

然后在60秒后止住。

接下来我可以尝试什么?

I'm new to Python and I'm trying to connect to a service that I currently have up running in Spring.

On the server-side I have a custom Handshake handler in place by extending DefaultHandshakeHandler and overriding the determineUser method, where I then extract the user information. However, when the request comes in, there is nothing to extract.

What I would like to know is, how do I send this information over with the request?

Here is my basic Python client.

import  websocket
import stomper

headers = {}
#### have also tried with headers = Authorization : Bearer + token ####

uri = "ws://localhost:[port]/ws"

def on_msg(ws,msg):
    print(msg)

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

def on_closed(ws):
    print(#Closed#)

def on_open(ws):
    ws.send("CONNECT\naccept-version:1.0,1.1,2.0\n\nx00n")
    sub = stomper.subscribe("/user/topic/my-end-point", "username", ack="auto")
    ws.send(sub)

ws = websocket.WebSocketApp(uri, header=headers, on_message=on_msg, on_error=on_error, on_close=on_closed)
ws.on_open = on_open
ws.run.forever()

When I run this, I get the following:

\--- request header ---
GET /ws HTTP/1.1
Upgrade: websocket
Host: localhost:\[port\]
Origin: http://localhost\[port\]
Sec-WebSocket-Key: key
Sec-WebSocket-version: 13
Connection: Upgrade

\--- response header ---
HTTP/1.1 101 Switching Protocols
Date: date
Connection: Upgrade
Sec-WebSocket-Accept: key
Server: server-info
Upgrade: WebSocket

send: b'\\x81...
CONNECTED
version: 1.1
session: session_id
server: server-info

It then times out after 60 seconds.

What can I try next?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画中仙 2025-01-26 05:33:50

我也许应该更新这个。

我在 url 字符串中缺少身份验证令牌参数。

现在它按预期工作了。

I should probably update this.

I was missing the authentication token parameter in the url string.

It works as expected now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文