Event Source Library
Project description
README: Event Source Library for Python
=======================================
This library implements W3C Draft's on event-source:
- http://dev.w3.org/html5/eventsource/
It enables a halfduplex communication from server to client, but initiated
by the client, through standard HTTP(S) communication.
Dependances
-----------
- Fairly recent python (tested with 2.7)
- Fairly recent tornado (tested with 2.2.1)
Usage
-----
1. Launch the server:
eventsource_listener -P 8888 -i -k 50000
2. Launch the client:
eventsource_client 42:42:42:42:42:42 -r 5000
3. Send requests:
eventsource_request 42:42:42:42:42:42 ping "42"
eventsource_request 42:42:42:42:42:42 close
Command Line arguments
----------------------
- `eventsource/listener.py` or `eventsource_server`:
usage: eventsource/listener.py [-h] [-H HOST] [-P PORT] [-d]
[-j] [-k KEEPALIVE] [-i]
Event Source Listener
optional arguments:
-h, --help show this help message and exit
-H HOST, --host HOST Host to bind on
-P PORT, --port PORT Port to bind on
-d, --debug enables debug output
-j, --json to enable JSON Event
-k KEEPALIVE, --keepalive KEEPALIVE
Keepalive timeout
-i, --id to generate identifiers
- `eventsource/client.py` or `eventsource_client`:
usage: eventsource/client.py [-h] [-H HOST] [-P PORT] [-d]
[-r RETRY]
token
Event Source Client
positional arguments:
token Token to be used for connection
optional arguments:
-h, --help show this help message and exit
-H HOST, --host HOST Host to connect to
-P PORT, --port PORT Port to be used connection
-d, --debug enables debug output
-r RETRY, --retry RETRY
Reconnection timeout
- `eventsource/send_request.py` or `eventsource_request`:
usage: eventsource/send_request.py [-h] [-H HOST] [-P PORT] [-j]
token action [data]
Generates event for Event Source Library
positional arguments:
token Token to be used for connection
action Action to send
data Data to be sent
optional arguments:
-h, --help show this help message and exit
-H HOST, --host HOST Host to connect to
-P PORT, --port PORT Port to be used connection
-j, --json Treat data as JSON
Integrate
---------
On the server side, basically all you have to do is to add the following to your code:
from eventsource import listener
application = tornado.web.Application([
(r"/(.*)/(.*)", listener.EventSourceHandler,
dict(event_class=EVENT,
keepalive=KEEPALIVE)),
])
application.listen(PORT)
tornado.ioloop.IOLoop.instance().start()
where:
- PORT is an integer for the port to bind to
- KEEPALIVE is an integer for the timeout between two keepalive messages (to protect from disconnections)
- EVENT is a eventsource.listener.Event based class, either one you made or
- eventsource.listener.StringEvent : Each event gets and resends multiline strings
- eventsource.listener.StringIdEvent : Each event gets and resends multiline strings, with an unique id for each event
- eventsource.listener.JSONEvent : Each event gets and resends JSON valid strings
- eventsource.listener.JSONIdEvent : Each event gets and resends JSON valid string, with an unique id for each event
Extend
------
To extend the behaviour of the event source library, without breaking eventsource
definition, the Event based classes implements all processing elements that shall
be done on events.
There is two abstract classes that defines Event:
- eventsource.listener.Event : defines the constructor of an Event
- eventsource.listener.EventId : defines an always incrementing id handler
here is an example to create a new Event that takes multiline data and join it in a one
line string seperated with semi-colons.
class OneLineEvent(Event):
ACTIONS = ["ping",Event.FINISH]
"""Property to enable multiline output of the value"""
def get_value(self):
# replace carriage returns by semi-colons
# this method shall always return a list (even if one value)
return [";".join([line for line in self._value.split('\n')])]
value = property(get_value,set_value)
And now, I want to add basic id support to OneLineEvent, in OneLineIdEvent,
nothing is easier :
class OneLineIdEvent(OneLineEvent,IdEvent):
id = property(IdEvent.get_value)
Or if I want the id to be a timestamp:
import time
class OneLineTimeStampEvent(OneLineEvent):
id = property(lambda s: "%f" % (time.time(),))
You can change the behaviour of a few things in a Event-based class:
- Event.LISTEN contains the GET action to open a connection (per default "poll")
- Event.FINISH contains the POST action to close a connection (per default "close")
- Event.RETRY contains the POST action to define the timeout after reconnecting on network disconnection (per default "0", which means disabled)
- in the Event.ACTIONS list, you define what POST actions are allowed, per default, only Event.FINISH is allowed.
- Event.content_type contains the "content_type" that will be asked for every form (it is not enforced).
To change the way events are generated, you can directly call EventSourceHandler.buffer_event()
to create a new event to be sent. But the post action is best, at least while WSGI can't handle
correctly long polling connections.
Licensing
---------
Python Event Source Library
(c) 2012 Bernard Pratz
Patches by Ian Whyman and Коренберг Марк
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
EOF
See http://packages.python.org/eventsource/ for the full documentation
See https://github.com/guyzmo/event-source-library for latest sources and for patches
News
====
version 1.1.0:
* syntax clean up
* in listener:
* removed events buffering as a deque
* added toro dependency
* added toro.asyncresult to replace event's deque buffer
* used toro.asyncresult to block and call the event loop
* in client:
* added keep alive parameter
version 1.0.5:
* moved event_loop callback rearming away
* changed events buffer from list to deque
version 1.0.4:
* added invalid SSL certificates support
* integrated patches from Ian Whyman:
* support of bad HTTP services implementations (be compatible with '\n' and '\r\n')
* added user/password support for connections in client
* integrated patches from Коренберг Марк:
* use of json module of tornado
* cosmetic changes
version 1.0.3:
* added support for SSL connections
=======================================
This library implements W3C Draft's on event-source:
- http://dev.w3.org/html5/eventsource/
It enables a halfduplex communication from server to client, but initiated
by the client, through standard HTTP(S) communication.
Dependances
-----------
- Fairly recent python (tested with 2.7)
- Fairly recent tornado (tested with 2.2.1)
Usage
-----
1. Launch the server:
eventsource_listener -P 8888 -i -k 50000
2. Launch the client:
eventsource_client 42:42:42:42:42:42 -r 5000
3. Send requests:
eventsource_request 42:42:42:42:42:42 ping "42"
eventsource_request 42:42:42:42:42:42 close
Command Line arguments
----------------------
- `eventsource/listener.py` or `eventsource_server`:
usage: eventsource/listener.py [-h] [-H HOST] [-P PORT] [-d]
[-j] [-k KEEPALIVE] [-i]
Event Source Listener
optional arguments:
-h, --help show this help message and exit
-H HOST, --host HOST Host to bind on
-P PORT, --port PORT Port to bind on
-d, --debug enables debug output
-j, --json to enable JSON Event
-k KEEPALIVE, --keepalive KEEPALIVE
Keepalive timeout
-i, --id to generate identifiers
- `eventsource/client.py` or `eventsource_client`:
usage: eventsource/client.py [-h] [-H HOST] [-P PORT] [-d]
[-r RETRY]
token
Event Source Client
positional arguments:
token Token to be used for connection
optional arguments:
-h, --help show this help message and exit
-H HOST, --host HOST Host to connect to
-P PORT, --port PORT Port to be used connection
-d, --debug enables debug output
-r RETRY, --retry RETRY
Reconnection timeout
- `eventsource/send_request.py` or `eventsource_request`:
usage: eventsource/send_request.py [-h] [-H HOST] [-P PORT] [-j]
token action [data]
Generates event for Event Source Library
positional arguments:
token Token to be used for connection
action Action to send
data Data to be sent
optional arguments:
-h, --help show this help message and exit
-H HOST, --host HOST Host to connect to
-P PORT, --port PORT Port to be used connection
-j, --json Treat data as JSON
Integrate
---------
On the server side, basically all you have to do is to add the following to your code:
from eventsource import listener
application = tornado.web.Application([
(r"/(.*)/(.*)", listener.EventSourceHandler,
dict(event_class=EVENT,
keepalive=KEEPALIVE)),
])
application.listen(PORT)
tornado.ioloop.IOLoop.instance().start()
where:
- PORT is an integer for the port to bind to
- KEEPALIVE is an integer for the timeout between two keepalive messages (to protect from disconnections)
- EVENT is a eventsource.listener.Event based class, either one you made or
- eventsource.listener.StringEvent : Each event gets and resends multiline strings
- eventsource.listener.StringIdEvent : Each event gets and resends multiline strings, with an unique id for each event
- eventsource.listener.JSONEvent : Each event gets and resends JSON valid strings
- eventsource.listener.JSONIdEvent : Each event gets and resends JSON valid string, with an unique id for each event
Extend
------
To extend the behaviour of the event source library, without breaking eventsource
definition, the Event based classes implements all processing elements that shall
be done on events.
There is two abstract classes that defines Event:
- eventsource.listener.Event : defines the constructor of an Event
- eventsource.listener.EventId : defines an always incrementing id handler
here is an example to create a new Event that takes multiline data and join it in a one
line string seperated with semi-colons.
class OneLineEvent(Event):
ACTIONS = ["ping",Event.FINISH]
"""Property to enable multiline output of the value"""
def get_value(self):
# replace carriage returns by semi-colons
# this method shall always return a list (even if one value)
return [";".join([line for line in self._value.split('\n')])]
value = property(get_value,set_value)
And now, I want to add basic id support to OneLineEvent, in OneLineIdEvent,
nothing is easier :
class OneLineIdEvent(OneLineEvent,IdEvent):
id = property(IdEvent.get_value)
Or if I want the id to be a timestamp:
import time
class OneLineTimeStampEvent(OneLineEvent):
id = property(lambda s: "%f" % (time.time(),))
You can change the behaviour of a few things in a Event-based class:
- Event.LISTEN contains the GET action to open a connection (per default "poll")
- Event.FINISH contains the POST action to close a connection (per default "close")
- Event.RETRY contains the POST action to define the timeout after reconnecting on network disconnection (per default "0", which means disabled)
- in the Event.ACTIONS list, you define what POST actions are allowed, per default, only Event.FINISH is allowed.
- Event.content_type contains the "content_type" that will be asked for every form (it is not enforced).
To change the way events are generated, you can directly call EventSourceHandler.buffer_event()
to create a new event to be sent. But the post action is best, at least while WSGI can't handle
correctly long polling connections.
Licensing
---------
Python Event Source Library
(c) 2012 Bernard Pratz
Patches by Ian Whyman and Коренберг Марк
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
EOF
See http://packages.python.org/eventsource/ for the full documentation
See https://github.com/guyzmo/event-source-library for latest sources and for patches
News
====
version 1.1.0:
* syntax clean up
* in listener:
* removed events buffering as a deque
* added toro dependency
* added toro.asyncresult to replace event's deque buffer
* used toro.asyncresult to block and call the event loop
* in client:
* added keep alive parameter
version 1.0.5:
* moved event_loop callback rearming away
* changed events buffer from list to deque
version 1.0.4:
* added invalid SSL certificates support
* integrated patches from Ian Whyman:
* support of bad HTTP services implementations (be compatible with '\n' and '\r\n')
* added user/password support for connections in client
* integrated patches from Коренберг Марк:
* use of json module of tornado
* cosmetic changes
version 1.0.3:
* added support for SSL connections
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
eventsource-1.1.1.tar.gz
(10.6 kB
view details)
Built Distribution
eventsource-1.1.1-py2.6.egg
(25.0 kB
view details)
File details
Details for the file eventsource-1.1.1.tar.gz
.
File metadata
- Download URL: eventsource-1.1.1.tar.gz
- Upload date:
- Size: 10.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d18715ac4e38fa6f1d88b6ce717332630e1a5ca2c9787587353831f0d6aeb2ef |
|
MD5 | de348367582da8fc8f7699d159fd6655 |
|
BLAKE2b-256 | 03b8137d9a7358b69052a54f2adccf2087be789ece2082b35a0e5d432ef33891 |
File details
Details for the file eventsource-1.1.1-py2.6.egg
.
File metadata
- Download URL: eventsource-1.1.1-py2.6.egg
- Upload date:
- Size: 25.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b504cea5e972727305a14c5ca9e7279dc33ebb0c723f089d52efdfeafd2f8176 |
|
MD5 | 4bb3ca2883d53bdaade97f53605e8dab |
|
BLAKE2b-256 | b9d811c896f173c5119a2f2cdc9d290278a9318c141b8276b829d2c29c9edfa0 |