Daemonizing python's BaseHTTPServer

后端 未结 6 1898
南方客
南方客 2021-02-05 20:35

I am working on a daemon where I need to embed a HTTP server. I am attempting to do it with BaseHTTPServer, which when I run it in the foreground, it works fine, but when I tr

6条回答
  •  借酒劲吻你
    2021-02-05 21:20

    Since this has solicited answers since I originally posted, I thought that I'd share a little info.

    The issue with the output has to do with the fact that the default handler for the logging module uses the StreamHandler. The best way to handle this is to create your own handlers. In the case where you want to use the default logging module, you can do something like this:

    # Get the default logger
    default_logger = logging.getLogger('')
    
    # Add the handler
    default_logger.addHandler(myotherhandler)
    
    # Remove the default stream handler
    for handler in default_logger.handlers:
        if isinstance(handler, logging.StreamHandler):
            default_logger.removeHandler(handler)
    

    Also at this point I have moved to using the very nice Tornado project for my embedded http servers.

提交回复
热议问题