def start(self): self.router = SockJSRouter(PubSubConnection, '/pubsub', user_settings={'master': self}) app = web.Application(self.router.urls) app.listen(9000) for monitor in self.monitors: t = threading.Thread(target=monitor, args=(self, )) t.daemon = True t.start() t = threading.Thread(target=self.ioloop.start) t.daemon = True t.start() logging.info('Realtime listener started.') # Returning control to the parent application so we can catch keyword # interrupts. try: while 1: t.join(1) if not t.isAlive(): break except KeyboardInterrupt: print '\nExiting...' sys.exit(1)
def main(): import core # turn on heavy debug logging #setup_logging() try: SockRouter = SockJSRouter(Status) application = Application( SockRouter.apply_routes([(r"/", IndexHandler), (r"/record", RecordHandler), (r"/resetPeaks", ResetPeaksHandler), (r"/resetBTimer", ResetBTimerHandler), ( r"/flac/(.*)", tornado.web.StaticFileHandler, { "path": "." }, )]), static_path=os.path.join(os.path.dirname(__file__), "static"), debug=sys.DEV_MODE) if not sys.DEV_MODE: port = 80 else: port = 8080 application.listen(port) ioloop.IOLoop.instance().start() finally: core.port.stop() core.port.waitTillFinished()
def run_tornado(addr, port, *args, **kwargs): """ Starts the tornado webserver as wsgi server for OpenSlides. It runs in one thread. """ # Don't try to read the command line args from openslides parse_command_line(args=[]) # Setup WSGIContainer app = WSGIContainer(get_wsgi_application()) # Collect urls from openslides.core.chatbox import ChatboxSocketHandler chatbox_socket_js_router = SockJSRouter(ChatboxSocketHandler, '/core/chatbox') sock_js_router = SockJSRouter(OpenSlidesSockJSConnection, '/sockjs') other_urls = [ (r"%s(.*)" % settings.STATIC_URL, DjangoStaticFileHandler), (r'%s(.*)' % settings.MEDIA_URL, StaticFileHandler, {'path': settings.MEDIA_ROOT}), ('.*', FallbackHandler, dict(fallback=app))] # Start the application debug = settings.DEBUG tornado_app = Application(sock_js_router.urls + chatbox_socket_js_router.urls + other_urls, autoreload=debug, debug=debug) server = HTTPServer(tornado_app) server.listen(port=port, address=addr) IOLoop.instance().start()
def __init__(self, **kwargs): if options.baseurl is None: raise ValueError('Base URL must be configured') router = SockJSRouter(SearchConnection, '/search', self.sockjs_settings) # Allow connections to find the application router.application = self handlers = list(self.handlers) router.apply_routes(handlers) settings = dict(self.app_settings) settings.update(kwargs) tornado.web.Application.__init__(self, handlers, **settings) if not os.path.isdir(options.blob_cache_dir): os.makedirs(options.blob_cache_dir, 0700) self.blob_cache = BlobCache(options.blob_cache_dir) self.search_cache = SearchCache(options.search_cache_dir) self._pruner = threading.Thread(target=self._prune_cache_thread, name='prune-cache') self._pruner.daemon = True self._pruner.start()
def __init__(self): SockJSRoutes = SockJSRouter(RealtimeConnection, '/realtime') settings = dict( template_path = os.path.join(os.path.dirname(__file__), "templates"), static_path = os.path.join(os.path.dirname(__file__), "static"), cookie_secret = TempoCommon.ConfigData['CookieSecret'], login_url = "/auth/login", debug = TempoCommon.ConfigData['Debug'] ) handlers = [ (r"/", HomeHandler), (r"/upload", UploadHandler), (r"/uploadengine", UploadEngineHandler), (r'/uploads/(.*)', tornado.web.StaticFileHandler, {'path': os.path.join(os.path.dirname(__file__), "uploads")}), (r"/api/asset/([^/]+){0,1}", API_AssetHandler), (r"/api/assets", API_AssetSearchHandler), (r"/api/assetfields", API_AssetFieldsHandler), (r"/api/folders", API_FolderListingHandler), (r"/api/quickplay", API_QuickPlay), (r"/auth/login", AuthLoginHandler), (r"/auth/logout", AuthLogoutHandler) ] + SockJSRoutes.urls tornado.web.Application.__init__(self, handlers, **settings) SockJSRoutes.app = self
def main(): import core # turn on heavy debug logging #setup_logging() try: SockRouter = SockJSRouter(Status) application = Application(SockRouter.apply_routes([(r"/", IndexHandler), (r"/record", RecordHandler), (r"/resetPeaks", ResetPeaksHandler), (r"/resetBTimer", ResetBTimerHandler), (r"/flac/(.*)",tornado.web.StaticFileHandler, {"path": "."},)]), static_path = os.path.join(os.path.dirname(__file__), "static"), debug = sys.DEV_MODE ) if not sys.DEV_MODE: port = 80 else: port = 8080 application.listen(port) ioloop.IOLoop.instance().start() finally: core.port.stop() core.port.waitTillFinished()
def main(): SocketRouter = SockJSRouter(SocketConnection, '/socket') AppRouter = SockJSRouter(AppSocketConnection, '/app') app = tornado.web.Application( SocketRouter.urls + AppRouter.urls + [ (r"/", Mainhandler), (r"/dev", Devhandler), (r"/user/([0-9]+)/([0-9]+)", Userhandler), (r"/login", LoginHandler), (r"/logout", LogoutHandler), (r"/signup", SignupHandler), ], template_path=os.path.join(os.path.dirname(__file__), "templates"), static_path=os.path.join(os.path.dirname(__file__), "static"), cookie_secret="98RaTgemQjS/zVMMFr8oZ35z9S1UsEaGgl/E3cpxm/E=", login_url="/login", xsrf_cookies="True", degug="True", websocket_ping_interval=40, websocket_ping_timeout=120, ) serverDB.initDatabase() http_server = tornado.httpserver.HTTPServer(app) http_server.listen(os.environ.get('PORT')) tornado.ioloop.IOLoop.instance().start()
def main(host_config_filename, targets_config_filename): config = application_config.load_config(host_config_filename, targets_config_filename) application_logger.initialize_logger(config['logging']) host = config['host'] port = int(config['port']) upload_folder = config['upload_folder'] file_stash_folder = config['file_stash_folder'] queue_folder = config['queue_folder'] num_distribution_processes = int(config['num_distribution_processes']) max_file_age_days = int(config['max_file_age_days']) max_file_age_check_interval_seconds = int(config['max_file_age_check_interval_seconds']) max_task_execution_time_seconds = int(config['max_task_execution_time_seconds']) max_task_finalization_time_seconds = int(config['max_task_finalization_time_seconds']) task_cleanup_interval_seconds = int(config['task_cleanup_interval_seconds']) max_task_wait_seconds = int(config['max_task_wait_seconds']) max_task_exist_days = int(config['max_task_exist_days']) root = Flask(__name__) root.config['host_description'] = config['host_description'] root.config['SEND_FILE_MAX_AGE_DEFAULT'] = 1 sendor_queue = SendorQueue(num_distribution_processes, queue_folder, max_task_execution_time_seconds, max_task_finalization_time_seconds, task_cleanup_interval_seconds, max_task_wait_seconds, max_task_exist_days) file_stash = FileStash(file_stash_folder, max_file_age_days, max_file_age_check_interval_seconds) targets = Targets(config['targets']) ui_app = ui.create_ui(file_stash, upload_folder) root.register_blueprint(url_prefix = '/ui', blueprint = ui_app) rest_api_app = FileDistribution.rest_api.create_rest_api(sendor_queue, targets, file_stash) root.register_blueprint(url_prefix = '/api', blueprint = rest_api_app) FileDistribution.backsync_api.create_backsync_api(sendor_queue, targets, file_stash) @root.route('/') @root.route('/index.html') def index(): return redirect('ui') logger.info("Starting wsgi server") wsgi_root = tornado.wsgi.WSGIContainer(root) backsyncRouter = SockJSRouter(BacksyncModelRouter, '/backsync') handlers = [] backsyncRouter.apply_routes(handlers) handlers.extend([(r".*", tornado.web.FallbackHandler, dict(fallback=wsgi_root))]) application = tornado.web.Application(handlers) application.listen(port) tornado.ioloop.IOLoop.instance().start()
def __init__(self, settings): handlers = [ tornado.web.url( r'/', MainHandler, name="main" ), tornado.web.url( r'/project/create$', ProjectCreateHandler, name="project_create" ), tornado.web.url( r'/project/([^/]+)/settings/([^/]+)$', ProjectSettingsHandler, name="project_settings" ), tornado.web.url( r'/rpc/([^/]+)$', RpcHandler, name="store" ), tornado.web.url( r'/auth$', AuthHandler, name="auth" ), tornado.web.url( r'/logout$', LogoutHandler, name="logout" ) ] # create SockJS route for admin connections AdminConnectionRouter = SockJSRouter( AdminSocketHandler, '/socket' ) handlers = AdminConnectionRouter.urls + handlers # create SockJS route for client connections SockjsConnectionRouter = SockJSRouter( SockjsConnection, '/connection' ) handlers = SockjsConnectionRouter.urls + handlers # match everything else to 404 handler handlers.append( tornado.web.url( r'.*', Http404Handler, name='http404' ) ) AdminSocketHandler.application = self SockjsConnection.application = self tornado.web.Application.__init__( self, handlers, **settings )
def start_warrior_server(warrior, bind_address="localhost", port_number=8001, http_username=None, http_password=None): '''Starts the warrior web interface.''' SeesawConnection.warrior = warrior warrior.on_projects_loaded += SeesawConnection.handle_projects_loaded warrior.on_project_refresh += SeesawConnection.handle_project_refresh warrior.on_project_installing += SeesawConnection.handle_project_installing warrior.on_project_installed += SeesawConnection.handle_project_installed warrior.on_project_installation_failed += \ SeesawConnection.handle_project_installation_failed warrior.on_project_selected += SeesawConnection.handle_project_selected warrior.on_broadcast_message_received += SeesawConnection.handle_broadcast_message warrior.on_status += SeesawConnection.handle_warrior_status warrior.runner.on_pipeline_start_item += SeesawConnection.handle_start_item warrior.runner.on_pipeline_finish_item += \ SeesawConnection.handle_finish_item warrior.runner.on_status += SeesawConnection.handle_runner_status if not http_username: http_username = warrior.http_username if not http_password: http_password = warrior.http_password ioloop.PeriodicCallback(SeesawConnection.broadcast_bandwidth, 1000).start() ioloop.PeriodicCallback(SeesawConnection.broadcast_timestamp, 1000).start() router = SockJSRouter(SeesawConnection) application = web.Application( router.apply_routes([(r"/(.*\.(html|css|js|swf|png|ico))$", web.StaticFileHandler, { "path": PUBLIC_PATH }), ("/", IndexHandler), ("/api/(.+)$", ApiHandler, { "warrior": warrior })]), # flash_policy_port = 843, # flash_policy_file = os.path.join(PUBLIC_PATH, "flashpolicy.xml"), socket_io_address=bind_address, socket_io_port=port_number, # settings for AuthenticatedApplication auth_enabled=lambda: (realize(http_password) or "").strip() != "", check_auth=lambda r, username, password: (password == realize(http_password) and (realize(http_username) or "").strip() in ["", username]), auth_realm="ArchiveTeam Warrior", skip_auth=[]) application.listen(port_number, bind_address)
def getHandlers(): """ Return a list of the handlers for the jLog target. """ import socket ip = socket.gethostbyname(socket.gethostname()) uri = 'tcp://%s:13002' % ip server = SockJSRouter(LoggingConnection, '/jLog') server.zmq_subscriber = ZMQSubscriber(uri=uri) return [ (r'/jLog', IndexHandler), ] + server.urls
def __init__(self, handler, baseurl, port): self.port = port self.baseurl = baseurl router = SockJSRouter(handler, self.baseurl) # Meteor doesn't obey sockjs url conventions so we patch it on our end to work around newurls = [] for url in router._transport_urls: if "websocket" in url[0]: newurls.append(url) newurls.append((url[0].replace(self.baseurl, self.baseurl + '/sockjs'), url[1], url[2])) router._transport_urls = newurls self.application = tornado.web.Application(router.urls)
def start_warrior_server(warrior, bind_address="localhost", port_number=8001, http_username=None, http_password=None): '''Starts the warrior web interface.''' SeesawConnection.warrior = warrior warrior.on_projects_loaded += SeesawConnection.handle_projects_loaded warrior.on_project_refresh += SeesawConnection.handle_project_refresh warrior.on_project_installing += SeesawConnection.handle_project_installing warrior.on_project_installed += SeesawConnection.handle_project_installed warrior.on_project_installation_failed += \ SeesawConnection.handle_project_installation_failed warrior.on_project_selected += SeesawConnection.handle_project_selected warrior.on_broadcast_message_received += SeesawConnection.handle_broadcast_message warrior.on_status += SeesawConnection.handle_warrior_status warrior.runner.on_pipeline_start_item += SeesawConnection.handle_start_item warrior.runner.on_pipeline_finish_item += \ SeesawConnection.handle_finish_item warrior.runner.on_status += SeesawConnection.handle_runner_status if not http_username: http_username = warrior.http_username if not http_password: http_password = warrior.http_password ioloop.PeriodicCallback(SeesawConnection.broadcast_bandwidth, 1000).start() ioloop.PeriodicCallback(SeesawConnection.broadcast_timestamp, 1000).start() router = SockJSRouter(SeesawConnection) application = web.Application( router.apply_routes([ (r"/(.*\.(html|css|js|swf|png|ico))$", web.StaticFileHandler, {"path": PUBLIC_PATH}), ("/", IndexHandler), ("/api/(.+)$", ApiHandler, {"warrior": warrior})]), # flash_policy_port = 843, # flash_policy_file = os.path.join(PUBLIC_PATH, "flashpolicy.xml"), socket_io_address=bind_address, socket_io_port=port_number, # settings for AuthenticatedApplication auth_enabled=lambda: (realize(http_password) or "").strip() != "", check_auth=lambda r, username, password: ( password == realize(http_password) and (realize(http_username) or "").strip() in ["", username] ), auth_realm="ArchiveTeam Warrior", skip_auth=[] ) application.listen(port_number, bind_address)
def __init__(self, socket_path="/updates", websockets=True, subscriptions=None, redis_host="127.0.0.1", redis_port=6379, exit_gracefully=True, debug=False): """ RedisVisualization server. Servers updates from redis using websockets and also provide static files using Tornado. Inputs ------ @socket_path str : where should client ask for redis updates (default = '/updates') @websockets bool : use websockets ? (default = True) @subscriptions list<str> : what to subscribe to on Redis (default=['namespace_*', 'updates_*']) @redis_host str : what hostname is redis server on (default='127.0.0.1') @redis_port int : what port to listen to for redis server (default=6379) @exit_gracefully bool : capture SIGINT event & shut down server from ioloop (default=True) """ self.exit_gracefully = exit_gracefully if subscriptions is None: subscriptions = ["updates_*"] # 2. Start a connection pool to redis: pool = tornadoredis.ConnectionPool(host=redis_host, port=redis_port) self.clients = tornadoredis.Client(connection_pool=pool, password="") init_redis(tornadoredis.Client(connection_pool=pool, password="")) self.clients.connect() get_redis().connect() # make sure redis reports expiration and set events: try: self.clients.psubscribe( subscriptions, lambda msg: self.clients.listen(Connection.pubsub_message)) except tornadoredis.exceptions.ConnectionError: print(""" Could not connect to Redis. Start server with: > redis-server """) signal_handler(None, None) try_exit() if not websockets: Router = SockJSRouter(Connection, socket_path, dict(disabled_transports=['websocket'])) else: Router = SockJSRouter(Connection, socket_path) # 4. Creater router for http + sockets: self.App = tornado.web.Application( generate_routes(debug) + Router.urls)
def main(argv=None): if argv is None: argv = sys.argv # currently only allow one command-line argument, the port to run on. logging.getLogger().setLevel(logging.DEBUG) port = int(argv[1]) if (len(argv) > 1) else settings.WEBSOCKET_PORT ParticipantRouter = SockJSRouter(ParticipantConnection, '%s/participant' % settings.WEBSOCKET_URI) ExperimenterRouter = SockJSRouter(ExperimenterConnection, '%s/experimenter' % settings.WEBSOCKET_URI) urls = list(chain.from_iterable([ParticipantRouter.urls, ExperimenterRouter.urls])) app = web.Application(urls) logger.info("starting sockjs server on port %s", port) app.listen(port) if getattr(settings, 'RAVEN_CONFIG', None): app.sentry_client = AsyncSentryClient(settings.RAVEN_CONFIG['dsn']) ioloop.IOLoop.instance().start()
def create_application_handlers(sockjs_settings): handlers = [ tornado.web.url(r'/api/([^/]+)/?$', ApiHandler, name="api"), tornado.web.url(r'/info/$', InfoHandler, name="info"), tornado.web.url(r'/action/$', ActionHandler, name="action"), tornado.web.url(r'/auth/$', AuthHandler, name="auth"), (r'/socket', AdminWebSocketHandler), ] if options.web: logger.info("serving web application from {0}".format( os.path.abspath(options.web))) handlers.append((r'/(.*)', tornado.web.StaticFileHandler, { "path": options.web, "default_filename": "index.html" })) # create SockJS route for client connections client_sock_router = SockJSRouter(SockjsConnection, '/connection', user_settings=sockjs_settings) handlers = client_sock_router.urls + handlers return handlers
def handle(self, **options): if len(settings.SOCKJS_CLASSES) > 1: from django.core.exceptions import ImproperlyConfigured raise ImproperlyConfigured( "Multiple connections not yet supported") module_name, cls_name = settings.SOCKJS_CLASSES[0].rsplit('.', 1) module = import_module(module_name) cls = getattr(module, cls_name) channel = getattr(settings, 'SOCKJS_CHANNEL', '/echo') if not channel.startswith('/'): channel = '/%s' % channel router = SockJSRouter(cls, channel) app_settings = { 'debug': settings.DEBUG, } PORT = 9999 app = web.Application(router.urls, **app_settings) app.listen(PORT, no_keep_alive=False) print "Running sock app on port", PORT, "with channel", channel try: ioloop.IOLoop.instance().start() except KeyboardInterrupt: # so you don't think you errored when ^C'ing out pass
def handle(self, *args, **options): # connect to redis url = urlparse.urlparse(settings.REDIS_URL) pool = tornadoredis.ConnectionPool(host=url.hostname, port=url.port) redis_connection = tornadoredis.Client(connection_pool=pool, password=url.password) redis_connection.connect() sys.stdout.write("\nConnected to Redis ({}) \n".format(url)) redis_connection.psubscribe( "*", lambda message: redis_connection.listen( connections.MainConnection.pubsub_message)) # enable sockjs routing router = SockJSRouter( connections.MainConnection, '/chat', dict(disabled_transports=DISABLED_TRANSPORTS), ) app = web.Application( router.urls, "0.0.0.0", ) app.listen(IO_PORT) sys.stdout.write("\nApp listening at port {}\n".format(IO_PORT)) sys.stdout.flush() ioloop.IOLoop.instance().start()
def start(self): self.router = SockJSRouter(PubSubConnection, "/pubsub", user_settings={"master": self}) app = web.Application(self.router.urls) app.listen(9000) for monitor in self.monitors: t = threading.Thread(target=monitor, args=(self,)) t.daemon = True t.start() t = threading.Thread(target=self.ioloop.start) t.daemon = True t.start() logging.info("Realtime listener started.") # Returning control to the parent application so we can catch keyword # interrupts. try: while 1: t.join(1) if not t.isAlive(): break except KeyboardInterrupt: print "\nExiting..." sys.exit(1)
def run_app(): # configure logging level if settings.VERBOSE: logger.setLevel(logging.DEBUG) else: logger.setLevel(logging.INFO) ThunderRouter = SockJSRouter(ThunderSocketHandler, "/connect") urls = ThunderRouter.urls + api.urls application = tornado.web.Application(urls, settings.DEBUG) ss = SortingStation.instance() # Single-client only at the moment. ss.create_messenger(settings.APIKEY, settings.APISECRET) logger.info("Starting Thunderpush server at %s:%d", settings.HOST, settings.PORT) application.listen(settings.PORT, settings.HOST) try: tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: logger.info("Shutting down...")
def run(self): self.tord_static_path = os.path.join( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static'), 'tord') self.template = template.Loader(os.path.abspath(self.templates_dir)) settings.pubsub['klass'] = import_path( 'async_pubsub.%s_pubsub.%sPubSub' % (self.pubsub_klass.lower(), self.pubsub_klass)) settings.pubsub['opts'] = self.pubsub_opts self._add_http_route('%s/(.*)' % self.static_path, web.StaticFileHandler, {'path': os.path.abspath(self.static_dir)}, True) self._add_http_route('%s/tord/(.*)' % self.static_path, web.StaticFileHandler, {'path': self.tord_static_path}, True) self.app = web.Application( SockJSRouter(WebSocketHandler, self.ws_path).urls + settings.routes['http'], debug=self.debug) self.app.listen(self.port) logger.info('Listening on port %s ...' % self.port) try: ioloop.IOLoop.instance().start() except KeyboardInterrupt: pass finally: logger.info('Shutting down ...')
def start(): """Setup HTTP/WS server. **MUST** be called prior to any operation.""" StatusRouter = SockJSRouter(handlers.StatusConnection, "/data") application = tornado.web.Application( [ # FIXME daltonism workaround, should be implemented client-side (r'/(?:|blind)', handlers.HomePageHandler), (r'/log', handlers.LogPageHandler), (r'/status', handlers.StatusPageHandler), (r'/presence', handlers.PresenceForecastHandler), (r'/(info)', handlers.MarkdownPageHandler), (r'/login', handlers.LoginPageHandler), (r'/logout', handlers.LogoutPageHandler), (r'/admin', handlers.AdminPageHandler), (r'/message', handlers.MessagePageHandler), (r'/data.php', handlers.RTCHandler) ] + StatusRouter.urls, ui_modules=uimodules, gzip=True, debug=options.developer_mode, static_path=options.assets_path, xsrf_cookies=True, cookie_secret=options.cookie_secret) server = tornado.httpserver.HTTPServer(application) #TODO other options LOG.info('Starting HTTP/WS server...') bind(server, options.web_port, options.web_usocket)
def run(): router = SockJSRouter(connections.PhotoStream, settings.SOCKET_STREAMER_URL) handlers = router.urls runtime_vars = dict() runtime_vars["last_obj_count"] = None runtime_vars.update(MultiuploaderImage.objects.aggregate(last_upload=Max('upload_date'))) def db_periodic_check(*a, **kw): obj_count = MultiuploaderImage.objects.count() if obj_count != runtime_vars["last_obj_count"]: runtime_vars["last_obj_count"] = obj_count if not runtime_vars["last_upload"] is None: objs = MultiuploaderImage.objects.filter(upload_date__gt=runtime_vars["last_upload"]) runtime_vars.update(MultiuploaderImage.objects.aggregate(last_upload=Max('upload_date'))) for obj in objs: for user in connections.PhotoStream.connected_users: user.notify_new_entry(obj) app = web.Application(handlers) app.listen(int(settings.SOCKET_STREAMER_PORT), "0.0.0.0") ioloop.PeriodicCallback(db_periodic_check, 1000).start() autoreload.start(ioloop.IOLoop.instance()) print "socket streamer is (re)started" try: ioloop.IOLoop.instance().start() except KeyboardInterrupt: return
def run(self): # Setup SockJS flaskwsgi = WSGIContainer(self.flaskapp) self.socketrouter = SockJSRouter(SocketConnection, '/sockjs') tornado_app = tornado.web.Application(self.socketrouter.urls + [(r".*", tornado.web.FallbackHandler, { "fallback": flaskwsgi })]) tornado_app.listen(80) # Wake up robot Robot.wake() # Start default app startup_app = Preferences.get('general', 'startup_app', None) if startup_app in Apps.apps: self.request_handler.page_openapp(startup_app) # SSL security # http_server = tornado.httpserver.HTTPServer(tornado_app, ssl_options={ # "certfile": "/etc/ssl/certs/server.crt", # "keyfile": "/etc/ssl/private/server.key", # }) # http_server.listen(443) try: # ioloop.PeriodicCallback(UserSocketConnection.dump_stats, 1000).start() IOLoop.instance().start() except KeyboardInterrupt: print_info('Keyboard interupt')
def handle(self, **options): if len(settings.SOCKJS_CLASSES) > 1: from django.core.exceptions import ImproperlyConfigured raise ImproperlyConfigured( "Multiple connections not yet supported") module_name, cls_name = settings.SOCKJS_CLASSES[0].rsplit('.', 1) module = import_module(module_name) cls = getattr(module, cls_name) channel = getattr(settings, 'SOCKJS_CHANNEL', '/chat') if not channel.startswith('/'): channel = '/%s' % channel router = SockJSRouter(cls, channel) app_settings = { 'debug': settings.DEBUG, } port = int(options['port']) app = web.Application(router.urls, **app_settings) app.listen(port, no_keep_alive=options['no_keep_alive']) print("Running sock app on port", port, "with channel", channel) try: ioloop.IOLoop.instance().start() except KeyboardInterrupt: pass
def __init__(self, opts): #handlers = [ # (r'/point/test(.*)', TestHandler) #] + config.router + route.get_routes() self.ChatRouter = SockJSRouter(ChatConnection, '/chat') print 'self.ChatRouter=', dir(self.ChatRouter) print '_connection=', dir(self.ChatRouter._connection) handlers = [] + self.ChatRouter.urls # print ' Application:opts=', repr(opts) settings = dict( template_path=os.path.join(os.path.dirname(__file__), "templates"), static_path=os.path.join(os.path.dirname(__file__), "static"), # xsrf_cookies=True, login_url="/auth/login", autoescape=None # , #**{k: v.value() for k, v in opts.items()} ) #settings.update(opts) for k, v in opts.items(): settings[k] = v.value() #print 'settings=', repr(settings) web.Application.__init__(self, handlers, **settings) # Единое соединение с базой данных для всех обработчиков #self.db = base.DB.db(DB_URL, DB_REPLICASET) self.db = getdb(opts.mongodb_url, opts.mongodb_replicaset) # Синхронная работа через pymongo # self.syncdb = pymongo.Connection(DB_URL).navicc ioloop.PeriodicCallback(self.tick, 1000).start()
def start_runner_server(project, runner, bind_address="localhost", port_number=8001, http_username=None, http_password=None): '''Starts a web interface for a manually run pipeline. Unlike :func:`start_warrior_server`, this UI does not contain an configuration or project management panel. ''' # if bind_address == "0.0.0.0": # bind_address = "" SeesawConnection.project = project SeesawConnection.runner = runner runner.on_pipeline_start_item += SeesawConnection.handle_start_item runner.on_pipeline_finish_item += SeesawConnection.handle_finish_item runner.on_status += SeesawConnection.handle_runner_status ioloop.PeriodicCallback(SeesawConnection.broadcast_timestamp, 1000).start() router = SockJSRouter(SeesawConnection) application = web.Application( router.apply_routes([(r"/(.*\.(html|css|js|swf|png|ico))$", web.StaticFileHandler, { "path": PUBLIC_PATH }), ("/", IndexHandler), ("/api/(.+)$", ApiHandler, { "runner": runner })]), # flash_policy_port = 843, # flash_policy_file=os.path.join(PUBLIC_PATH, "flashpolicy.xml"), socket_io_address=bind_address, socket_io_port=port_number, # settings for AuthenticatedApplication auth_enabled=(http_password or "").strip() != "", check_auth=lambda r, username, password: (password == http_password and (http_username or "").strip() in ["", username]), auth_realm="ArchiveTeam Warrior", skip_auth=[]) application.listen(port_number, bind_address)
def __init__(self): self.db = db self.config = config from bcloud.handlers.host import HostHandler, HostsHandler from bcloud.handlers.index import MainHandler from bcloud.handlers.websocket import BCloudSocketHandler from bcloud.handlers.project import ProjectHandler from bcloud.handlers.websocket import WSocketHandler from sockjs.tornado import SockJSRouter from bcloud.handlers.project import ProjectsHandler handlers = [ (r"/", MainHandler), (r"/terminal/(?P<tid>.*)", TermHandler), (r"/terminal1/(?P<tid>.*)", Term1Handler), (r"/host", HostsHandler), (r"/host/(?P<hid>.*)", HostHandler), (r"/project", ProjectsHandler), (r"/project/(?P<pid>.*)", ProjectHandler), (r"/task", TasksHandler), (r"/task/(?P<tid>.*)", TaskHandler), (r"/docker", DockerHandle), ] handlers += SockJSRouter(WSocketHandler, '/ws').urls handlers += SockJSRouter(BCloudSocketHandler, '/term').urls handlers += SockJSRouter(BCloudSocket1Handler, '/term1').urls settings = dict( template_path=os.path.join(os.path.dirname(__file__), "templates"), static_path=os.path.join(os.path.dirname(__file__), "static"), ui_modules={ "Entry": EntryModule, "Sidebar": SidebarModule, "Menubar": MenubarModule, "Footer": FooterModule, 'include': TemplateModule, }, xsrf_cookies=False, cookie_secret="__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__", # login_url="/auth/login", debug=True, ) super(Application, self).__init__(handlers, **settings)
def main(): # Parse args parser = argparse.ArgumentParser(description="dead simple commits feed") parser.add_argument('-s', '--secret', type=str, default=gen_secret(18), help="auth secret") parser.add_argument('-p', '--port', type=int, default=5000, help="port") parser.add_argument('--redis-prefix', type=str, default='revfeed', help="redis prefix") parser.add_argument('--redis-host', type=str, default='localhost', help="redis host") parser.add_argument('--redis-port', type=int, default=6379, help="redis port") parser.add_argument('--clear', action='store_true', help="clear feed") args = parser.parse_args() # Setup redis connection redis_prefix = args.redis_prefix redis_conn = redis.StrictRedis(args.redis_host, args.redis_port) # Clear feed if requested if args.clear: count = 0 for key in redis_conn.keys(redis_key(redis_prefix, '*')): count += redis_conn.delete(key) logger.info("deleted {} keys".format(count)) return # Setup handlers handlers = [] handlers.append((r'/', IndexHandler, dict( redis_prefix=redis_prefix, redis_conn=redis_conn, ))) handlers.append((r'/commits', CommitsHandler, dict( secret=args.secret, redis_prefix=redis_prefix, redis_conn=redis_conn, ))) handlers.extend(SockJSRouter(NotifierConnection, '/notifier').urls) # Setup app static_path = pkg_resources.resource_filename(__package__, 'static') app = web.Application( handlers, gzip=True, static_path=static_path, log_function=log_request, ) app.listen(args.port) for a in ('secret', 'port'): logger.info("{}={!r}".format(a, getattr(args, a))) # Start IOLoop try: ioloop.IOLoop.instance().start() except (KeyboardInterrupt, SystemExit): pass
def make_app(): sock_router = SockJSRouter(WebSocketHandler, '/websocket') return Application(sock_router.urls + [ (r'/static/(.*)', StaticFileHandler, { 'path': 'static' }), url(r'/(.*)', IndexHandler), ])
def getsocks(logsock=None, cmdsock=None): """ """ socks = [] if logsock: LoggerConnection.subscriber = ZMQSubscriber(logsock) socks += SockJSRouter(LoggerConnection, '/logs').urls return socks
def make_router(): return SockJSRouter( MultiplexConnection.get( main=MainConnection, shell=ShellConnection, ), '/socket' )
def sockjs_webapp_factory(connection): """ `sockjs_webapp_factory` provied a Tornado web application with a sockjs handler for incoming connections. """ from sockjs.tornado import SockJSRouter return web.Application(SockJSRouter(connection, SERVER_BASE_URL).urls)
def start_tornado(): # logging.getLogger().setLevel(logging.DEBUG) # logging.getLogger().setLevel(logging.INFO) EchoRouter = SockJSRouter(PlansqTornadoChat, '/socket') app = web.Application(EchoRouter.urls) app.listen(config.TORNADO_PORT) ioloop.IOLoop.instance().start()
def __init__(self): options = {'disconnect_delay': 5, 'jsessionid': False, 'sockjs_url': 'https://d1fxtkz8shb9d2.cloudfront.net/sockjs-0.3.min.js'} self.WaitRouter = SockJSRouter(WaitingRoomConnection, '/sockjs/wait', options) self.GameRouter = SockJSRouter(GameConnection, '/sockjs/game', options) self.SessionRouter = SockJSRouter(SessionConnection, '/sockjs/session', options) GameConnection.ready = 0 GameConnection.size = 2 urls = [ (r'/', handlers.MainHandler), (r'/session', handlers.SessionHandler), (r'/about', RegisterHandler), (r'/quiz/user/([a-zA-Z0-9])*', handlers.QuizHandler), (r'/instructionsemployer([^/]*)', handlers.InstructionsHandler), (r'/instructionsemployee([^/]*)', handlers.Instructions2Handler), (r'/tutorial1/user/([a-zA-Z0-9])*', handlers.TutorialHandler), (r'/tutorial2/user/([a-zA-Z0-9])*', handlers.Tutorial2Handler), (r'/welcome([^/]*)', handlers.WelcomeHandler), (r'/payment([^/]*)', handlers.PaymentHandler), (r'/game/user/([a-zA-Z0-9])*', handlers.GameHandler), (r'/api/player/register([^/]*)', handlers.PlayerCreateHandler), (r'/api/player/(.*)', handlers.PlayerHandler), (r'/api/credential', handlers.CredentialHandler), (r'/experimenter/config/sync/activate/([a-zA-Z0-9]+$)', handlers.SyncExperimentLaunchHandler), (r'/admin/user', handlers.UserHandler), (r'/admin', AdminHandler) ] + self.WaitRouter.urls + self.GameRouter.urls + self.SessionRouter.urls settings = { "debug": True, "template_path": os.path.join(os.path.dirname(__file__), "templates"), "static_path": os.path.join(os.path.dirname(__file__), "static"), "cookie_secret": "__TODO:_GENERATE_RANDOM_VALUE_HERE__" } self.redis_cmd = redis.Redis(db=0, \ password='******', \ unix_socket_path='/tmp/redis.sock') self.redis_pub = Client() self.redis_pub.connect_usocket('/tmp/redis.sock', callback=self.auth) tornado.web.Application.__init__(self, urls, **settings)
def start_runner_server(project, runner, bind_address="localhost", port_number=8001, http_username=None, http_password=None): '''Starts a web interface for a manually run pipeline. Unlike :func:`start_warrior_server`, this UI does not contain an configuration or project management panel. ''' # if bind_address == "0.0.0.0": # bind_address = "" SeesawConnection.project = project SeesawConnection.runner = runner runner.on_pipeline_start_item += SeesawConnection.handle_start_item runner.on_pipeline_finish_item += SeesawConnection.handle_finish_item runner.on_status += SeesawConnection.handle_runner_status ioloop.PeriodicCallback(SeesawConnection.broadcast_timestamp, 1000).start() router = SockJSRouter(SeesawConnection) application = web.Application( router.apply_routes([ (r"/(.*\.(html|css|js|swf|png|ico))$", web.StaticFileHandler, {"path": PUBLIC_PATH}), ("/", IndexHandler), ("/api/(.+)$", ApiHandler, {"runner": runner})]), # flash_policy_port = 843, # flash_policy_file=os.path.join(PUBLIC_PATH, "flashpolicy.xml"), socket_io_address=bind_address, socket_io_port=port_number, # settings for AuthenticatedApplication auth_enabled=(http_password or "").strip() != "", check_auth=lambda r, username, password: ( password == http_password and (http_username or "").strip() in ["", username] ), auth_realm="ArchiveTeam Warrior", skip_auth=[] ) application.listen(port_number, bind_address)
def create_application_handlers(sockjs_settings): handlers = [ tornado.web.url(r'/', MainHandler, name="main"), tornado.web.url(r'/project/create$', ProjectCreateHandler, name="project_create"), tornado.web.url(r'/project/([^/]+)/([^/]+)$', ProjectDetailHandler, name="project_detail"), tornado.web.url(r'/project/([^/]+)/namespace/create$', NamespaceFormHandler, name="namespace_create"), tornado.web.url(r'/project/([^/]+)/namespace/edit/([^/]+)/', NamespaceFormHandler, name="namespace_edit"), tornado.web.url(r'/api/([^/]+)$', ApiHandler, name="api"), tornado.web.url(r'/auth$', AuthHandler, name="auth"), tornado.web.url(r'/logout$', LogoutHandler, name="logout"), tornado.web.url(r'/dumps$', StructureDumpHandler, name="dump_structure"), tornado.web.url(r'/loads$', StructureLoadHandler, name="load_structure") ] # create SockJS route for admin connections admin_sock_router = SockJSRouter(AdminSocketHandler, '/socket', user_settings=sockjs_settings) handlers = admin_sock_router.urls + handlers # create SockJS route for client connections client_sock_router = SockJSRouter(SockjsConnection, '/connection', user_settings=sockjs_settings) handlers = client_sock_router.urls + handlers # match everything else to 404 handler handlers.append(tornado.web.url(r'.*', Http404Handler, name='http404')) return handlers
def make_router(): conns = { 'main': MainConnection, 'logs': LogsConnection, 'shell': ShellConnection, } if config.HAS_MANAGE: from mist.manage.sock import ManageLogsConnection conns['manage_logs'] = ManageLogsConnection return SockJSRouter(MultiplexConnection.get(**conns), '/socket')
class RealTimeMagic(object): monitors = [] def __init__(self, *args, **kwargs): # Use a better datastructure for subscriptions # Consider a self-locking dict self.subscriptions = defaultdict(list) # consider spawning threads # Authenticators can be a normal dict self.authenticators = defaultdict(list) # Functions to handle an incomming message self.openings = [] self.receivers = [] self.push_traps = [] self.closings = [] self.ioloop = ioloop.IOLoop.instance() self.local = kwargs.get("local", False) self.async = kwargs.get("async", True) if self.async: self.workers = ThreadPool(kwargs.get("threads", 64)) def start(self): self.router = SockJSRouter(PubSubConnection, "/pubsub", user_settings={"master": self}) app = web.Application(self.router.urls) app.listen(9000) for monitor in self.monitors: t = threading.Thread(target=monitor, args=(self,)) t.daemon = True t.start() t = threading.Thread(target=self.ioloop.start) t.daemon = True t.start() logging.info("Realtime listener started.") # Returning control to the parent application so we can catch keyword # interrupts. try: while 1: t.join(1) if not t.isAlive(): break except KeyboardInterrupt: print "\nExiting..." sys.exit(1) def slow_stuff(self, num): print "Executing a slow query..." import psycopg2 cnn = psycopg2.connect(dsn="dbname=snarl") cur = cnn.cursor() cur.execute("SELECT pg_sleep(%s);" % num) cur.close() cnn.close() def _publish(self, connections, message): # self.slow_stuff(10) logging.info("Broadcasting message...") self.router.broadcast(connections, message) # for ws in connections: # ws.send(message) def publish(self, channel, message, async=True): """ This is already being handled asynchronously when comming from the connection. """ if self.async and async:
def __init__(self, *args, **kwargs): SockJSRouter.__init__(self, *args, **kwargs) try: self.app = kwargs['application'] except KeyError: pass