gpt4 book ai didi

python - 是否可以 : Get the cookie ? 检索没有事件名称的响应? python-socketio(客户端)

转载 作者:太空宇宙 更新时间:2023-11-03 20:38:53 24 4
gpt4 key购买 nike

我使用 python socket.io 客户端,我想知道是否可以检索 session cookie,如果可以,如何获取?

另一个问题,是否可以在不通过 @sio.event@sio.on() 的情况下从 .emit 获得答案? 与 websockets 库一样:

websocket.send('Hi')
response = websocket.rcv()
print(response) -> "Hi :)"

如果没有,是否可以创建一个事件/on,从服务器检索不包含事件名称的消息?

例如,我从客户端发送: sio.emit("GetNbrPlayers") 但服务器回答我 [{"data": "5"}] 没有数据之前的事件(它也是使用socket.io的服务器/api),我想收到此消息,但不可能通过事件/打开,因为答案中没有事件名称。

提前谢谢您!

最佳答案

it is possible to retrieve the session cookie,

session cookie 是什么意思?如果您指的是 Flask session cookie 之类的东西,那么不,没有 session cookie。用户 session 存储在服务器中,而不是存储在 cookie 中。

如果您指的是包含分配给客户端的 session ID 的 sid cookie,您可以从客户端对象的 sid 属性获取此 id,例如sio.sid

is it possible to get the answer from an .emit without going through an @sio.event or @sio.on()?

是的,您可以使用 Socket.IO 协议(protocol)的“ack”功能来实现此目的。如果服务器是 Python 服务器,您只需从处理函数返回响应即可。例如:

@sio.event
def GetNbrPlayers():
return [{"data": "5"}]

在 Python 客户端中,您有两种方法来接收此信息。您可以使用回调函数:

def get_response(data):
print(data)

sio.emit("GetNbrPlayers", callback=get_response)

或者您可以使用 call() 而不是 emit() 将发出和回调处理合并到单个函数调用中:

data = sio.call("GetNbrPlayers")

关于python - 是否可以 : Get the cookie ? 检索没有事件名称的响应? python-socketio(客户端),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56980093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com