Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ before_install:
- sudo apt-get install nodejs; node --version
install:
- pip install -U coverage requests six websocket-client
- npm install -G socket.io
- npm install -G socket.io@1
before_script:
- DEBUG=* node socketIO_client/tests/serve.js &
- sleep 1
Expand Down
128 changes: 95 additions & 33 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,54 @@

socketIO-client
===============
Here is a `socket.io <http://socket.io>`_ client library for Python. You can use it to write test code for your socket.io server.
Here is a `socket.io <http://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 <https://github.com/automattic/socket.io-protocol>`_, which is not backwards compatible. If you want to communicate using `socket.io protocol 0.9 <https://github.com/learnboost/socket.io-spec>`_ (which is compatible with `gevent-socketio <https://github.com/abourget/gevent-socketio>`_), please use `socketIO-client 0.5.7.2 <https://pypi.python.org/pypi/socketIO-client/0.5.7.2>`_.
Please note that this version implements `socket.io protocol 1 <https://github.com/automattic/socket.io-protocol>`_, which is not backwards compatible. If you want to communicate using `socket.io protocol 0 <https://github.com/learnboost/socket.io-spec>`_ (which is compatible with `gevent-socketio <https://github.com/abourget/gevent-socketio>`_), please use `socketIO-client 0.5.7.4 <https://pypi.python.org/pypi/socketIO-client/0.5.7.4>`_.


Installation
------------
Install the package in an isolated environment. ::

VIRTUAL_ENV=$HOME/.virtualenv

# Prepare isolated environment
VIRTUAL_ENV=$HOME/.virtualenvs/crosscompute
virtualenv $VIRTUAL_ENV

# Activate isolated environment
source $VIRTUAL_ENV/bin/activate
pip install -U socketIO-client==0.8.0


Test
----
Install additional packages if you want to run the tests. ::

# Install package
pip install -U socketIO-client
VIRTUAL_ENV=$HOME/.virtualenvs/crosscompute
export NODE_PATH=$VIRTUAL_ENV/lib/node_modules
export NPM_CONFIG_PREFIX=$VIRTUAL_ENV
npm install -g socket.io@1

Launch test server and run tests.::

cd ~/Documents
git clone https://github.com/invisibleroads/socketIO-client
git checkout -t 0.8.0
cd socketIO-client
DEBUG=* node tests/serve.js # Start socket.io server in terminal one
nosetests # Run tests in terminal two


Usage
-----
Activate isolated environment. ::

VIRTUAL_ENV=$HOME/.virtualenv
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__))")
Start your socket.io server. ::

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
nosetests # Run tests in terminal three

For debugging information, run these commands first. ::

