Ejemplo n.º 1
0
    def client_handler(self, client):
        """
        Handle client and trigger http_request handler
        """

        # Prepare request
        request = Request.parse(client.message)
        Log.info('Request: {}'.format(request))

        # Handle and process
        HttpServer.HTTP_REQUEST_HANDLER(client=client, request=request)
Ejemplo n.º 2
0
    def client_handler(self, client):
        """
        Handle client and trigger http_request handler
        """

        # Prepare request
        request = Request.parse(client.message)
        Log.info('Request: {}'.format(request))

        # Handle and process
        HttpServer.HTTP_REQUEST_HANDLER(
            client=client,
            request=request
        )
Ejemplo n.º 3
0
    def _loop(self):
        """
        Main loop
        :return:
        """
        if self.conn is None:
            Log.critical('Server is not initialized')
            sys.exit()

        Log.info('Server listen {}:{}'.format(self.addr[0], self.addr[1]))
        Log.debug('Waiting maximum {} clients'.format(self.clients))

        try:
            # Wait for a new clients
            while True:
                # New client accepted
                conn, addr = self.conn.accept()
                if self.LOG_CLIENTS:
                    Log.info('New client connected {}:{}'.format(addr[0], addr[1]))

                # Read message
                message = self._read(conn)

                # Dump request
                if self.DUMP_REQUEST and self.LOG_CLIENTS:
                    Log.info('Request received:\n{}\n'.format(message))

                # Handle client
                client = self.CLIENT_OBJECT(conn, addr, message)
                try:
                    self.client_handler(client)
                except Exception as e:
                    Log.warning('Client error: {}'.format(e.message))
                    self.client_error_handler(e)

                    # Dump response
                    if self.DUMP_RESPONSE:
                        Log.info('Response will send:\n{}\n'.format(''.join(client.buffer)))

                    if self.AUTOFLUSH_RESPONSE is True:
                        client.flush()
                    continue

                # Dump response
                if self.DUMP_RESPONSE:
                    Log.info('Response will send:\n{}\n'.format(''.join(client.buffer)))

                # Flush response
                if self.AUTOFLUSH_RESPONSE is True:
                    client.flush()

        except KeyboardInterrupt:
            # Shutting down server
            self._shutdown()
            Log.info('Server is down!')
            sys.exit()

        except Exception as e:
            # Shutting down server
            Log.error(e)

            # Exit or restart
            if self.RESTART_ON_ERROR is False:
                Log.warning('Server is down!')
                sys.exit()
            else:
                Log.info('Server is now restarting')

            self._shutdown(e)
Ejemplo n.º 4
0
    def _loop(self):
        """
        Main loop
        :return:
        """
        if self.conn is None:
            Log.critical('Server is not initialized')
            sys.exit()

        Log.info('Server listen {}:{}'.format(self.addr[0], self.addr[1]))
        Log.debug('Waiting maximum {} clients'.format(self.clients))

        try:
            # Wait for a new clients
            while True:
                # New client accepted
                conn, addr = self.conn.accept()
                if self.LOG_CLIENTS:
                    Log.info('New client connected {}:{}'.format(
                        addr[0], addr[1]))

                # Read message
                message = self._read(conn)

                # Dump request
                if self.DUMP_REQUEST and self.LOG_CLIENTS:
                    Log.info('Request received:\n{}\n'.format(message))

                # Handle client
                client = self.CLIENT_OBJECT(conn, addr, message)
                try:
                    self.client_handler(client)
                except Exception as e:
                    Log.warning('Client error: {}'.format(e.message))
                    self.client_error_handler(e)

                    # Dump response
                    if self.DUMP_RESPONSE:
                        Log.info('Response will send:\n{}\n'.format(''.join(
                            client.buffer)))

                    if self.AUTOFLUSH_RESPONSE is True:
                        client.flush()
                    continue

                # Dump response
                if self.DUMP_RESPONSE:
                    Log.info('Response will send:\n{}\n'.format(''.join(
                        client.buffer)))

                # Flush response
                if self.AUTOFLUSH_RESPONSE is True:
                    client.flush()

        except KeyboardInterrupt:
            # Shutting down server
            self._shutdown()
            Log.info('Server is down!')
            sys.exit()

        except Exception as e:
            # Shutting down server
            Log.error(e)

            # Exit or restart
            if self.RESTART_ON_ERROR is False:
                Log.warning('Server is down!')
                sys.exit()
            else:
                Log.info('Server is now restarting')

            self._shutdown(e)