Пример #1
0
    def server_thread():
        from tornado.log import access_log, app_log, gen_log
        access_log.setLevel(logging.ERROR)
        app_log.setLevel(logging.ERROR)
        gen_log.setLevel(logging.ERROR)

        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)

        set_ioloop(tornado.ioloop.IOLoop.current())  # to enable bokeh app

        port = 0
        if os.environ.get("PYWEBIO_SCRIPT_MODE_PORT"):
            port = int(os.environ.get("PYWEBIO_SCRIPT_MODE_PORT"))

        server, port = _setup_server(webio_handler=SingleSessionWSHandler,
                                     port=port,
                                     host='localhost')
        tornado.ioloop.IOLoop.current().spawn_callback(
            partial(wait_to_stop_loop, server=server))

        if "PYWEBIO_SCRIPT_MODE_PORT" not in os.environ:
            tornado.ioloop.IOLoop.current().spawn_callback(
                open_webbrowser_on_server_started, 'localhost', port)

        tornado.ioloop.IOLoop.current().start()
        logger.debug('Tornado server exit')
Пример #2
0
    def serve(self, port: int = 5000, n_process: int = None):
        if n_process is None:
            n_process = 1 if sys.platform == 'win32' else 8

        fmt = LogFormatter(fmt='%(asctime)s - %(levelname)s - %(message)s',
                           datefmt='%Y-%m-%d %H:%M:%S',
                           color=True)
        root_logger = logging.getLogger()

        console_handler = logging.StreamHandler()
        file_handler = logging.FileHandler('server.log')

        console_handler.setFormatter(fmt)
        file_handler.setFormatter(fmt)

        root_logger.addHandler(console_handler)
        root_logger.addHandler(file_handler)

        app_log.setLevel(logging.INFO)
        gen_log.setLevel(logging.INFO)
        access_log.setLevel(logging.INFO)

        app_log.info("Model is loading...")
        app_log.info("Model Has Been Loaded!")

        app = Application([(r"/.*", LTPHandler, dict(ltp=self))])

        server = HTTPServer(app)
        server.bind(port)
        server.start(n_process)
        ioloop.IOLoop.instance().start()
Пример #3
0
def main():
    # use line buffer
    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 1)
    sys.stderr = os.fdopen(sys.stderr.fileno(), 'w', 1)
    args = parser.parse_args()
    key = args.key

    if key is None:
        print("A key is needed: use -k option to provide it")
        sys.exit(1)

    # it's the key set by the user
    control.app_key = key

    # set logging level
    access_log.setLevel(logging.INFO)

    # specifies to tornado app on which address:port it has to listen to
    application.listen(args.port, address=args.address)

    tornado.ioloop.IOLoop.instance().add_timeout(
        0, lambda: print_info(args.address, args.port))

    try:
        tornado.ioloop.IOLoop.instance().start()
    except KeyboardInterrupt:
        pass
Пример #4
0
    def init_logger(cls, port):
        formatter = LogFormatter(fmt=cls.__fmt.format(port=port),
                                 datefmt="",
                                 color=False)

        access_log_handler = ConcurrentRotatingFileHandler(
            filename=os.path.join(ServerConfig["log_dir"], "access.log"))
        access_log_handler.setFormatter(formatter)
        access_log.addHandler(access_log_handler)

        server_log_handler = ConcurrentRotatingFileHandler(
            filename=os.path.join(ServerConfig['log_dir'], 'server.log'),
            maxBytes=128 * 1024 * 1024,
            backupCount=5,
            encoding='utf8')
        server_log_handler.setFormatter(formatter)
        gen_log.addHandler(server_log_handler)
        app_log.addHandler(server_log_handler)

        access_log.setLevel(logging.INFO)
        gen_log.setLevel(getattr(logging, ServerConfig['log_level'].upper()))
        app_log.setLevel(getattr(logging, ServerConfig['log_level'].upper()))

        access_log.propagate = app_log.propagate = gen_log.propagate = False
        return
Пример #5
0
def main():
    # Always use line buffer
    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 1)
    sys.stderr = os.fdopen(sys.stderr.fileno(), 'w', 1)

    args = parser.parse_args()

    if args.key is None:
        key = os.getenv('REREREMOTE_KEY')
    else:
        key = args.key

    if key is None:
        print("You need to provide a key, either using -k argument or "+
              "REREREMOTE_KEY environment variable.", file=sys.stderr)
        sys.exit(1)

    control.app_key = key
    access_log.setLevel(logging.INFO)
    application.listen(args.port, address=args.address)

    tornado.ioloop.IOLoop.instance().add_timeout(0, lambda:
            print_server_running(args.address, args.port))

    try:
        tornado.ioloop.IOLoop.instance().start()
    except KeyboardInterrupt:
        pass
