socketIO-client-nexus
Here is a socket.io client library for Python. You can use it to write test code for your socket.io server.
This is a forked version to implement the Socket.io 2.x changes. You can find the original here.
Installation
Install the package in an isolated environment.
VIRTUAL_ENV=$HOME/.virtualenvs/crosscompute virtualenv $VIRTUAL_ENV source $VIRTUAL_ENV/bin/activate pip install -U socketIO-client==0.8.0 # Install package pip install -U socketIO-client-nexus
Usage
Activate isolated environment.
VIRTUAL_ENV=$HOME/.virtualenvs/crosscompute source $VIRTUAL_ENV/bin/activate export NODE_PATH=$VIRTUAL_ENV/lib/node_modules export NPM_CONFIG_PREFIX=$VIRTUAL_ENV
Launch your socket.io server.
cd $(python -c "import os, socketIO_client;\ print(os.path.dirname(socketIO_client.__file__))") npm install -g http-proxy DEBUG=* node tests/serve.js # Start socket.io server in terminal one DEBUG=* node tests/proxy.js # Start proxy server in terminal two
For debugging information, run these commands first.
import logging logging.getLogger('socketIO-client-nexus').setLevel(logging.DEBUG) logging.basicConfig()
Emit.
from socketIO_client import SocketIO, LoggingNamespace with SocketIO('127.0.0.1', 8000, LoggingNamespace) as socketIO: socketIO.emit('aaa') socketIO.wait(seconds=1)
Emit with callback.
from socketIO_client import SocketIO, LoggingNamespace def on_bbb_response(*args): print('on_bbb_response', args) with SocketIO('127.0.0.1', 8000, LoggingNamespace) as socketIO: socketIO.emit('bbb', {'xxx': 'yyy'}, on_bbb_response) socketIO.wait_for_callbacks(seconds=1)
Define events.
from socketIO_client import SocketIO, LoggingNamespace def on_connect(): print('connect') def on_disconnect(): print('disconnect') def on_reconnect(): print('reconnect') def on_aaa_response(*args): print('on_aaa_response', args) socketIO = SocketIO('127.0.0.1', 8000, LoggingNamespace) socketIO.on('connect', on_connect) socketIO.on('disconnect', on_disconnect) socketIO.on('reconnect', on_reconnect) # Listen socketIO.on('aaa_response', on_aaa_response) socketIO.emit('aaa') socketIO.emit('aaa') socketIO.wait(seconds=1) # Stop listening socketIO.off('aaa_response') socketIO.emit('aaa') socketIO.wait(seconds=1) # Listen only once socketIO.once('aaa_response', on_aaa_response) socketIO.emit('aaa') # Activate aaa_response socketIO.emit('aaa') # Ignore socketIO.wait(seconds=1)
Define events in a namespace.
from socketIO_client import SocketIO, BaseNamespace class Namespace(BaseNamespace): def on_aaa_response(self, *args): print('on_aaa_response', args) self.emit('bbb') socketIO = SocketIO('127.0.0.1', 8000, Namespace) socketIO.emit('aaa') socketIO.wait(seconds=1)
Define standard events.
from socketIO_client import SocketIO, BaseNamespace class Namespace(BaseNamespace): def on_connect(self): print('[Connected]') def on_reconnect(self): print('[Reconnected]') def on_disconnect(self): print('[Disconnected]') socketIO = SocketIO('127.0.0.1', 8000, Namespace) socketIO.wait(seconds=1)
Define different namespaces on a single socket.
from socketIO_client import SocketIO, BaseNamespace class ChatNamespace(BaseNamespace): def __init__(self, io, path, id): self.id = id def on_aaa_response(self, *args): print(self.id + ': on_aaa_response', args) class NewsNamespace(BaseNamespace): def on_aaa_response(self, *args): print('on_aaa_response', args) socketIO = SocketIO('127.0.0.1', 8000) chat_namespace = socketIO.define(ChatNamespace, '/chat', 'myId') news_namespace = socketIO.define(NewsNamespace, '/news') chat_namespace.emit('aaa') news_namespace.emit('aaa') socketIO.wait(seconds=1)
Connect via SSL (invisibleroads#54).
from socketIO_client import SocketIO # Skip server certificate verification SocketIO('https://127.0.0.1', verify=False) # Verify the server certificate SocketIO('https://127.0.0.1', verify='server.crt') # Verify the server certificate and encrypt using client certificate socketIO = SocketIO('https://127.0.0.1', verify='server.crt', cert=( 'client.crt', 'client.key'))
Specify params, headers, cookies, proxies thanks to the requests library.
from socketIO_client import SocketIO from base64 import b64encode SocketIO('127.0.0.1', 8000, params={ 'q': 'qqq', }, headers={ 'Authorization': 'Bearer xyz', }, cookies={ 'a': 'aaa', }, proxies={ 'https': 'https://proxy.example.com:8080', })
Wait forever.
from socketIO_client import SocketIO socketIO = SocketIO('127.0.0.1', 8000) socketIO.wait()
Don't wait forever.
from requests.exceptions import ConnectionError from socketIO_client import SocketIO try: socket = SocketIO('127.0.0.1', 8000, wait_for_connection=False) socket.wait() except ConnectionError: print('The server is down. Try again later.')
License
This software is available under the MIT License.
Credits
- Guillermo Rauch wrote the socket.io specification.
- Hiroki Ohtani wrote websocket-client.
- Roderick Hodgson wrote a prototype for a Python client to a socket.io server.
- Alexandre Bourget wrote gevent-socketio, which is a socket.io server written in Python.
- Joe Palmer sponsored development.
- Paul Kienzle, Zac Lee, Josh VanderLinden, Ian Fitzpatrick, Lucas Klein, Rui Chicoria, Travis Odom, Patrick Huber, Brad Campbell, Daniel, Sean Arietta, Sacha Stafyniak submitted code to expand support of the socket.io protocol.
- Bernard Pratz, Francis Bull wrote prototypes to support xhr-polling and jsonp-polling.
- Eric Chen, Denis Zinevich, Thiago Hersan, Nayef Copty, Jörgen Karlsson, Branden Ghena, Tim Landscheidt, Khairi Hafsham, Matt Porritt, Matt Dainty, Thomaz de Oliveira dos Reis, Felix König, George Wilson, Andreas Strikos, Alessio Sergi Claudio Yacarini, Robbie Clarken suggested ways to make the connection more robust.
- Merlijn van Deen, Frederic Sureau, Marcus Cobden, Drew Hutchison, wuurrd, Adam Kecer, Alex Monk, Vishal P R, John Vandenberg, Thomas Grainger, Daniel Quinn, Adric Worley, Adam Roses Wight, Jan Včelák proposed changes that make the library more friendly and practical for you!