title: Connecting Python Client to Socket.io with custom socket.io events
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>My socket.io server defines custom events such as <code>.on('connection')</code>. How can I emit/receive such custom events in python?</p>
  
  <p>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 = '<a href="http://socket-url-server.com" rel="nofollow">http://socket-url-server.com</a>'</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>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?</p>
  
  <p>I also tried using <a href="https://pypi.python.org/pypi/socketIO-client" rel="nofollow">https://pypi.python.org/pypi/socketIO-client</a>, but its documentation is very poor</p>
  
  <p>I wrote the following script for it below but I get <code>No handlers could be found for logger "socketIO_client"</code> and it just hangs forever.</p>
  
  <pre><code>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)
  </code></pre>