Пример #6
0
Файл: log.py Проект: cwen0/zeus
def set_tornado_log():
    handler = logging.StreamHandler(sys.stdout)
    handler.setFormatter(formatter)

    gen_log.addHandler(handler)
    gen_log.setLevel(logging.INFO)

    access_log.addHandler(handler)
    access_log.setLevel(logging.INFO)

    app_log.addHandler(handler)
    access_log.setLevel(logging.INFO)
Пример #7
0
    def __init__(self,
                 debug_mode=False,
                 access_logging=None,
                 static_path='./static',
                 adapters=None):
        """Initialise the HttpServer object.

        :param debug_mode: Set True to enable Tornado debug mode
        :param static_path: Set the path to static file content rendered by default route
        :param adapters: list of adapters to register with API route
        """
        settings = {
            "debug": debug_mode,
            "log_function": self.log_request,
        }

        # Set the up the access log level
        if access_logging is not None:
            try:
                level_val = getattr(logging, access_logging.upper())
                access_log.setLevel(level_val)
            except AttributeError:
                logging.error("Access logging level {} not recognised".format(
                    access_logging))

        # Create an API route
        self.api_route = ApiRoute()

        # Register adapters with the API route and get handlers
        for adapter in adapters:
            self.api_route.register_adapter(adapters[adapter])

        # Initialize adapters for all those that require inter adapter communication
        self.api_route.initialize_adapters()

        handlers = self.api_route.get_handlers()

        # Create a default route for static content and get handlers
        default_route = DefaultRoute(static_path)
        handlers += default_route.get_handlers()

        # Create the Tornado web application for these handlers
        self.application = tornado.web.Application(handlers, **settings)
Пример #8
0
    def serve(self):
        access_log.setLevel(logging.INFO if self.verbose else logging.ERROR)

        print(f'Connecting to PostgreSQL at {self.pghost}:{self.pgport}, '
              f'db={self.dbname}, user={self.user}...')
        io_loop = IOLoop.current()
        self.pool = io_loop.run_sync(
            partial(create_pool,
                    dsn=f"postgresql://{self.user}:{self.password}@"
                    f"{self.pghost}:{self.pgport}/{self.dbname}"))
        io_loop.run_sync(partial(self.init_connection))

        if self.sql_file:
            with open(self.sql_file) as stream:
                query = stream.read()
            print(f'Loaded {self.sql_file}')
        else:
            query = self.generated_query

        if self.verbose:
            print(f'Using SQL query:\n\n-------\n\n{query}\n\n-------\n\n')

        application = Application([
            (r"/", GetMetadata, dict(metadata=self.metadata)),
            (r"/tiles/([0-9]+)/([0-9]+)/([0-9]+).pbf", GetTile,
             dict(pool=self.pool,
                  query=query,
                  key_column=self.key_column,
                  gzip=self.gzip,
                  test_geometry=self.test_geometry,
                  verbose=self.verbose)),
        ])

        #         application.listen(self.port)
        #         print(f"Postserve started, listening on 0.0.0.0:{self.port}")
        #         print(f"Use {self.url} as the data source")
        #         IOLoop.instance().start()
        server = tornado.httpserver.HTTPServer(application)
        server.bind(self.port)
        #server.start(tornado.process.cpu_count())
        server.start(4)
        IOLoop.current().start()
Пример #9
0
    def __init__(self, debug_mode=False, access_logging=None,
                 static_path='./static', adapters=None):
        """Initialise the HttpServer object.

        :param debug_mode: Set True to enable Tornado debug mode
        :param static_path: Set the path to static file content rendered by default route
        :param adapters: list of adapters to register with API route
        """
        settings = {
            "debug": debug_mode,
            "log_function": self.log_request,
        }

        # Set the up the access log level
        if access_logging is not None:
            try:
                level_val = getattr(logging, access_logging.upper())
                access_log.setLevel(level_val)
            except AttributeError:
                logging.error(
                    "Access logging level {} not recognised".format(access_logging)
                )

        # Create an API route
        self.api_route = ApiRoute()

        # Register adapters with the API route and get handlers
        for adapter in adapters:
            self.api_route.register_adapter(adapters[adapter])

        # Initialize adapters for all those that require inter adapter communication
        self.api_route.initialize_adapters()

        handlers = self.api_route.get_handlers()

        # Create a default route for static content and get handlers
        default_route = DefaultRoute(static_path)
        handlers += default_route.get_handlers()

        # Create the Tornado web application for these handlers
        self.application = tornado.web.Application(handlers, **settings)
Пример #10
0
def enable_tornado_log():
    """开启 Tornado 内置日志信息
    * ``tornado.access``: Per-request logging for Tornado's HTTP servers (and
      potentially other servers in the future)
    * ``tornado.application``: Logging of errors from application code (i.e.
      uncaught exceptions from callbacks)
    * ``tornado.general``: General-purpose logging, including any errors
      or warnings from Tornado itself.
    """
    try:
        access_log.addHandler(filehandler)
        access_log.setLevel(logger.level)

        app_log.addHandler(filehandler)
        app_log.setLevel(logger.level)

        gen_log.addHandler(filehandler)
        gen_log.setLevel(logger.level)
    except Exception:
        error_msg = traceback.format_exc()
        logger.warning(f'enable tornado log fail.\t{error_msg}')
        logger.error(f'enable tornado log fail.')
