Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Created on Apr 13, 2011
- @author: rudd-o
- '''
- from gevent import http
- import time
- subscribers = {}
- separator = """--00MsgBus00\n"""
- def hello_world(request):
- uri = request.uri
- if uri not in subscribers: subscribers[uri] = []
- bucket = subscribers[request.uri]
- if request.type == 0:
- bucket.append(request)
- request.add_output_header('Content-Type', 'multipart/x-mixed-replace;boundary=00MsgBus00')
- request.send_reply_start(200, "OK")
- request.send_reply_chunk(separator)
- elif request.type == 1:
- contenttype = request.find_input_header("Content-Type")
- data = request.input_buffer.read(-1)
- chunk = "\n".join([
- 'Content-Type: %s'%(contenttype),
- 'Content-Length: %s\n'%(len(data)),
- data
- ])
- for subscriber in bucket[:]:
- #try:
- subscriber.send_reply_chunk(chunk)
- subscriber.send_reply_chunk(separator)
- #except http.core.HttpRequestDeleted:
- #print "Removing subscriber to",uri
- #bucket.remove(subscriber)
- request.add_output_header('Content-Type', 'text/plain')
- request.add_output_header('Connection', 'close')
- request.send_reply(200, "OK", '')
- else:
- assert 0
- class ContinuousHTTPServer(http.HTTPServer):
- def __init__(self,listener,handle):
- h = [("Server","libeventmsgbus")]
- http.HTTPServer.__init__(self,listener,handle,default_response_headers=h)
- if __name__ == '__main__':
- print 'Serving on 8888...'
- ContinuousHTTPServer(('0.0.0.0', 8888), hello_world).serve_forever()
Add Comment
Please, Sign In to add comment