def main(): client = socket(AF_INET, SOCK_STREAM) try: addr = sys.argv[1] except IndexError: CLIENT_LOGGER.warning("Нет адреса сервера") addr = 'localhost' try: port = int(sys.argv[2]) except IndexError: CLIENT_LOGGER.warning("Выбран стандарный порт: 7777") port = 7777 except ValueError: CLIENT_LOGGER.error("Порт должен быть целым числом") client.close() sys.exit(0) CLIENT_LOGGER.debug("1. Соединение") client.connect((addr, port)) message = create_presence() CLIENT_LOGGER.debug("2. Отправка сообщения") send_message(client, message) CLIENT_LOGGER.debug("3. Ожидание ответа") response = get_message(client) response = translate_message(response) print(response) CLIENT_LOGGER.info('Response: ' + str(response)) client.close()
def main(): global server global client server = socket(AF_INET, SOCK_STREAM) try: addr = sys.argv[1] except IndexError: addr = '0.0.0.0' try: port = int(sys.argv[2]) except IndexError: port = 7777 except ValueError: print('Введите целое число') sys.exit(0) # a.setsockopt(SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value)); server.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) server.bind((addr, port)) server.listen(1) while True: client, addr = server.accept() message = get_message(client) print(message) response = server_response(message) send_message(client, response) client.close()
def main(): global server global client server = socket(AF_INET, SOCK_STREAM) try: addr = sys.argv[1] except IndexError: SERVER_LOGGER.warning("Нет адреса, использован стандартный") addr = '' try: port = int(sys.argv[2]) except IndexError: SERVER_LOGGER.warning("Выбран стандарный порт: 7777") port = 7777 except ValueError: SERVER_LOGGER.warning("Порт должен быть целым числом") sys.exit(0) # a.setsockopt(SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value)); server.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) server.bind((addr, port)) server.listen(1) while True: client, addr = server.accept() SERVER_LOGGER.debug("1. Подтверждение") message = get_message(client) SERVER_LOGGER.debug("2. Получение сообщения") print(message) SERVER_LOGGER.info('Message: ' + str(message)) response = server_response(message) SERVER_LOGGER.debug("3. Отправка ответа") send_message(client, response) client.close()
def read_requests(r_clients, all_clients): messages = [] for sock in r_clients: try: message = get_message(sock) messages.append(message) except BaseException: print('Client {} {} disconnected'.format(sock.fileno(), sock.getpeername())) all_clients.remove(sock) return messages
def main(): client = socket(AF_INET, SOCK_STREAM) try: addr = sys.argv[1] except IndexError: CLIENT_LOGGER.warning("Нет адреса сервера") addr = 'localhost' try: port = int(sys.argv[2]) except IndexError: CLIENT_LOGGER.warning("Выбран стандарный порт: 7777") port = 7777 except ValueError: CLIENT_LOGGER.error("Порт должен быть целым числом") client.close() sys.exit(0) try: mode = sys.argv[3] except IndexError: mode = 'r' CLIENT_LOGGER.debug("Начало соединения") client.connect((addr, port)) message = create_message() send_message(client, message) CLIENT_LOGGER.debug("Ожидание ответа") response = get_message(client) response = translate_message(response) print(f'Установлено соединение с сервером.') CLIENT_LOGGER.info('Ответ: ' + str(response)) if response['response'] == OK: if mode == 'r': read_messages(client) elif mode == 'w': write_messages(client) else: raise Exception("Invalid mode") # Старый код # CLIENT_LOGGER.debug("1. Соединение") # client.connect((addr, port)) # message = create_message() # CLIENT_LOGGER.debug("2. Отправка сообщения") # send_message(client, message) # CLIENT_LOGGER.debug("3. Ожидание ответа") # response = get_message(client) # response = translate_message(response) # print(response) # CLIENT_LOGGER.info('Response: ' + str(response)) client.close()
def main(): client = socket(AF_INET, SOCK_STREAM) try: addr = sys.argv[1] except IndexError: addr = 'localhost' try: port = int(sys.argv[2]) except IndexError: port = 7777 except ValueError: print('Введите целое число') client.close() sys.exit(0) client.connect((addr, port)) message = create_presence() send_message(client, message) response = get_message(client) response = translate_message(response) print(response) client.close()
def read_messages(client): while True: message = get_message(client) print(message)