def __init__(self, engine_cls, debug=False, force_snmp_init=True): super(RedisStateHandler, self).__init__() self._state_tracker = EngineStateTracker() logger.info("Initializing websocket server...") # set up a web socket server socket_conf = { "host": os.environ.get("SIMENGINE_SOCKET_HOST"), "port": int(os.environ.get("SIMENGINE_SOCKET_PORT")), } self._server = Server( (socket_conf["host"], socket_conf["port"])).register(self) # Worker(process=False).register(self) Static().register(self._server) Logger().register(self._server) if debug: Debugger(events=False).register(self) self._ws = WebSocket().register(self._server) WebSocketsDispatcher("/simengine").register(self._server) logger.info("Initializing engine...") self._engine = engine_cls( force_snmp_init=force_snmp_init).register(self) self._engine.subscribe_tracker(self._ws) self._engine.subscribe_tracker(self._state_tracker) # Use redis pub/sub communication logger.info("Initializing redis connection...") self._redis_store = redis.StrictRedis(host="localhost", port=6379)
def main(): opts, args = parse_options() bind = parse_bind(opts.bind) if opts.validate: application = (Application() + Root()) app = validator(application) httpd = make_server(bind[0], bind[1], app) httpd.serve_forever() raise SystemExit(0) manager = Manager() opts.debug and Debugger().register(manager) Poller = select_poller(opts.poller.lower()) Poller().register(manager) if opts.server.lower() == "base": BaseServer(bind).register(manager) HelloWorld().register(manager) else: Server(bind).register(manager) Root().register(manager) docroot = os.getcwd() if not args else args[0] Static(docroot=docroot, dirlisting=True).register(manager) opts.passwd and Authentication(passwd=opts.passwd).register(manager) opts.logging and Logger().register(manager) if opts.profile and hotshot: profiler = hotshot.Profile(".profile") profiler.start() if opts.debug: print(graph(manager, name="circuits.web")) print() print(inspect(manager)) for i in range(opts.jobs): manager.start(process=True) manager.run() if opts.profile and hotshot: profiler.stop() profiler.close() stats = hotshot.stats.load(".profile") stats.strip_dirs() stats.sort_stats("time", "calls") stats.print_stats(20)
def test_logger(webapp): logobj = DummyLogger() logger = Logger(logger=logobj) logger.register(webapp) f = urlopen(webapp.server.http.base) s = f.read() assert s == b"Hello World!" s = logobj.message try: address = gethostbyname(gethostname()) except gaierror: address = "127.0.0.1" d = {} d["h"] = address d["l"] = "-" d["u"] = "-" d["r"] = "GET / HTTP/1.1" d["s"] = "200" d["b"] = "12" d["f"] = "" d["a"] = "Python-urllib/%s" % sys.version[:3] keys = list(d.keys()) for k in keys: assert d[k] in s logger.unregister()
def test_file(webapp): logfile = StringIO() logger = Logger(file=logfile) logger.register(webapp) f = urlopen(webapp.server.http.base) s = f.read() assert s == b"Hello World!" logfile.seek(0) s = logfile.read().strip() try: address = gethostbyname(gethostname()) except gaierror: address = "127.0.0.1" d = {} d["h"] = address d["l"] = "-" d["u"] = "-" d["r"] = "GET / HTTP/1.1" d["s"] = "200" d["b"] = "12" d["f"] = "" d["a"] = "Python-urllib/%s" % sys.version[:3] keys = list(d.keys()) for k in keys: assert d[k] in s logfile.close() logger.unregister()
def test_filename(webapp, tmpdir): logfile = str(tmpdir.ensure("logfile")) logger = Logger(file=logfile) logger.register(webapp) logfile = open(logfile, "r") f = urlopen(webapp.server.http.base) s = f.read() assert s == b"Hello World!" logfile.seek(0) s = logfile.read().strip() try: address = gethostbyname(gethostname()) except gaierror: address = "127.0.0.1" d = {} d["h"] = address d["l"] = "-" d["u"] = "-" d["r"] = "GET / HTTP/1.1" d["s"] = "200" d["b"] = "12" d["f"] = "" d["a"] = "Python-urllib/%s" % sys.version[:3] keys = list(d.keys()) for k in keys: if d[k] and d[k].startswith("127."): # loopback network: 127.0.0.0/8 assert re.search("127(\.[0-9]{1,3}){3}", s) else: assert d[k] in s logfile.close() logger.unregister()
def test_logger(webapp): logobj = DummyLogger() logger = Logger(logger=logobj) logger.register(webapp) f = urlopen(webapp.server.http.base) s = f.read() assert s == b"Hello World!" s = logobj.message try: address = gethostbyname(gethostname()) except gaierror: address = "127.0.0.1" d = {} d["h"] = address d["l"] = "-" d["u"] = "-" d["r"] = "GET / HTTP/1.1" d["s"] = "200" d["b"] = "12" d["f"] = "" d["a"] = "Python-urllib/%s" % sys.version[:3] keys = list(d.keys()) for k in keys: if d[k] and d[k].startswith("127."): # loopback network: 127.0.0.0/8 assert re.search("127(\.[0-9]{1,3}){3}", s) else: assert d[k] in s logger.unregister()
def GET(self, name="FrontPage", action="view"): environ = self.environ.copy() environ["page.name"] = name environ["parser"] = text2html d = {} d["title"] = name d["version"] = circuits.__version__ d["menu"] = text2html(self.db.get("SiteMenu", ""), environ=environ) text = self.db.get(name, "") s = open("tpl/%s.html" % action, "r").read() if action == "view": d["text"] = text2html(text, environ=environ) else: d["text"] = text return s % d def POST(self, name="FrontPage", **form): self.db.save(name, form.get("text", "")) return self.redirect(name) app = Server(("0.0.0.0", 8000)) Static(docroot="static").register(app) Root().register(app) Logger().register(app) app.run()
EntityPreprocessorComponent(), DialogGeneratorComponent() ], "jobRunnerComponents": [ TimeJobRunnerComponent(), GreetJobRunnerComponent(), ComplimentJobRunnerComponent(), QuestionJobRunnerComponent(), SearchGeneralJobRunnerComponent() ], "gateways": [WSGateway(), Root()], "dispatchers": [WebSocketsDispatcher("/websocket")], "circuitComponents": [ Debugger(), Static(), Logger(), ] } def bootstrapAppComponents(app, appComponents): for appComponent in appComponents: appComponent.register(app) def bootstrapJobRunnerComponents(app, jobRunnerComponents): for jobRunnerComponent in jobRunnerComponents: jobRunnerComponent.register(app) def bootstrapGateways(app, gateways):
def main(): config = Config() manager = Manager() if config.get("debug"): manager += Debugger( events=config.get("verbose"), file=config.get("errorlog"), ) environ = Environment(config) SignalHandler(environ).register(environ) manager += environ if config.get("sock") is not None: bind = config.get("sock") elif ":" in config.get("bind"): address, port = config.get("bind").split(":") bind = ( address, int(port), ) else: bind = ( config.get("bind"), config.get("port"), ) server = (Server(bind) + Sessions() + Root(environ) + CacheControl(environ) + ErrorHandler(environ)) if MemoryMonitor is not None: MemoryMonitor(channel="/memory").register(server) if not config.get("disable-logging"): server += Logger(file=config.get("accesslog", sys.stdout)) if not config.get("disable-static"): server += Static(docroot=os.path.join(config.get("theme"), "htdocs")) if not config.get("disable-hgweb"): baseui = ui() baseui.setconfig("web", "prefix", "/+hg") baseui.setconfig("web", "style", "gitweb") baseui.setconfig("web", "allow_push", "*") baseui.setconfig("web", "push_ssl", False) baseui.setconfig("web", "allow_archive", ["bz2", "gz", "zip"]) baseui.setconfig("web", "description", config.get("description")) server += Gateway({ "/+hg": hgweb(environ.storage.repo_path, config.get("name"), baseui) }) if not config.get("disable-compression"): server += Compression(environ) if config.get("daemon"): manager += Daemon(config.get("pidfile")) server.register(manager) manager.run()