def handle_tcp_request(bytes_request): request = json.loads(bytes_request.decode()) if validate_request(request): action = request.get('action') controller = resolve(action) if controller: try: response = controller(request) logging.debug(f'Client send request {request}') except Exception as err: response = response = make_response( request, 500, f'Internal server error' ) logging.critical(f'Exception - {err}') else: response = response = make_response( request, 404, f'Action {action} is not supported' ) logging.error(f'Client call action with name {action}') else: response = make_response( request, 400, 'Wrong request' ) logging.error(f'Client send wrong request {request}') string_response = json.dumps(response) return string_response.encode()
def handle_default_request(raw_request): request = json.loads(raw_request.decode()) if validate_request(request): action_name = request.get('action') controller = resolve(action_name) if controller: try: logging.debug( f'Controller {action_name} resolved with request: {request}' ) response = controller(request) except Exception as err: logging.critical(f'Controller {action_name} error: {err}', exc_info=err) response = make_response(request, 500, 'Internal server error') else: logging.error(f'Controller {action_name} not found') response = make_response( request, 404, f'Action with name {action_name} not supported') else: logging.error(f'Controller wrong request: {request}') response = make_response(request, 400, 'wrong request format') return json.dumps(response).encode()
def handle_default_request(raw_request): request = json.loads(raw_request.decode()) if validate_request(request): action_name = request.get('action') controller = resolve(action_name) if controller: try: # print(f'Controller {action_name} resolved with request: {request}') logging.debug( f'Controller {action_name} resolved with request: {request}' ) # rec2mongo({'level': 'debug', 'Controller': action_name, 'request': str(request), 'time': str(datetime.now())}) response = controller(request) except Exception as err: # print(f'Controller {action_name} error: {err}') logging.critical(f'Controller {action_name} error: {err}') # rec2mongo({'level': 'critical', 'Controller': action_name, 'error': str(err), 'time': str(datetime.now())}) response = make_response(request, 500, 'Internal server error') else: # print(f'Controller {action_name} not found') logging.error(f'Controller {action_name} not found') # rec2mongo({'level': 'error', 'Controller': action_name, 'error': 'Controller not found', 'time': str(datetime.now())}) response = make_response( request, 404, f'Action with name {action_name} not supported') else: # print(f'Controller wrong request: {request}') logging.error(f'Controller wrong request: {request}') # rec2mongo({'level': 'error', 'request': str(request), 'time': str(datetime.now())}) response = make_response(request, 400, 'wrong request format') return json.dumps(response).encode()
def handle_default_request(raw_request): request = json.loads(raw_request.decode()) if validate_request(request): action_name = request.get('action') controller = resolve(action_name) if controller: try: logging.debug( 'controller: {} resolved with request: {}'.format( action_name, request)) response = controller(request) except Exception as err: logging.critical('controller: {} error: {}'.format( action_name, err)) response = make_response(request, 500, 'internal server error') else: logging.error('controller: {} not found'.format(action_name)) response = make_response( request, 404, 'action with name {} not supported'.format(action_name)) else: logging.error('controller wrong request: {}'.format(request)) response = make_response(request, 400, 'wrong request format') return json.dumps(response).encode()
def handle_default_request(raw_request): # в строку потом в словарь request = json.loads(raw_request.decode()) # проходим валидацию if validate_request(request): action_name = request.get('action') # извлекаем контроллер controller = resolve(action_name) # if action == 'echo': if controller: try: ''' фиксируем debug ''' logging.debug( f'Controller {action_name} resolved with request: {request}' ) response = controller(request) except Exception as err: ''' фикусируем критическую ситуацию ''' logging.critical(f'Controller {action_name} error: {err}') response = make_response(request, 500, 'Internal server error') else: ''' фиксируем симантические ошибки ''' logging.error(f'Controller {action_name} not found') response = make_response( request, 404, f'Action with name "{action_name}" not supported') else: ''' фиксируем симантические ошибки ''' logging.error(f'Controller wrong request: {request}') response = make_response(request, 400, 'Wrong request format!') return json.dumps(response).encode()
def resolve(self, url, html='', referer=None): #try: r = self.s.get(url).text fid = re.findall('id\s*=\s*[\"\']([^\"\']+).+?jokersplayer.+?\.js', r)[0] u = 'http://www.jokersplayer.xyz/embed.php?u=' + fid import resolvers return resolvers.resolve(u, referer=url)
def handle_tcp_request(bytes_request): request = json.loads(bytes_request.decode()) if validate_request(request): action = request.get('action') controller = resolve(action) if controller: try: response = controller(request) logging.debug(f'Client send request {request}') except Exception as err: response = make_response(request, 500) logging.critical(f'Exception — {err}') else: logging.critical('Invalid action') response = make_response(request, 404) else: logging.critical('Invalid request') response = make_response(request, 404) string_response = json.dumps(response) return string_response.encode()
def handle_default_request(raw_request, logger): request = json.loads(raw_request.decode()) print(f'request: {request}') if validate_request(request): action_name = request.get('action') controller = resolve(action_name) # ----------------------------------------------- if controller: try: logger.debug( f'Controller {controller} resolved with request: {request}' ) # print(f'Controller {controller} resolved with request: {request}') # response = make_response(request, 200, request.get('data')) response = controller(request) except Exception as err: logger.critical(f'Controller {controller} error: {err}') # print(f'Controller {controller} error: {err}') response = make_response(request, 500, 'Internal Server Error!') # ------------------------------------------------ else: logger.error(f'Controller {controller} not found!') # print(f'Controller {controller} not found!') response = make_response( request, 404, f'Action with name {action_name} is not supported!') else: logger.error(f'Wrong request!') # print(f'Wrong request!') response = make_response(request, 400, 'Wrong request format!') return json.dumps(response).encode()
def test_resolve(): assert resolvers.resolve(ACTION_NAME, SERVER_ACTIONS) == echo_controller
def test_resolve(initial_action): assert resolve(initial_action) is not None
print(f'Server started with {host}:{port}') while True: client, address = sock.accept() client_host, client_port = address print(f'Client was detected {client_host}:{client_port}') bytes_request = client.recv(config.get('buffersize')) request = json.loads(bytes_request.decode()) if validate_request(request): action = request.get('action') controller = resolve(action) if controller: try: response = controller(request) print(f'Client {host}:{port} send request {request}') except Exception as err: response = (request, 500, f'Internal server error') print(f'Exception - {err}') else: response = (request, 404, f'Action {action} is not supported') print(f'Client {host}:{port} call action with name {action}') else: response = (request, 400, 'Wrong request') print(f'Client {host}:{port} send wrong request {request}')
try: sock = socket() sock.bind((host, port,)) sock.listen(5) logging.info(f'Server was started with {host}:{port}') while True: client, address = sock.accept() logging.info(f'Client was connected with {address[0]}:{address[1]}') b_request = client.recv(default_config.get('buffersize')) request = json.loads(b_request.decode()) if validate_request(request): action_name = request.get('action') controller = resolve(action_name) if controller: try: logging.debug(f'Controller {action_name} resolved with request: {request}') response = controller(request) except Exception as err: logging.critical(f'Controller {action_name} error: {err}') response = make_response(request, 500, 'Internal server error') else: logging.error(f'Controller {action_name} not found') response = make_response(request, 404, f'Action with name {action_name} not supported') else: logging.error(f'Controller wrong request: {request}') response = make_response(request, 400, 'wrong request format') client.send( json.dumps(response).encode())