Expand Down Expand Up @@ -170,17 +182,19 @@ Connect via SSL (https://github.com/invisibleroads/socketIO-client/issues/54). :
socketIO = SocketIO('https://127.0.0.1', verify='server.crt', cert=(
'client.crt', 'client.key'))

Specify params, headers, cookies, proxies thanks to the `requests <http://python-requests.org>`_ library. ::
Specify params, headers, cookies, proxies thanks to the `requests <http://docs.python-requests.org>`_ 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'})
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. ::

Expand All @@ -191,8 +205,8 @@ Wait forever. ::

Don't wait forever. ::

from requests.exceptions import ConnectionError
from socketIO_client import SocketIO
from socketIO_client.exceptions import ConnectionError

try:
socket = SocketIO('127.0.0.1', 8000, wait_for_connection=False)
Expand All @@ -208,12 +222,60 @@ This software is available under the MIT License.

Credits
-------
- `Guillermo Rauch <https://github.com/rauchg>`_ wrote the `socket.io specification <https://github.com/automattic/socket.io-protocol>`_.
- `Hiroki Ohtani <https://github.com/liris>`_ wrote `websocket-client <https://github.com/liris/websocket-client>`_.
- `Roderick Hodgson <https://github.com/roderickhodgson>`_ wrote a `prototype for a Python client to a socket.io server <http://stackoverflow.com/questions/6692908/formatting-messages-to-send-to-socket-io-node-js-server-from-python-client>`_.
- `Alexandre Bourget <https://github.com/abourget>`_ wrote `gevent-socketio <https://github.com/abourget/gevent-socketio>`_, which is a socket.io server written in Python.
- `Paul Kienzle <https://github.com/pkienzle>`_, `Zac Lee <https://github.com/zratic>`_, `Josh VanderLinden <https://github.com/codekoala>`_, `Ian Fitzpatrick <https://github.com/ifitzpatrick>`_, `Lucas Klein <https://github.com/lukasklein>`_, `Rui Chicoria <https://github.com/rchicoria>`_, `Travis Odom <https://github.com/burstaholic>`_, `Patrick Huber <https://github.com/stackmagic>`_, `Brad Campbell <https://github.com/bradjc>`_, `Daniel <https://github.com/dabidan>`_, `Sean Arietta <https://github.com/sarietta>`_, `Sacha Stafyniak <https://github.com/stafyniaksacha>`_ submitted code to expand support of the socket.io protocol.
- `Bernard Pratz <https://github.com/guyzmo>`_, `Francis Bull <https://github.com/franbull>`_ wrote prototypes to support xhr-polling and jsonp-polling.
- `Joe Palmer <https://github.com/softforge>`_ sponsored development.
- `Eric Chen <https://github.com/taiyangc>`_, `Denis Zinevich <https://github.com/dzinevich>`_, `Thiago Hersan <https://github.com/thiagohersan>`_, `Nayef Copty <https://github.com/nayefc>`_, `Jörgen Karlsson <https://github.com/jorgen-k>`_, `Branden Ghena <https://github.com/brghena>`_, `Tim Landscheidt <https://github.com/scfc>`_, `Matt Porritt <https://github.com/mattporritt>`_, `Matt Dainty <https://github.com/bodgit>`_, `Thomaz de Oliveira dos Reis <https://github.com/thor27>`_, `Felix König <https://github.com/Felk>`_, `George Wilson <https://github.com/wilsonge>`_, `Andreas Strikos <https://github.com/astrikos>`_, `Alessio Sergi <https://github.com/asergi>`_ `Claudio Yacarini <https://github.com/cyacarinic>`_, `Khairi Hafsham <https://github.com/khairihafsham>`_, `Robbie Clarken <https://github.com/RobbieClarken>`_ suggested ways to make the connection more robust.
- `Merlijn van Deen <https://github.com/valhallasw>`_, `Frederic Sureau <https://github.com/fredericsureau>`_, `Marcus Cobden <https://github.com/leth>`_, `Drew Hutchison <https://github.com/drewhutchison>`_, `wuurrd <https://github.com/wuurrd>`_, `Adam Kecer <https://github.com/amfg>`_, `Alex Monk <https://github.com/Krenair>`_, `Vishal P R <https://github.com/vishalwy>`_, `John Vandenberg <https://github.com/jayvdb>`_, `Thomas Grainger <https://github.com/graingert>`_, `Daniel Quinn <https://github.com/danielquinn>`_, `Adric Worley <https://github.com/AdricEpic>`_, `Adam Roses Wight <https://github.com/adamwight>`_, `Jan Včelák <https://github.com/fcelda>`_ proposed changes that make the library more friendly and practical for you!
- `Guillermo Rauch <https://github.com/rauchg>`_
wrote the `socket.io specification <https://github.com/automattic/socket.io-protocol>`_.
- `Hiroki Ohtani <https://github.com/liris>`_
wrote `websocket-client <https://github.com/liris/websocket-client>`_.
- `Roderick Hodgson <https://github.com/roderickhodgson>`_
wrote a `prototype for a Python client to a socket.io server <http://stackoverflow.com/questions/6692908/formatting-messages-to-send-to-socket-io-node-js-server-from-python-client>`_.
- `Alexandre Bourget <https://github.com/abourget>`_
wrote `gevent-socketio <https://github.com/abourget/gevent-socketio>`_, which is a socket.io server written in Python.
- `Joe Palmer <https://github.com/softforge>`_
sponsored development.
- `Paul Kienzle <https://github.com/pkienzle>`_,
`Zac Lee <https://github.com/zratic>`_,
`Josh VanderLinden <https://github.com/codekoala>`_,
`Ian Fitzpatrick <https://github.com/ifitzpatrick>`_,
`Lucas Klein <https://github.com/lukasklein>`_,
`Rui Chicoria <https://github.com/rchicoria>`_,
`Travis Odom <https://github.com/burstaholic>`_,
`Patrick Huber <https://github.com/stackmagic>`_,
`Brad Campbell <https://github.com/bradjc>`_,
`Daniel <https://github.com/dabidan>`_,
`Sean Arietta <https://github.com/sarietta>`_,
`Sacha Stafyniak <https://github.com/stafyniaksacha>`_
submitted code to expand support of the socket.io protocol.
- `Bernard Pratz <https://github.com/guyzmo>`_,
`Francis Bull <https://github.com/franbull>`_ wrote prototypes to support xhr-polling and jsonp-polling.
- `Eric Chen <https://github.com/taiyangc>`_,
`Denis Zinevich <https://github.com/dzinevich>`_,
`Thiago Hersan <https://github.com/thiagohersan>`_,
`Nayef Copty <https://github.com/nayefc>`_,
`Jörgen Karlsson <https://github.com/jorgen-k>`_,
`Branden Ghena <https://github.com/brghena>`_,
`Tim Landscheidt <https://github.com/scfc>`_,
`Khairi Hafsham <https://github.com/khairihafsham>`_,
`Matt Porritt <https://github.com/mattporritt>`_,
`Matt Dainty <https://github.com/bodgit>`_,
`Thomaz de Oliveira dos Reis <https://github.com/thor27>`_,
`Felix König <https://github.com/Felk>`_,
`George Wilson <https://github.com/wilsonge>`_,
`Andreas Strikos <https://github.com/astrikos>`_,
`Alessio Sergi <https://github.com/asergi>`_ `Claudio Yacarini <https://github.com/cyacarinic>`_,
`Robbie Clarken <https://github.com/RobbieClarken>`_
suggested ways to make the connection more robust.
- `Merlijn van Deen <https://github.com/valhallasw>`_,
`Frederic Sureau <https://github.com/fredericsureau>`_,
`Marcus Cobden <https://github.com/leth>`_,
`Drew Hutchison <https://github.com/drewhutchison>`_,
`wuurrd <https://github.com/wuurrd>`_,
`Adam Kecer <https://github.com/amfg>`_,
`Alex Monk <https://github.com/Krenair>`_,
`Vishal P R <https://github.com/vishalwy>`_,
`John Vandenberg <https://github.com/jayvdb>`_,
`Thomas Grainger <https://github.com/graingert>`_,
`Daniel Quinn <https://github.com/danielquinn>`_,
`Adric Worley <https://github.com/AdricEpic>`_,
`Adam Roses Wight <https://github.com/adamwight>`_,
`Jan Včelák <https://github.com/fcelda>`_
proposed changes that make the library more friendly and practical for you!
13 changes: 7 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@


HERE = dirname(abspath(__file__))
LOAD_TEXT = lambda name: io.open(join(HERE, name), encoding='UTF-8').read()
DESCRIPTION = '\n\n'.join(LOAD_TEXT(_) for _ in [
DESCRIPTION = '\n\n'.join(io.open(
join(HERE, _), encoding='utf-8',
).read() for _ in [
'README.rst',
'CHANGES.rst',
])
setup(
name='socketIO-client',
version='0.7.2',
version='0.8.0',
description='A socket.io client library',
long_description=DESCRIPTION,
license='MIT',
Expand All @@ -26,9 +27,9 @@
author_email='[email protected]',
url='https://github.com/invisibleroads/socketIO-client',
install_requires=[
'requests>=2.7.0',
'six',
'websocket-client',
'requests>=2.18.2',
'six>=1.10.0',
'websocket-client>=0.44.0',
],
tests_require=[
'nose',
Expand Down
8 changes: 5 additions & 3 deletions socketIO_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
WebsocketTransport, XHR_PollingTransport, prepare_http_session, TRANSPORTS)


__all__ = 'SocketIO', 'SocketIONamespace'
__version__ = '0.7.2'
__all__ = [
'SocketIO',
'SocketIONamespace',
]
BaseNamespace = SocketIONamespace
LoggingNamespace = LoggingSocketIONamespace

Expand Down Expand Up @@ -336,7 +338,7 @@ class SocketIO(EngineIO):
SocketIO(
'127.0.0.1', 8000,
params={'q': 'qqq'},
headers={'Authorization': 'Basic ' + b64encode('username:password')},
headers={'Authorization': 'Bearer xyz'},
cookies={'a': 'aaa'},
proxies={'https': 'https://proxy.example.com:8080'})
"""
Expand Down
4 changes: 2 additions & 2 deletions socketIO_client/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class ConnectionError(SocketIOError):
pass


class TimeoutError(SocketIOError):
class PacketError(SocketIOError):
pass


class PacketError(SocketIOError):
class TimeoutError(SocketIOError):
pass
5 changes: 1 addition & 4 deletions socketIO_client/heartbeats.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from invisibleroads_macros.log import get_log
from threading import Thread, Event

from .exceptions import ConnectionError, TimeoutError


L = get_log(__name__)
from .logs import L


class HeartbeatThread(Thread):
Expand Down
3 changes: 2 additions & 1 deletion socketIO_client/logs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import time
from invisibleroads_macros.log import get_log

from .symmetries import get_log


L = get_log('socketIO-client')
Expand Down
22 changes: 17 additions & 5 deletions socketIO_client/symmetries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,34 @@


try:
from ssl import SSLError
except ImportError:
class SSLError(Exception):
pass
from logging import NullHandler
except ImportError: # Python 2.6
from logging import Handler

class NullHandler(Handler):

def emit(self, record):
pass
finally:
from logging import getLogger


try:
memoryview = memoryview
except NameError:
memoryview = buffer
memoryview = buffer # noqa


def get_byte(x, index):
return indexbytes(x, index)


def get_log(name):
log = getLogger(name)
log.addHandler(NullHandler())
return log


def get_character(x, index):
return chr(get_byte(x, index))

Expand Down
4 changes: 3 additions & 1 deletion socketIO_client/transports.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@

yes | pip uninstall websocket websocket-client
pip install -U websocket-client""")
else:
from websocket._ssl_compat import SSLError

from .exceptions import ConnectionError, TimeoutError
from .parsers import (
encode_engineIO_content, decode_engineIO_content,
format_packet_text, parse_packet_text)
from .symmetries import SSLError, memoryview
from .symmetries import memoryview


ENGINEIO_PROTOCOL = 3
Expand Down