websockets"方案https无效",但我实际上是尝试使用https

时间:2017-10-29 06:15:09

标签: javascript python websocket

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

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

错误:

  

方案https无效

     

关闭

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的源代码。在/Python/2.7/site-packages/websocket/_url.py上找到websocket-client的代码并修改了以下代码:

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]无法分配请求的地址

     

关闭

1 个答案:

答案 0 :(得分:0)

我也遇到了同样的情况,并发布了一个问题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