I'm using: https://pypi.python.org/pypi/websocket-client
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.
Error:
scheme https is invalid
closed
Python code is below:
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()
I know that I can connect to this websocket URL because the following javascript code works.
Javascript code is below:
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)
});
}
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] Can't assign requested address
closed