def sh_server_controller(request): """ Отсанавливает сервер, требудется ввод пароля :param request: dict :return: dict """ if request.get('data') == '12345': return make_200(request, 'Server has been shutdown', key='shd') else: raise Exception(401)
def timestamp_controller(request): return make_200(request, datetime.now().timestamp())
def echo_controller(request): return make_200(request, request.get('data'))
def test_make_response_200(initial_request, expected_data, expected_time, code_200): response = make_200(initial_request, expected_data, expected_time) code = response.get('code') assert code == code_200
sock.bind((host, port)) sock.listen(5) print(f'Server started with {host}:{port}') while True: client, (client_host, client_port) = sock.accept() print(f'Client {client_host}:{client_port} was connected') bytes_request = client.recv(buffersize) request = json.loads(bytes_request) if vilidate_request(request): action = request.get('action') if action == 'echo': try: response = make_200(request, request.get('data')) print(f'Request: {bytes_request.decode()}') except Exception as er: response = make_500(request) print(er) try: print(f'Request: {bytes_request.decode()}') # client.send(bytes_request) response = make_200(request, 200, request.get('data')) # client.send({'action': request.get("action"), 'code': 200, 'data': request.get('data')}) except Exception as err: request = make_500(request) print(err) else: response = make_400(request, 400, 'Request is not valide')
def test_make_200(initial_request, code_200, expected_data, expected_time): response = make_200(initial_request, expected_data, date=expected_time) assert response.get('code') == code_200
def test_code_make_200(): response = make_200(REQUEST, DATA, date=TIME) action = response.get('code') assert action == CODE
def test_time_make_200(): response = make_200(REQUEST, DATA, date=TIME) action = response.get('time') assert action == TIME
def test_data_make_200(): response = make_200(REQUEST, DATA, date=TIME) action = response.get('data') assert action == DATA