Guest User

Untitled

a guest
Jan 12th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. '''
  2. Created on Apr 13, 2011
  3.  
  4. @author: rudd-o
  5. '''
  6.  
  7. from gevent import http
  8. import time
  9.  
  10. subscribers = {}
  11. separator = """--00MsgBus00\n"""
  12.  
  13. def hello_world(request):
  14. uri = request.uri
  15. if uri not in subscribers: subscribers[uri] = []
  16. bucket = subscribers[request.uri]
  17. if request.type == 0:
  18. bucket.append(request)
  19. request.add_output_header('Content-Type', 'multipart/x-mixed-replace;boundary=00MsgBus00')
  20. request.send_reply_start(200, "OK")
  21. request.send_reply_chunk(separator)
  22. elif request.type == 1:
  23. contenttype = request.find_input_header("Content-Type")
  24. data = request.input_buffer.read(-1)
  25. chunk = "\n".join([
  26. 'Content-Type: %s'%(contenttype),
  27. 'Content-Length: %s\n'%(len(data)),
  28. data
  29. ])
  30. for subscriber in bucket[:]:
  31. #try:
  32. subscriber.send_reply_chunk(chunk)
  33. subscriber.send_reply_chunk(separator)
  34. #except http.core.HttpRequestDeleted:
  35. #print "Removing subscriber to",uri
  36. #bucket.remove(subscriber)
  37. request.add_output_header('Content-Type', 'text/plain')
  38. request.add_output_header('Connection', 'close')
  39. request.send_reply(200, "OK", '')
  40. else:
  41. assert 0
  42.  
  43. class ContinuousHTTPServer(http.HTTPServer):
  44. def __init__(self,listener,handle):
  45. h = [("Server","libeventmsgbus")]
  46. http.HTTPServer.__init__(self,listener,handle,default_response_headers=h)
  47.  
  48. if __name__ == '__main__':
  49. print 'Serving on 8888...'
  50. ContinuousHTTPServer(('0.0.0.0', 8888), hello_world).serve_forever()
Add Comment
Please, Sign In to add comment