#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/8/31 14:32 # @Author : linjunpeng # @Site : # @File : index_handler.py # @Software: PyCharm # @Desc import tornado from tornado import options options.OptionParser().define("port", default=8000, help="run on the given port", type=int) class IndexHandler(tornado.web.RequestHandler): def get(self): greeting = self.get_argument('greeting', 'Hello') self.write(greeting + ', friendly user!') if __name__ == "__main__": tornado.options.parse_command_line() app = tornado.web.Application(handlers=[(r"/", IndexHandler)]) http_server = tornado.httpserver.HTTPServer(app) http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start()
def main(): from tornado import options default_general_logformat = "[%(asctime)s.%(msecs)d]\t[%(filename).5s:%(lineno)d]\t%(levelname)s\t%(message)s" default_access_logformat = "[%(asctime)s.%(msecs)d]\t[%(filename).5s:%(lineno)d]\t%(levelname)s\t%(trace_id)s\t%(message)s" opts = options.OptionParser() opts.define("version", type=bool, help="show version and exit", callback=show_version) opts.define("locators", default=["localhost:10053"], type=str, multiple=True, help="comma-separated endpoints of locators") opts.define("cache", default=DEFAULT_SERVICE_CACHE_COUNT, type=int, help="count of instances per service") opts.define( "config", help="path to configuration file", type=str, callback=lambda path: opts.parse_config_file(path, final=False)) opts.define("count", default=1, type=int, help="count of tornado processes") opts.define( "endpoints", default=["tcp://localhost:8080"], type=str, multiple=True, help= "Specify endpoints to bind on: prefix unix:// or tcp:// should be used" ) opts.define("request_header", default="X-Request-Id", type=str, help="header used as a trace id") opts.define("forcegen_request_header", default=False, type=bool, help="enable force generation of the request header") opts.define("sticky_header", default="X-Cocaine-Sticky", type=str, help="sticky header name") opts.define("gcstats", default=False, type=bool, help="print garbage collector stats to stderr") opts.define("srwconfig", default="", type=str, help="path to srwconfig") opts.define("allow_json_rpc", default=True, type=bool, help="allow JSON RPC module") # tracing options opts.define("tracing_chance", default=DEFAULT_TRACING_CHANCE, type=float, help="default chance for an app to be traced") opts.define("configuration_service", default="unicorn", type=str, help="name of configuration service") opts.define( "tracing_conf_path", default="/zipkin_sampling", type=str, help="path to the configuration nodes in the configuration service") # various logging options opts.define( "logging", default="info", help=("Set the Python log level. If 'none', tornado won't touch the " "logging configuration."), metavar="debug|info|warning|error|none") opts.define("log_to_cocaine", default=False, type=bool, help="log to cocaine") opts.define( "log_to_stderr", type=bool, default=None, help=("Send log output to stderr. " "By default use stderr if --log_file_prefix is not set and " "no other logging is configured.")) opts.define("log_file_prefix", type=str, default=None, metavar="PATH", help=("Path prefix for log file")) opts.define("datefmt", type=str, default="%z %d/%b/%Y:%H:%M:%S", help="datefmt") opts.define("generallogfmt", type=str, help="log format of general logging system", default=default_general_logformat) opts.define("accesslogfmt", type=str, help="log format of access logging system", default=default_access_logformat) opts.define("logframework", type=bool, default=False, help="enable logging various framework messages") # util server opts.define("utilport", default=8081, type=int, help="listening port number for an util server") opts.define("utiladdress", default="127.0.0.1", type=str, help="address for an util server") opts.define("enableutil", default=False, type=bool, help="enable util server") opts.parse_command_line() srw_config = None if opts.srwconfig: try: srw_config = load_srw_config(opts.srwconfig) except Exception as err: print("unable to load SRW config: %s" % err) exit(1) use_reuseport = hasattr(socket, "SO_REUSEPORT") endpoints = Endpoints(opts.endpoints) sockets = [] for path in endpoints.unix: sockets.append(bind_unix_socket(path, mode=0o666)) if not use_reuseport: for endpoint in endpoints.tcp: # We have to bind before fork to distribute sockets to our forks socks = bind_sockets(endpoint.port, address=endpoint.host) sockets.extend(socks) if opts.enableutil: utilsockets = bind_sockets(opts.utilport, address=opts.utiladdress) try: if opts.count != 1: process.fork_processes(opts.count) enable_logging(opts) if opts.gcstats: enable_gc_stats() if use_reuseport: for endpoint in endpoints.tcp: # We have to bind before fork to distribute sockets to our forks socks = bind_sockets(endpoint.port, address=endpoint.host, reuse_port=True) sockets.extend(socks) proxy = CocaineProxy( locators=opts.locators, cache=opts.cache, request_id_header=opts.request_header, sticky_header=opts.sticky_header, forcegen_request_header=opts.forcegen_request_header, default_tracing_chance=opts.tracing_chance, srw_config=srw_config, allow_json_rpc=opts.allow_json_rpc) server = HTTPServer(proxy) server.add_sockets(sockets) if opts.enableutil: utilsrv = HTTPServer(UtilServer(proxy=proxy)) utilsrv.add_sockets(utilsockets) tornado.ioloop.IOLoop.current().start() except KeyboardInterrupt: pass
def main(): from tornado import options opts = options.OptionParser() opts.define("version", type=bool, help="show version and exit", callback=show_version) opts.define("locators", default=["localhost:10053"], type=str, multiple=True, help="comma-separated endpoints of locators") opts.define("cache", default=DEFAULT_SERVICE_CACHE_COUNT, type=int, help="count of instances per service") opts.define( "config", help="path to configuration file", type=str, callback=lambda path: opts.parse_config_file(path, final=False)) opts.define("count", default=1, type=int, help="count of tornado processes") opts.define("port", default=8080, type=int, help="listening port number") opts.define( "endpoints", default=["tcp://localhost:8080"], type=str, multiple=True, help= "Specify endpoints to bind on: prefix unix:// or tcp:// should be used" ) opts.define("request_header", default="X-Request-Id", type=str, help="header used as a trace id") opts.define("forcegen_request_header", default=False, type=bool, help="enable force generation of the request header") opts.define("sticky_header", default="X-Cocaine-Sticky", type=str, help="sticky header name") opts.define("gcstats", default=False, type=bool, help="print garbage collector stats to stderr") # tracing options opts.define("tracing_chance", default=DEFAULT_TRACING_CHANCE, type=float, help="default chance for an app to be traced") opts.define("configuration_service", default="unicorn", type=str, help="name of configuration service") opts.define( "tracing_conf_path", default="/zipkin_sampling", type=str, help="path to the configuration nodes in the configuration service") # various logging options opts.define( "logging", default="info", help=("Set the Python log level. If 'none', tornado won't touch the " "logging configuration."), metavar="debug|info|warning|error|none") opts.define( "log_to_stderr", type=bool, default=None, help=("Send log output to stderr. " "By default use stderr if --log_file_prefix is not set and " "no other logging is configured.")) opts.define("log_file_prefix", type=str, default=None, metavar="PATH", help=("Path prefix for log file")) opts.define("datefmt", type=str, default="%z %d/%b/%Y:%H:%M:%S", help="datefmt") opts.define("generallogfmt", type=str, help="log format of general logging system", default=DEFAULT_GENERAL_LOGFORMAT) opts.define("accesslogfmt", type=str, help="log format of access logging system", default=DEFAULT_ACCESS_LOGFORMAT) opts.define("logframework", type=bool, default=False, help="enable logging various framework messages") opts.define("fingerscrossed", type=bool, default=True, help="enable lazy logging") # util server opts.define("utilport", default=8081, type=int, help="listening port number for an util server") opts.define("utiladdress", default="127.0.0.1", type=str, help="address for an util server") opts.define("enableutil", default=False, type=bool, help="enable util server") opts.define("so_reuseport", default=True, type=bool, help="use SO_REUSEPORT option") opts.parse_command_line() enable_logging(opts) logger = logging.getLogger("cocaine.proxy.general") use_reuseport = False endpoints = Endpoints(opts.endpoints) sockets = [] if endpoints.has_unix: logger.info("Start binding on unix sockets") for path in endpoints.unix: logger.info("Binding on %s", path) sockets.append(bind_unix_socket(path, mode=0o666)) if opts.so_reuseport: if not support_reuseport(): logger.warning("Your system doesn't support SO_REUSEPORT." " Bind and fork mechanism will be used") else: logger.info("SO_REUSEPORT will be used") use_reuseport = True if not use_reuseport and endpoints.has_tcp: logger.info("Start binding on tcp sockets") for endpoint in endpoints.tcp: logger.info("Binding on %s:%d", endpoint.host, endpoint.port) # We have to bind before fork to distribute sockets to our forks socks = bind_sockets(endpoint.port, address=endpoint.host) logger.info( "Listening %s", ' '.join(str("%s:%s" % s.getsockname()[:2]) for s in socks)) sockets.extend(socks) if opts.enableutil: utilsockets = bind_sockets(opts.utilport, address=opts.utiladdress) logger.info( "Util server is listening on %s", ' '.join(str("%s:%s" % s.getsockname()[:2]) for s in utilsockets)) try: if opts.count != 1: process.fork_processes(opts.count) if opts.gcstats: enable_gc_stats() if use_reuseport and endpoints.has_tcp: logger.info("Start binding on tcp sockets") for endpoint in endpoints.tcp: logger.info("Binding on %s:%d", endpoint.host, endpoint.port) # We have to bind before fork to distribute sockets to our forks socks = bind_sockets(endpoint.port, address=endpoint.host, reuse_port=True) logger.info( "Listening %s", ' '.join( str("%s:%s" % s.getsockname()[:2]) for s in socks)) sockets.extend(socks) proxy = CocaineProxy( locators=opts.locators, cache=opts.cache, request_id_header=opts.request_header, sticky_header=opts.sticky_header, forcegen_request_header=opts.forcegen_request_header, default_tracing_chance=opts.tracing_chance) server = HTTPServer(proxy) server.add_sockets(sockets) if opts.enableutil: utilsrv = HTTPServer(UtilServer(proxy=proxy)) utilsrv.add_sockets(utilsockets) tornado.ioloop.IOLoop.current().start() except KeyboardInterrupt: pass