Wrapped code from http://stackoverflow.com/questions/6692908/formatting-messages-to-send-to-socket-io-node-js-server-from-python-client/
This commit is contained in:
commit
6a73a57bc3
12 changed files with 211 additions and 0 deletions
3
CHANGES.rst
Normal file
3
CHANGES.rst
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
0.1
|
||||||
|
---
|
||||||
|
- Wrapped code from `StackOverflow <http://stackoverflow.com/questions/6692908/formatting-messages-to-send-to-socket-io-node-js-server-from-python-client/>`_
|
||||||
3
MANIFEST.in
Normal file
3
MANIFEST.in
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
recursive-include socketIO *
|
||||||
|
include *.rst
|
||||||
|
global-exclude *.pyc
|
||||||
36
README.rst
Normal file
36
README.rst
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
socketIO.client
|
||||||
|
===============
|
||||||
|
Here is a barebones `socket.io <http://socket.io>`_ client library for Python.
|
||||||
|
|
||||||
|
Thanks to `rod <http://stackoverflow.com/users/370115/rod>`_ for his `StackOverflow question and answer <http://stackoverflow.com/questions/6692908/formatting-messages-to-send-to-socket-io-node-js-server-from-python-client/>`_, on which this code is based.
|
||||||
|
|
||||||
|
Thanks also to `liris <https://github.com/liris>`_ for his `websocket-client <https://github.com/liris/websocket-client>`_ and to `guille <https://github.com/guille>`_ for the `socket.io specification <https://github.com/LearnBoost/socket.io-spec>`_.
|
||||||
|
|
||||||
|
|
||||||
|
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'})
|
||||||
31
setup.py
Normal file
31
setup.py
Normal file
|
|
@ -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)
|
||||||
68
socketIO/__init__.py
Normal file
68
socketIO/__init__.py
Normal file
|
|
@ -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)
|
||||||
BIN
socketIO/__init__.pyc
Normal file
BIN
socketIO/__init__.pyc
Normal file
Binary file not shown.
55
socketIO_client.egg-info/PKG-INFO
Normal file
55
socketIO_client.egg-info/PKG-INFO
Normal file
|
|
@ -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 <http://socket.io>`_ client library for Python.
|
||||||
|
|
||||||
|
Thanks to `rod <http://stackoverflow.com/users/370115/rod>`_ for his `StackOverflow question and answer <http://stackoverflow.com/questions/6692908/formatting-messages-to-send-to-socket-io-node-js-server-from-python-client/>`_, on which this code is based.
|
||||||
|
|
||||||
|
Thanks also to `liris <https://github.com/liris>`_ for his `websocket-client <https://github.com/liris/websocket-client>`_ and to `guille <https://github.com/guille>`_ for the `socket.io specification <https://github.com/LearnBoost/socket.io-spec>`_.
|
||||||
|
|
||||||
|
|
||||||
|
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 <http://stackoverflow.com/questions/6692908/formatting-messages-to-send-to-socket-io-node-js-server-from-python-client/>`_
|
||||||
|
|
||||||
|
Keywords: socket.io node.js
|
||||||
|
Platform: UNKNOWN
|
||||||
|
Classifier: Intended Audience :: Developers
|
||||||
|
Classifier: Programming Language :: Python
|
||||||
|
Classifier: License :: OSI Approved :: MIT License
|
||||||
11
socketIO_client.egg-info/SOURCES.txt
Normal file
11
socketIO_client.egg-info/SOURCES.txt
Normal file
|
|
@ -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
|
||||||
1
socketIO_client.egg-info/dependency_links.txt
Normal file
1
socketIO_client.egg-info/dependency_links.txt
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
|
||||||
1
socketIO_client.egg-info/requires.txt
Normal file
1
socketIO_client.egg-info/requires.txt
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
websocket-client
|
||||||
1
socketIO_client.egg-info/top_level.txt
Normal file
1
socketIO_client.egg-info/top_level.txt
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
socketIO
|
||||||
1
socketIO_client.egg-info/zip-safe
Normal file
1
socketIO_client.egg-info/zip-safe
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue