3

I would write a websocket client in python to connect to a server written with socket.io. My current code, taken from 1 is the following:

import websocket, httplib, sys, asyncore
def connect(server, port):

    print("connecting to: %s:%d" %(server, port))

    conn  = httplib.HTTPConnection(server + ":" + str(port))
    conn.request('POST','/socket.io/1/')
    resp  = conn.getresponse() 
    hskey = resp.read().split(':')[0]
    ws = websocket.WebSocket(
                'ws://'+server+':'+str(port)+'/socket.io/1/websocket/'+hskey,
                onopen   = _onopen,
                onmessage = _onmessage,
                onclose = _onclose)

    return ws

def _onopen():
    print("opened!")

def _onmessage(msg):
    print("msg: " + str(msg))

def _onclose():
    print("closed!")


if __name__ == '__main__':

    server = 'localhost'
    port = 8081

    ws = connect(server, port)

    try:
        asyncore.loop()
    except KeyboardInterrupt:
        ws.close()

My question is how do I connect to a specific namespace?

Thanks

3 Answers 3

2

You can use socketIO-client which is available on PyPI under the MIT license. It has support for different namespaces of a single socket.

from socketIO_client import SocketIO, BaseNamespace

class MainNamespace(BaseNamespace):

    def on_aaa(self, *args):
        print 'aaa', args

class ChatNamespace(BaseNamespace):

    def on_bbb(self, *args):
        print 'bbb', args

class NewsNamespace(BaseNamespace):

    def on_ccc(self, *args):
        print 'ccc', args

mainSocket = SocketIO('localhost', 8000, MainNamespace)
chatSocket = mainSocket.connect('/chat', ChatNamespace)
newsSocket = mainSocket.connect('/news', NewsNamespace)
mainSocket.wait()
Sign up to request clarification or add additional context in comments.

1 Comment

For those of you using this, I believe the format's change slightly recently. Read the docs if you're getting stuck. pypi.python.org/pypi/socketIO-client
1

You can change the namespace more easily.

from socketIO_client import SocketIO, BaseNamespace
socket = SocketIO('192.168.4.47', 7777)
chat = socket.define(BaseNamespace, '/openchat')
chat.emit('echo', 'hello openchat my name is Anderson')

I've solved this problem in https://pypi.python.org/pypi/socketIO-client

hope you save your time a lot

Comments

0

I'd suggest you use Wireshark to sniff a connection made with Socket.IO and that you send the correct packets through your websocket connection to fake being a Socket.IO client.. and then implement the packets and messaging protocol of the Socket.IO layer.

There is basic documentation of the packet types in here:

http://gevent-socketio.readthedocs.org/en/latest/packet.html#module-socketio.packet

Also, you can read the test suite that shows the wire-level protocol (that you will probably need to implement):

https://github.com/abourget/gevent-socketio/blob/master/tests/test_packet.py

A specific namespace is specified in the packet of different types as you will see in the docs.

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.