This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updating pubsub reader code; moving client code to pubsubclient folder
- Loading branch information
Showing
8 changed files
with
113 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
pubsub | ||
====== | ||
|
||
pubsub (short for publish/subscribe) is a server that brokers new messages | ||
to all connected subscribers at the time a message is received. | ||
http://en.wikipedia.org/wiki/Publish/subscribe | ||
|
||
|
||
API endpoints: | ||
|
||
* /pub | ||
parameter: body | ||
|
||
* /sub | ||
request parameter: multipart=(1|0). turns on/off chunked response format (on by default) | ||
long lived connection which will stream back new messages. | ||
|
||
* /stats | ||
request parameter: reset=1 (resets the counters since last reset) | ||
response: Active connections, Total connections, Messages received, Messages sent, Kicked clients. | ||
|
||
* /clients | ||
response: list of remote clients, their connect time, and their current outbound buffer size. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
An example pubsub python client built upon the tornado iostream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import tornado.iostream | ||
import tornado.ioloop | ||
import socket | ||
import logging | ||
|
||
class HTTPError(Exception): | ||
def __init__(self, code, msg=None): | ||
self.code = code | ||
self.msg = msg | ||
super(HTTPError, self).__init__('%s %s' % (code , msg)) | ||
|
||
class PubsubReader(object): | ||
def __init__(self, io_loop=None): | ||
self.io_loop = io_loop or tornado.ioloop.IOLoop.instance() | ||
self.socket = None | ||
|
||
def _callback(self, data): | ||
try: | ||
self.callback(data) | ||
except: | ||
logging.exception('failed in callback') | ||
self.stream.read_until('\n', self._callback) | ||
|
||
def callback(self, data): | ||
raise | ||
|
||
def close(self): | ||
logging.info('closed') | ||
self.io_loop.stop() | ||
|
||
def http_get_line(self): | ||
return "GET /sub?multipart=0 HTTP/1.0\r\n\r\n" | ||
|
||
def open(self, host, port=80): | ||
self.host = host | ||
self.port = port | ||
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) | ||
logging.info('opening socket to %s:%s' % (host, port)) | ||
self.socket.connect((host, port)) | ||
self.stream = tornado.iostream.IOStream(self.socket) | ||
self.stream.set_close_callback(self.close) | ||
get_line = self.http_get_line() | ||
logging.info(get_line) | ||
self.stream.write(get_line) | ||
self.stream.read_until("\r\n\r\n", self.on_headers) | ||
|
||
def on_headers(self, data): | ||
headers = {} | ||
lines = data.split("\r\n") | ||
logging.info(lines) | ||
status_line = lines[0] | ||
if status_line.count(' ') < 2: | ||
raise HTTPError(599, 'connect error') | ||
status_code = status_line.split(' ', 2)[1] | ||
if status_code != "200": | ||
raise HTTPError(status_code) | ||
for line in lines[1:]: | ||
parts = line.split(":") | ||
if len(parts) == 2: | ||
headers[parts[0].strip()] = parts[1].strip() | ||
self.stream.read_until('\n', self._callback) | ||
|
14 changes: 14 additions & 0 deletions
14
pubsubclient/pubsubclient_python_tornado/pubsub_reader_client.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import logging | ||
import tornado.options | ||
import pubsub_reader | ||
|
||
class MyReader(pubsub_reader.PubsubReader): | ||
def callback(self, data): | ||
"""handle each chunk of data from the pubsub server""" | ||
logging.info(data) | ||
|
||
if __name__ == "__main__": | ||
tornado.options.parse_command_line() | ||
reader = MyReader(); | ||
reader.open('127.0.0.1', 80) | ||
reader.io_loop.start() |
File renamed without changes.