我正在为Python使用名为“ websocket”的模块,并且需要使用不同的Origin标头连接到服务器。我尝试在WebSocketApp中使用:header = {"Origin: *"}
,但它有2个Origin标头。
--- 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个原始标头,但我不知道如何解决。 完整代码:
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()
答案 0 :(得分:1)
我找到了解决方案。我需要将原点写入ws.run_forever(origin=originVar)
。