From 6a73a57bc305c47bc7c827aba74a0d23a740a7e3 Mon Sep 17 00:00:00 2001 From: Roy Hyunjin Han Date: Sat, 14 Apr 2012 12:09:14 -0400 Subject: [PATCH] Wrapped code from http://stackoverflow.com/questions/6692908/formatting-messages-to-send-to-socket-io-node-js-server-from-python-client/ --- CHANGES.rst | 3 + MANIFEST.in | 3 + README.rst | 36 ++++++++++ setup.py | 31 ++++++++ socketIO/__init__.py | 68 ++++++++++++++++++ socketIO/__init__.pyc | Bin 0 -> 3934 bytes socketIO_client.egg-info/PKG-INFO | 55 ++++++++++++++ socketIO_client.egg-info/SOURCES.txt | 11 +++ socketIO_client.egg-info/dependency_links.txt | 1 + socketIO_client.egg-info/requires.txt | 1 + socketIO_client.egg-info/top_level.txt | 1 + socketIO_client.egg-info/zip-safe | 1 + 12 files changed, 211 insertions(+) create mode 100644 CHANGES.rst create mode 100644 MANIFEST.in create mode 100644 README.rst create mode 100644 setup.py create mode 100644 socketIO/__init__.py create mode 100644 socketIO/__init__.pyc create mode 100644 socketIO_client.egg-info/PKG-INFO create mode 100644 socketIO_client.egg-info/SOURCES.txt create mode 100644 socketIO_client.egg-info/dependency_links.txt create mode 100644 socketIO_client.egg-info/requires.txt create mode 100644 socketIO_client.egg-info/top_level.txt create mode 100644 socketIO_client.egg-info/zip-safe 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 0000000000000000000000000000000000000000..eee6ebe432f572eb0b4bacb2de8ab0a082b0977e GIT binary patch literal 3934 zcmb_f+in|G6y3+;*m2U((9)DUE^UPoZA~i`q^Ojt7oohQFo`HcNF!(Fq@H9vW9A$; zt<<~_UXT#qz`yVS5}(5dz*>8JX@Ixd$=TlLGROO}_GN; zKZ+cSe7mzGNmJ~yt}aejPf5}e8|doNbah3NW!dVSfcIqxs8Kdknx@kN!NVf`!g*zeMd|%{ ztV9!yqbMn&elJg4zXubXIk`7BlYa%vVgH%$57KyS>{Z@td!^rnLATR^Ek@qIaq@9z z^CX%c@~2NcGV_`9xTZtvsF2QPdm)V$(t{n0invd~b^FD@cFTUh`(;_|!-cNv#?HGM zXDOUNop+-s%~Kym*M`s3%ab|n;)p6SE&MIv?`;#)<-`_;dgxCu&)VWO7HId0d-iIw zc5LLx$fNunsrx_B2Y3up%9=yaeI@X z!TNXV?r2etYH}3FQC)nC0Kpc7h?aB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g-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 @@ +