Пример #11
0
    def init_logging(self):
        """Initialize logging"""
        # This prevents double log messages because tornado use a root logger that
        # self.log is a child of. The logging module dipatches log messages to a log
        # and all of its ancenstors until propagate is set to False.
        self.log.propagate = False

        # Is this actually used anywhere? flake says it isn't
        # _formatter = self._log_formatter_cls(fmt=self.log_format, datefmt=self.log_datefmt)

        # hook up tornado 3's loggers to our app handlers
        for log in (app_log, access_log, gen_log):
            # ensure all log statements identify the application they come from
            log.name = self.log.name
        logger = logging.getLogger("tornado")
        logger.propagate = True
        logger.parent = self.log
        logger.setLevel(self.log.level)

        access_log.propagate = False
        # make sure access log is enabled even if error level is WARNING|ERROR
        access_log.setLevel(logging.INFO)
        stdout_handler = logging.StreamHandler(sys.stdout)
        access_log.addHandler(stdout_handler)
Пример #12
0
import tornado.log

from setting import setting
from tornado.options import options
from tornado.log import access_log as logger
from base import BaseHandler

# tornado.auth implementation
from oauth_handler.handler import OauthHandler
from oauth_handler.handler import FetchPersonalInfo as UserInfoHandler

# SDK implementation
# from sdk_handler.handler import OauthSDKHandler as OauthHandler
# from sdk_handler.handler import FetchUserInfoSDKHandler as UserInfoHandler

logger.setLevel(logging.DEBUG)


class MainHandler(BaseHandler):
    """
    Homepage for demo.

    Render a page which contains a link referring to "/oauth_handler".
    """

    def get(self):
        self.render("index.html", oauth_url="/oauth")

if __name__ == "__main__":
    application = tornado.web.Application([
        (r"/", MainHandler),
Пример #13
0
 def __init__(self):
     if conf.SETTINGS.get('access_log'):
         access_log.setLevel(logging.INFO)
     tornado.web.Application.__init__(self, route.route, **conf.SETTINGS)
Пример #14
0
            return self.set_status(401)

        query = {n: v[0].decode() for n, v in self.request.arguments.items()}
        self.write({
            'params': {
                'user': int(user),
                'record': int(record),
            },
            'query': query,
            'data': json.loads(self.request.body),
        })


urls = [url(f"/route-{n}", HTML) for n in range(5)]
urls += [url(f"/route-dyn-{n}/(.*)", HTML) for n in range(5)]

app = Application(urls + [
    url('/html', HTML),
    url('/upload', Upload),
    url('/api/users/([^/]+)/records/(.+)', API),
])

# Start the application
if __name__ == '__main__':
    uvloop.install()
    server = HTTPServer(app)
    server.bind(8080, reuse_port=True)
    server.start()
    access_log.setLevel('ERROR')
    IOLoop.current().start()
Пример #15
0
import tornado.log

from setting import setting
from tornado.options import options
from tornado.log import access_log as logger
from base import BaseHandler

# tornado.auth implementation
from oauth_handler.handler import OauthHandler
from oauth_handler.handler import FetchPersonalInfo as UserInfoHandler

# SDK implementation
# from sdk_handler.handler import OauthSDKHandler as OauthHandler
# from sdk_handler.handler import FetchUserInfoSDKHandler as UserInfoHandler

logger.setLevel(logging.DEBUG)


class MainHandler(BaseHandler):
    """
    Homepage for demo.

    Render a page which contains a link referring to "/oauth_handler".
    """
    def get(self):
        self.render("index.html", oauth_url="/oauth")


if __name__ == "__main__":
    application = tornado.web.Application([
        (r"/", MainHandler),
Пример #16
0
from server.class_calander import ClassTime
from server.handlers import handlers
from server.setting import project_dir, term_start_date
from server.tools import set_logger_to_file

define("port", default=5000, help="run on the given port", type=int)
dev = bool(os.environ.get('DEV'))

if __name__ == "__main__":
    tornado.options.parse_command_line()

    ClassTime.set_startday(term_start_date.year, term_start_date.month,
                           term_start_date.day)

    if not dev:
        # 线上环境配置
        access_log.disabled = True
        log_file = '%s/../app-%s.log' % (project_dir, options.port)
        set_logger_to_file(app_log, filename=log_file)
        set_logger_to_file(gen_log, filename=log_file)
    else:
        access_log.setLevel(logging.DEBUG)
        app_log.setLevel(logging.DEBUG)
        gen_log.setLevel(logging.DEBUG)
        logging.info('Dev environment')

    app = tornado.web.Application(handlers=handlers, debug=dev)
    http_server = tornado.httpserver.HTTPServer(app, xheaders=True)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()