commit 6a73a57bc305c47bc7c827aba74a0d23a740a7e3 Author: Roy Hyunjin Han Date: Sat Apr 14 12:09:14 2012 -0400 Wrapped code from http://stackoverflow.com/questions/6692908/formatting-messages-to-send-to-socket-io-node-js-server-from-python-client/ diff --git a/CHANGES.rst b/CHANGES.rst new file mode 100644 index 0000000..e1fd6da --- /dev/null +++ b/CHANGES.rst @@ -0,0 +1,3 @@ +0.1 +--- +- Wrapped code from `StackOverflow `_ diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..f3be32c --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,3 @@ +recursive-include socketIO * +include *.rst +global-exclude *.pyc diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..e761b8b --- /dev/null +++ b/README.rst @@ -0,0 +1,36 @@ +socketIO.client +=============== +Here is a barebones `socket.io `_ client library for Python. + +Thanks to `rod `_ for his `StackOverflow question and answer `_, on which this code is based. + +Thanks also to `liris `_ for his `websocket-client `_ and to `guille `_ for the `socket.io specification `_. + + +Installation +------------ +:: + + # Prepare isolated environment + ENV=$HOME/Projects/env + virtualenv $ENV + mkdir $ENV/opt + + # Activate isolated environment + source $ENV/bin/activate + + # Install package + easy_install -U socketIO-client + + +Usage +----- +:: + + ENV=$HOME/Projects/env + source $ENV/bin/activate + python + + from socketIO import SocketIO + s = SocketIO('localhost', 8000) + s.emit('news', {'hello': 'world'}) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..babba77 --- /dev/null +++ b/setup.py @@ -0,0 +1,31 @@ +import os + +from setuptools import setup, find_packages + + +here = os.path.abspath(os.path.dirname(__file__)) +README = open(os.path.join(here, 'README.rst')).read() +CHANGES = open(os.path.join(here, 'CHANGES.rst')).read() + + +setup( + name='socketIO-client', + version='0.1', + description='Barebones socket.io client library', + long_description=README + '\n\n' + CHANGES, + license='MIT', + classifiers=[ + 'Intended Audience :: Developers', + 'Programming Language :: Python', + 'License :: OSI Approved :: MIT License', + ], + keywords='socket.io node.js', + author='Roy Hyunjin Han', + author_email='starsareblueandfaraway@gmail.com', + url='https://github.com/invisibleroads/socketIO-client', + install_requires=[ + 'websocket-client', + ], + packages=find_packages(), + include_package_data=True, + zip_safe=True) diff --git a/socketIO/__init__.py b/socketIO/__init__.py new file mode 100644 index 0000000..07fd24c --- /dev/null +++ b/socketIO/__init__.py @@ -0,0 +1,68 @@ +from simplejson import dumps +from threading import Thread, Event +from urllib import urlopen +from websocket import create_connection + + +class SocketIO(object): + + def __init__(self, host, port): + self.host = host + self.port = port + self.__do_handshake() + self.__connect() + self.heartbeatThread = RhythmicThread(self.heartbeatTimeout - 2, self.__send_heartbeat) + self.heartbeatThread.start() + + def __do_handshake(self): + try: + response = urlopen('http://%s:%d/socket.io/1/' % (self.host, self.port)) + except IOError: + raise SocketIOError('Could not start connection') + if 200 != response.getcode(): + raise SocketIOError('Could not establish connection') + self.sessionID, heartbeatTimeout, connectionTimeout, supportedTransports = response.readline().split(':') + self.heartbeatTimeout = int(heartbeatTimeout) + self.connectionTimeout = int(connectionTimeout) + if 'websocket' not in supportedTransports.split(','): + raise SocketIOError('Could not parse handshake') + + def __connect(self): + self.connection = create_connection('ws://%s:%d/socket.io/1/websocket/%s' % (self.host, self.port, self.sessionID)) + + def __del__(self): + self.heartbeatThread.cancel() + self.connection.close() + + def __send_heartbeat(self): + self.connection.send('2::') + + def emit(self, eventName, eventData): + self.connection.send('5:::' + dumps(dict(name=eventName, args=eventData))) + + +class SocketIOError(Exception): + pass + + +class RhythmicThread(Thread): + 'Execute function every few seconds' + + daemon = True + + def __init__(self, intervalInSeconds, function, *args, **kw): + super(RhythmicThread, self).__init__() + self.intervalInSeconds = intervalInSeconds + self.function = function + self.args = args + self.kw = kw + self.done = Event() + + def cancel(self): + self.done.set() + + def run(self): + self.done.wait(self.intervalInSeconds) + while not self.done.is_set(): + self.function(*self.args, **self.kw) + self.done.wait(self.intervalInSeconds) diff --git a/socketIO/__init__.pyc b/socketIO/__init__.pyc new file mode 100644 index 0000000..eee6ebe Binary files /dev/null and b/socketIO/__init__.pyc differ diff --git a/socketIO_client.egg-info/PKG-INFO b/socketIO_client.egg-info/PKG-INFO new file mode 100644 index 0000000..326b844 --- /dev/null +++ b/socketIO_client.egg-info/PKG-INFO @@ -0,0 +1,55 @@ +Metadata-Version: 1.0 +Name: socketIO-client +Version: 0.1 +Summary: Barebones socket.io client library +Home-page: https://github.com/invisibleroads/socketIO-client +Author: Roy Hyunjin Han +Author-email: starsareblueandfaraway@gmail.com +License: MIT +Description: socketIO.client + =============== + Here is a barebones `socket.io `_ client library for Python. + + Thanks to `rod `_ for his `StackOverflow question and answer `_, on which this code is based. + + Thanks also to `liris `_ for his `websocket-client `_ and to `guille `_ for the `socket.io specification `_. + + + Installation + ------------ + :: + + # Prepare isolated environment + ENV=$HOME/Projects/env + virtualenv $ENV + mkdir $ENV/opt + + # Activate isolated environment + source $ENV/bin/activate + + # Install package + easy_install -U socketIO-client + + + Usage + ----- + :: + + ENV=$HOME/Projects/env + source $ENV/bin/activate + python + + from socketIO import SocketIO + s = SocketIO('localhost', 8000) + s.emit('news', {'hello': 'world'}) + + + 0.1 + --- + - Wrapped code from `StackOverflow `_ + +Keywords: socket.io node.js +Platform: UNKNOWN +Classifier: Intended Audience :: Developers +Classifier: Programming Language :: Python +Classifier: License :: OSI Approved :: MIT License diff --git a/socketIO_client.egg-info/SOURCES.txt b/socketIO_client.egg-info/SOURCES.txt new file mode 100644 index 0000000..008eb59 --- /dev/null +++ b/socketIO_client.egg-info/SOURCES.txt @@ -0,0 +1,11 @@ +CHANGES.rst +MANIFEST.in +README.rst +setup.py +socketIO/__init__.py +socketIO_client.egg-info/PKG-INFO +socketIO_client.egg-info/SOURCES.txt +socketIO_client.egg-info/dependency_links.txt +socketIO_client.egg-info/requires.txt +socketIO_client.egg-info/top_level.txt +socketIO_client.egg-info/zip-safe \ No newline at end of file diff --git a/socketIO_client.egg-info/dependency_links.txt b/socketIO_client.egg-info/dependency_links.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/socketIO_client.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/socketIO_client.egg-info/requires.txt b/socketIO_client.egg-info/requires.txt new file mode 100644 index 0000000..4126a48 --- /dev/null +++ b/socketIO_client.egg-info/requires.txt @@ -0,0 +1 @@ +websocket-client \ No newline at end of file diff --git a/socketIO_client.egg-info/top_level.txt b/socketIO_client.egg-info/top_level.txt new file mode 100644 index 0000000..a815bbe --- /dev/null +++ b/socketIO_client.egg-info/top_level.txt @@ -0,0 +1 @@ +socketIO diff --git a/socketIO_client.egg-info/zip-safe b/socketIO_client.egg-info/zip-safe new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/socketIO_client.egg-info/zip-safe @@ -0,0 +1 @@ +