gpt4 book ai didi

python - 将 Origin header 设置为 websocket-client

转载 作者:行者123 更新时间:2023-12-01 01:21:47 46 4
gpt4 key购买 nike

我正在使用名为“websocket”的 python 模块,并且我需要连接到具有不同 Origin header 的服务器。我尝试在 WebSocketApp 中使用: header = {"Origin: *"},但它会生成 2 个 Origin header 。

--- request header ---
GET /?name=...&room=...&color=...HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: ...
Origin: ...
Sec-WebSocket-Key: rIfcXahGvdgTJ7WDhe023w==
Sec-WebSocket-Version: 13
Origin: *

如您所见,它有 2 个原始 header ,我不知道如何修复它。完整代码:

from color import cprint
import websocket
import json as JSON

ws = websocket.WebSocket()

nick = input("Select name: ")
room = input("Select room: ")
color = input("Select color: ")

try:
import thread
except ImportError:
import _thread as thread
import time

def on_message(ws, message):
if JSON.loads(message)[0] == 1:
print(JSON.loads(message)[1], ": ", JSON.loads(message)[2])

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

def on_close(ws):
cprint("red", "### Disconnected from chat. ###")

def on_open(ws):
def run(*args):
for i in range(3):
time.sleep(1)
time.sleep(1)
ws.close()
print("thread terminating...")
thread.start_new_thread(run, ())


if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp('ws://...?name=%s&room=%s&color=%s' % (nick, room, color),
on_message = on_message,
on_error = on_error,
on_close = on_close,
header = {"Origin: *"})
ws.on_open = on_open
ws.run_forever()

最佳答案

我找到了解决方案。我需要将 origin 写入 ws.run_forever(origin=originVar)

关于python - 将 Origin header 设置为 websocket-client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53748146/

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