gpt4 book ai didi

javascript - websockets "scheme https is invalid",但我实际上正在尝试使用 https

转载 作者:行者123 更新时间:2023-11-28 04:02:15 27 4
gpt4 key购买 nike

我正在使用:https://pypi.python.org/pypi/websocket-client

尝试将 python 2.7 websockets 客户端连接到实际使用 https 而不是 ws 或 wss 的 URL,但出现以下错误。无论如何我可以解决这个问题吗?请注意,我没有创建此 websockets 服务器,因此我无法控制使其符合 ws 或 wss 标准。

错误:

scheme https is invalid

closed

Python代码如下:

import websocket
import thread
import time

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

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

def on_close(ws):
print("### closed ###")

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


if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("https://MYURL",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()

我知道我可以连接到此 websocket URL,因为以下 javascript 代码有效。

Javascript代码如下:

const io = require('socket.io-client');

var socket = io.connect('https://MYURL', { transports: ['websocket'] });
socket.on('connect', () => {
console.log('socket connected');
getMarket();
});

socket.on('disconnect', () => {
console.log('socket disconnected');
});

getMarket = () => {
socket.emit('getMarket', {});

socket.once('market', (market) => {
console.log("stuff", market)
});
}

我尝试修改 websocket-client 的源代码。转到websocket-client的代码/Python/2.7/site-packages/websocket/_url.py并修改以下代码:

is_secure = False
if scheme == "ws":
if not port:
port = 80
elif scheme == "wss":
is_secure = True
if not port:
port = 443
else:
pass
#raise ValueError("!scheme %s is invalid" % scheme)

但是现在当我运行它时,我收到另一个错误

[Errno 49] Can't assign requested address

closed

最佳答案

我也遇到了同样的情况并发布了一个问题here

没有任何答案,我尝试通过编辑 _url.py 按照您的步骤进行操作,它对我有用。这是我编辑 _url.py 的尝试:

def parse_url(url):
"""
parse url and the result is tuple of
(hostname, port, resource path and the flag of secure mode)

url: url string.
"""
if ":" not in url:
raise ValueError("url is invalid")

scheme, url = url.split(":", 1)

parsed = urlparse(url, scheme="ws")
if parsed.hostname:
hostname = parsed.hostname
else:
raise ValueError("hostname is invalid")
port = 0
if parsed.port:
port = parsed.port

is_secure = False
if scheme == "ws":
if not port:
port = 80
elif scheme == "wss":
is_secure = True
if not port:
port = 443
else:
if scheme == "https":
is_secure = True
if not port:
port = 443
else:
raise ValueError("scheme %s is invalid" % scheme)

if parsed.path:
resource = parsed.path
else:
resource = "/"

if parsed.query:
resource += "?" + parsed.query

return hostname, port, resource, is_secure

关于javascript - websockets "scheme https is invalid",但我实际上正在尝试使用 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46997384/

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