def _connection(self, *args, **kwargs): """ Creation socket :param args: :param kwargs: :return: """ try: # Create socket self.conn = socket.socket(*args, **kwargs) self.conn.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Bind address self.conn.bind(self.addr) # Listen clients self.conn.listen(self.clients) except socket.error as e: Log.critical(e) sys.exit()
def _connection(self, *args, **kwargs): """ Creation socket :param args: :param kwargs: :return: """ try: # Create socket self.conn = socket.socket(*args, **kwargs) self.conn.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Bind address self.conn.bind(self.addr) # Listen clients self.conn.listen(self.clients) except socket.error as e: Log.critical(e) sys.exit()
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)
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)