I have a socket.io server (in node.js) which I'm trying to connect a python client to, mainly to provide a command line interface to my socket server.
I'm using Python's web sockets, however, I've realised that this only supports four "events": on open, on close, on error and on message.
My socket.io server defines custom events such as .on('connection'). How can I emit/receive such custom events in python?
Here is my script so far, which just starts up and closes, and so it does not work. import web socket, requests, thread, time host = 'http://socket-url-server.com'
def on_open(ws):
def run(*args):
print 'did connect'
for i in range(3):
time.sleep(1)
result = ws.recv()
print 'received:'
print result
time.sleep(1)
ws.close()
print "thread terminating..."
thread.start_new_thread(run, ())
def on_message(ws, message):
print message
def on_error(ws, error):
print error
def on_close(ws):
print "### closed ###"
print('Connecting to %s' % host)
socket = host.replace('http://', 'ws://')
print socket
websocket.enableTrace(True)
_ws = websocket.WebSocketApp(socket,
on_message = on_message,
on_error = on_error,
on_close = on_close)
_ws.on_open = on_open
_ws.run_forever()
Which library / method should I use otherwise? If I do a handshake and grab the socket.io server key myself, how can I emit/receive events after that?
I also tried using https://pypi.python.org/pypi/socketIO-client, but its documentation is very poor
I wrote the following script for it below but I get No handlers could be found for logger "socketIO_client" and it just hangs forever.
from socketIO_client import SocketIO, BaseNamespace
class Namespace(BaseNamespace):
def on_connect(self):
print '[Connected]'
socketIO = SocketIO('http://socket-server-url.com', 80, Namespace)
socketIO.wait(seconds=1)