diff --git a/.travis.yml b/.travis.yml index 079fe51..79d7e0f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/README.rst b/README.rst index 6bd2be7..d1de2ea 100644 --- a/README.rst +++ b/README.rst @@ -4,42 +4,54 @@ socketIO-client =============== -Here is a `socket.io `_ client library for Python. You can use it to write test code for your socket.io server. +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 `_. +Please note that this version implements `socket.io protocol 1 `_, which is not backwards compatible. If you want to communicate using `socket.io protocol 0 `_ (which is compatible with `gevent-socketio `_), please use `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. :: @@ -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 `_ library. :: +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'}) + 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. :: @@ -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) @@ -208,12 +222,60 @@ 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. -- `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. -- `Joe Palmer `_ sponsored development. -- `Eric Chen `_, `Denis Zinevich `_, `Thiago Hersan `_, `Nayef Copty `_, `Jörgen Karlsson `_, `Branden Ghena `_, `Tim Landscheidt `_, `Matt Porritt `_, `Matt Dainty `_, `Thomaz de Oliveira dos Reis `_, `Felix König `_, `George Wilson `_, `Andreas Strikos `_, `Alessio Sergi `_ `Claudio Yacarini `_, `Khairi Hafsham `_, `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! +- `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! diff --git a/setup.py b/setup.py index ed1811e..b3b9679 100644 --- a/setup.py +++ b/setup.py @@ -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', @@ -26,9 +27,9 @@ author_email='rhh@crosscompute.com', 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', diff --git a/socketIO_client/__init__.py b/socketIO_client/__init__.py index 5b935f0..4bfb464 100644 --- a/socketIO_client/__init__.py +++ b/socketIO_client/__init__.py @@ -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 @@ -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'}) """ diff --git a/socketIO_client/exceptions.py b/socketIO_client/exceptions.py index ed2b4d2..a16697c 100644 --- a/socketIO_client/exceptions.py +++ b/socketIO_client/exceptions.py @@ -6,9 +6,9 @@ class ConnectionError(SocketIOError): pass -class TimeoutError(SocketIOError): +class PacketError(SocketIOError): pass -class PacketError(SocketIOError): +class TimeoutError(SocketIOError): pass diff --git a/socketIO_client/heartbeats.py b/socketIO_client/heartbeats.py index a039e73..8ff92a2 100644 --- a/socketIO_client/heartbeats.py +++ b/socketIO_client/heartbeats.py @@ -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): diff --git a/socketIO_client/logs.py b/socketIO_client/logs.py index d2c3119..7284235 100644 --- a/socketIO_client/logs.py +++ b/socketIO_client/logs.py @@ -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') diff --git a/socketIO_client/symmetries.py b/socketIO_client/symmetries.py index 852fc98..d792898 100644 --- a/socketIO_client/symmetries.py +++ b/socketIO_client/symmetries.py @@ -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)) diff --git a/socketIO_client/transports.py b/socketIO_client/transports.py index 85baa06..c43ed0c 100644 --- a/socketIO_client/transports.py +++ b/socketIO_client/transports.py @@ -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