def main(): global _REDIS_CLIENT monkey.patch_all() gevent.spawn(monitor_redis) _REDIS_CLIENT = redis.StrictRedis(host=_REDIS_HOST, port=_REDIS_PORT) router = wsgi_helpers.Router( [ ("/", wsgi_helpers.handle_file("views/index.htm")), ("/websocket", handle_websocket), ("/static/.+", wsgi_helpers.handle_static("/static")), ] ) print "SERVING ON %s:%d" % (_HOSTNAME, _PORT) server = WSGIServer((_HOSTNAME, _PORT), router, handler_class=WebSocketHandler) server.serve_forever()
def handle_websocket(environ, start_response): """Simple broadcast websocket handler.""" websocket = environ["wsgi.websocket"] try: _SUBSCRIBERS.append(websocket) # Just hang around waiting for messages -- # If this was bidirectional, we'd do receive() # calls here. while True: gevent.sleep(1) finally: _SUBSCRIBERS.remove(websocket) router = wsgi_helpers.Router([ ("/", wsgi_helpers.handle_file("views/index.htm")), ("/favicon.ico", wsgi_helpers.handle_file("static/favicon.ico")), ("/websocket", handle_websocket), ("/static/.*", wsgi_helpers.handle_static("/static")) ]) def authorization_header(): raw_string = "%s:%s" % (USERNAME, PASSWORD) b64_string = base64.b64encode(raw_string) auth_header = "Basic %s" % (b64_string) return auth_header if __name__ == "__main__": port = int(os.environ.get("PORT", 8081))