我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用tornado.web.URLSpec()。
def run(self): import tornado.autoreload tornado.autoreload.watch('index.wsgi') import re from tornado.web import URLSpec, StaticFileHandler # The user should not use `tornado.web.Application.add_handlers` # since here in SAE one application only has a single host, so here # we can just use the first host_handers. handlers = self.application.handlers[0][1] for prefix, path in self.static_files.iteritems(): pattern = re.escape(prefix) + r"(.*)" handlers.insert(0, URLSpec(pattern, StaticFileHandler, {"path": path})) os.environ['sae.run_main'] = '1' import tornado.ioloop from tornado.httpserver import HTTPServer server = HTTPServer(self.application, xheaders=True) server.listen(self.conf.port, self.conf.host) tornado.ioloop.IOLoop.instance().start()
def add_routes(self, context, *handlers): self.url_specs[context] += [ web.URLSpec(*handler).regex for handler in handlers ] self.add_handlers(r'.*$', handlers)
def add_favicon_path(self, path: str) -> None: """Add path to serve favicon file. ``path`` should be a directory, which contains favicon file (``favicon.ico``) for your app. """ spec = web.URLSpec( '/(favicon.ico)', StaticFileHandler, dict(path=path) ) # Need some check handlers = self.handlers[0][1] handlers.append(spec)
def add_raw_route(url_regex, handler, **kwargs): _handlers.append(URLSpec(url_regex, handler, kwargs))
def include(prefix, module_path): module = __import__(module_path, globals(), locals(), fromlist=["*"]) urls = getattr(module, 'urls') print urls final_urls = list() for url in urls: pattern = url.regex.pattern if pattern.startswith("/"): pattern = r"%s%s" % (prefix, pattern[1:]) else: pattern = r"%s%s" % (prefix, pattern) final_urls.append(URLSpec(pattern, url.handler_class, kwargs=url.kwargs, name=url.name)) return final_urls
def __init__(self, config): self._graph = tf.Graph() tf_config = None if not config.get("gpu", None): tf_config = tf.ConfigProto(device_count={"GPU":0}) else: tf_config = tf.ConfigProto(device_count={"GPU":1}) tf_config.gpu_options.allow_growth = True tf_config.gpu_options.per_process_gpu_memory_fraction=config["gpu_memory_fraction"] self._sess = tf.Session(config=tf_config, graph=self._graph) with self._sess.graph.as_default(): graph_def = tf.GraphDef() with open(config['model'], 'rb') as file: graph_def.ParseFromString(file.read()) tf.import_graph_def(graph_def, name="") self._input_x = self._sess.graph.get_operation_by_name('ph_input_x').outputs[0] self._pred = self._sess.graph.get_operation_by_name('predictions').outputs[0] self._softmax = self._sess.graph.get_operation_by_name('softmax').outputs[0] self._http_app = web.Application( handlers=[ web.URLSpec(r"/api/echo/(.*)", EchoHandler, dict(app=self)), web.URLSpec(r"/api/image", EchoHandler, dict(app=self)), web.URLSpec(r"/ui/segmentation", TestUIHandler, dict(app=self)) ], debug=config["debug"], )