• Stars
    star
    446
  • Rank 97,259 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created over 12 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A socket.io client library for Python
https://travis-ci.org/invisibleroads/socketIO-client.svg?branch=master

socketIO-client

Here is a socket.io client library for Python. You can use it to write test code for your socket.io server.

Please note that this version implements socket.io protocol 1.x, which is not backwards compatible. If you want to communicate using socket.io protocol 0.9 (which is compatible with gevent-socketio), please use socketIO-client 0.5.7.2.

Installation

Install the package in an isolated environment.

VIRTUAL_ENV=$HOME/.virtualenv

# Prepare isolated environment
virtualenv $VIRTUAL_ENV

# Activate isolated environment
source $VIRTUAL_ENV/bin/activate

# Install package
pip install -U socketIO-client

Usage

Activate isolated environment.

VIRTUAL_ENV=$HOME/.virtualenv
source $VIRTUAL_ENV/bin/activate

Launch your socket.io server.

cd $(python -c "import os, socketIO_client;\
    print(os.path.dirname(socketIO_client.__file__))")

DEBUG=* node tests/serve.js  # Start socket.io server in terminal one
DEBUG=* node tests/proxy.js  # Start proxy server in terminal two
nosetests                    # Run tests in terminal three

For debugging information, run these commands first.

import logging
logging.getLogger('socketIO-client').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 on_aaa_response(self, *args):
        print('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')
news_namespace = socketIO.define(NewsNamespace, '/news')

chat_namespace.emit('aaa')
news_namespace.emit('aaa')
socketIO.wait(seconds=1)

Connect via SSL (#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': 'Basic ' + b64encode('username:password')},
    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

More Repositories

1

invisibleroads-tutorials

Python
21
star
2

pyramid-examples

Example applications using the web framework Pyramid
Python
14
star
3

imapIO

Convenience classes and methods for processing IMAP mailboxes
Python
12
star
4

geometryIO

GDAL wrapper for reading and writing geospatial data to a variety of vector formats
Python
8
star
5

networkplanner

Electricity infrastructure prototyping framework
Python
6
star
6

geotable

Read and write spatial vectors
Python
6
star
7

georegistry

Web service for centralizing geospatial data across applications
Python
6
star
8

scripts

Useful command-line scripts
Shell
5
star
9

whenIO

Methods for formatting and parsing friendly timestamps
Python
5
star
10

find-buildings-gblearn2

Find buildings in satellite images using Yann LeCun's gblearn2
Python
5
star
11

invisibleroads-scaffolds

Pyramid application templates based on invisibleroads.com
JavaScript
3
star
12

imap-transfer

Command-line utility for transferring messages from one IMAP server to another
Python
3
star
13

find-buildings-cuda-convnet

Find buildings in satellite images using Alex Krizhevsky's cuda-convnet
Python
3
star
14

inteum-quickbooks-sync

Python
2
star
15

inteum-dashboard

A simple web dashboard for tracking patent activity from Inteum C/S
Python
2
star
16

archiveIO

Convenience decorators for reading and writing to compressed archives
Python
2
star
17

imap-search-scout

Python
2
star
18

invisibleroads

Simple framework for extensible command line scripts
Python
2
star
19

scripts-extra

Python
2
star
20

invisibleroads-posts

Web application defaults
Python
1
star
21

jobitos

Python
1
star
22

inteum-lotus-helper

Python
1
star
23

pycon-nyc-bid

Let's bring the Python Conference to New York City for 2016 and 2017!
1
star
24

human-trafficking

Python
1
star