websockets“方案https无效",但我实际上是在尝试使用https [英] websockets "scheme https is invalid", but i'm actually trying to use https
问题描述
我正在使用:https://pypi.python.org/pypi/websocket-客户
尝试将 python 2.7 websockets 客户端连接到实际使用 https 而不是 ws 或 wss 的 URL,但我收到以下错误.无论如何,我可以解决这个问题吗?请注意,我没有创建这个 websockets 服务器,所以我无法控制它是否符合 ws 或 wss 标准.
Trying to connect a python 2.7 websockets client to a URL which actually uses https and not ws or wss, but i'm getting the following error. Anyway I can get around this? Please note that I did not create this websockets server so I have no control over making it conform to ws or wss standards.
错误:
方案 https 无效
scheme https is invalid
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 代码有效.
I know that I can connect to this websocket URL because the following javascript code works.
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 的源代码.去/Python/2.7/site-packages/websocket/_url.py的websocket-client的代码,修改如下代码:
I tried modifying the source code for websocket-client. Went to websocket-client's code at /Python/2.7/site-packages/websocket/_url.py and modified the following code:
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)
但是现在当我运行它时,我得到另一个错误
But now when I run it, I get another error
[Errno 49] 无法分配请求的地址
[Errno 49] Can't assign requested address
推荐答案
我也遇到了同样的情况,发了一个问题这里
I also got into the same situation and posted a question here
没有任何答案,我尝试按照您的步骤编辑 _url.py,它对我有用.这是我编辑 _url.py 的尝试:
Without any answer, I tried to follow your step by editing _url.py and it works for me. Here is my attempt in editing the _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
这篇关于websockets“方案https无效",但我实际上是在尝试使用https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!