(port=DEFAULT_LOGGING_CONFIG_PORT, verify=None)
return Server(ConfigSocketReceiver, ConfigStreamHandler, port, verify)
Start up a socket server on the specified port, and listen for new
    configurations.

    These will be sent as a file suitable for processing by fileConfig().
    Returns a Thread object on which you can call start() to start the server,
    and which you can join() when appropriate. To stop the server, call
    stopListening().

    Use the ``verify`` argument to verify any bytes received across the wire
    from a client. If specified, it should be a callable which receives a
    single argument - the bytes of configuration data received across the
    network - and it should return either ``None``, to indicate that the
    passed in bytes could not be verified and should be discarded, or a
    byte string which is then passed to the configuration machinery as
    normal. Note that you can return transformed bytes, e.g. by decrypting
    the bytes passed in.
Start up a socket server on the specified port, and listen for new
    configurations.
Start up a socket server on the specified port and listen for new configurations .
def listen(port=DEFAULT_LOGGING_CONFIG_PORT, verify=None):
    """
    Start up a socket server on the specified port, and listen for new
    configurations.

    These will be sent as a file suitable for processing by fileConfig().
    Returns a Thread object on which you can call start() to start the server,
    and which you can join() when appropriate. To stop the server, call
    stopListening().

    Use the ``verify`` argument to verify any bytes received across the wire
    from a client. If specified, it should be a callable which receives a
    single argument - the bytes of configuration data received across the
    network - and it should return either ``None``, to indicate that the
    passed in bytes could not be verified and should be discarded, or a
    byte string which is then passed to the configuration machinery as
    normal. Note that you can return transformed bytes, e.g. by decrypting
    the bytes passed in.
    """

    class ConfigStreamHandler(StreamRequestHandler):
        """
        Handler for a logging configuration request.

        It expects a completely new logging configuration and uses fileConfig
        to install it.
        """
        def handle(self):
            """
            Handle a request.

            Each request is expected to be a 4-byte length, packed using
            struct.pack(">L", n), followed by the config file.
            Uses fileConfig() to do the grunt work.
            """
            try:
                conn = self.connection
                chunk = conn.recv(4)
                if len(chunk) == 4:
                    slen = struct.unpack(">L", chunk)[0]
                    chunk = self.connection.recv(slen)
                    while len(chunk) < slen:
                        chunk = chunk + conn.recv(slen - len(chunk))
                    if self.server.verify is not None:
                        chunk = self.server.verify(chunk)
                    if chunk is not None:   # verified, can process
                        chunk = chunk.decode("utf-8")
                        try:
                            import json
                            d =json.loads(chunk)
                            assert isinstance(d, dict)
                            dictConfig(d)
                        except Exception:
                            #Apply new configuration.

                            file = io.StringIO(chunk)
                            try:
                                fileConfig(file)
                            except Exception:
                                traceback.print_exc()
                    if self.server.ready:
                        self.server.ready.set()
            except OSError as e:
                if e.errno != RESET_ERROR:
                    raise

    class ConfigSocketReceiver(ThreadingTCPServer):
        """
        A simple TCP socket-based logging config receiver.
        """

        allow_reuse_address = 1

        def __init__(self, host='localhost', port=DEFAULT_LOGGING_CONFIG_PORT,
                     handler=None, ready=None, verify=None):
            ThreadingTCPServer.__init__(self, (host, port), handler)
            logging._acquireLock()
            self.abort = 0
            logging._releaseLock()
            self.timeout = 1
            self.ready = ready
            self.verify = verify

        def serve_until_stopped(self):
            import select
            abort = 0
            while not abort:
                rd, wr, ex = select.select([self.socket.fileno()],
                                           [], [],
                                           self.timeout)
                if rd:
                    self.handle_request()
                logging._acquireLock()
                abort = self.abort
                logging._releaseLock()
            self.server_close()

    class Server(threading.Thread):

        def __init__(self, rcvr, hdlr, port, verify):
            super(Server, self).__init__()
            self.rcvr = rcvr
            self.hdlr = hdlr
            self.port = port
            self.verify = verify
            self.ready = threading.Event()

        def run(self):
            server = self.rcvr(port=self.port, handler=self.hdlr,
                               ready=self.ready,
                               verify=self.verify)
            if self.port == 0:
                self.port = server.server_address[1]
            self.ready.set()
            global _listener
            logging._acquireLock()
            _listener = server
            logging._releaseLock()
            server.serve_until_stopped()

    return Server(ConfigSocketReceiver, ConfigStreamHandler, port, verify)
def listen ( port = DEFAULT_LOGGING_CONFIG_PORT , verify = None ) : class ConfigStreamHandler ( StreamRequestHandler ) : """
        Handler for a logging configuration request.

        It expects a completely new logging configuration and uses fileConfig
        to install it.
        """ def handle ( self ) : """
            Handle a request.

            Each request is expected to be a 4-byte length, packed using
            struct.pack(">L", n), followed by the config file.
            Uses fileConfig() to do the grunt work.
            """ try : conn = self . connection chunk = conn . recv ( 4 ) if len ( chunk ) == 4 : slen = struct . unpack ( ">L" , chunk ) [ 0 ] chunk = self . connection . recv ( slen ) while len ( chunk ) < slen : chunk = chunk + conn . recv ( slen - len ( chunk ) ) if self . server . verify is not None : chunk = self . server . verify ( chunk ) if chunk is not None : # verified, can process chunk = chunk . decode ( "utf-8" ) try : import json d = json . loads ( chunk ) assert isinstance ( d , dict ) dictConfig ( d ) except Exception : #Apply new configuration. file = io . StringIO ( chunk ) try : fileConfig ( file ) except Exception : traceback . print_exc ( ) if self . server . ready : self . server . ready . set ( ) except OSError as e : if e . errno != RESET_ERROR : raise class ConfigSocketReceiver ( ThreadingTCPServer ) : """
        A simple TCP socket-based logging config receiver.
        """ allow_reuse_address = 1 def __init__ ( self , host = 'localhost' , port = DEFAULT_LOGGING_CONFIG_PORT , handler = None , ready = None , verify = None ) : ThreadingTCPServer . __init__ ( self , ( host , port ) , handler ) logging . _acquireLock ( ) self . abort = 0 logging . _releaseLock ( ) self . timeout = 1 self . ready = ready self . verify = verify def serve_until_stopped ( self ) : import select abort = 0 while not abort : rd , wr , ex = select . select ( [ self . socket . fileno ( ) ] , [ ] , [ ] , self . timeout ) if rd : self . handle_request ( ) logging . _acquireLock ( ) abort = self . abort logging . _releaseLock ( ) self . server_close ( ) class Server ( threading . Thread ) : def __init__ ( self , rcvr , hdlr , port , verify ) : super ( Server , self ) . __init__ ( ) self . rcvr = rcvr self . hdlr = hdlr self . port = port self . verify = verify self . ready = threading . Event ( ) def run ( self ) : server = self . rcvr ( port = self . port , handler = self . hdlr , ready = self . ready , verify = self . verify ) if self . port == 0 : self . port = server . server_address [ 1 ] self . ready . set ( ) global _listener logging . _acquireLock ( ) _listener = server logging . _releaseLock ( ) server . serve_until_stopped ( ) return Server ( ConfigSocketReceiver , ConfigStreamHandler , port , verify )