commit 8fdb726e04a7ed1d1075aed4bde4f3e34672923e Author: Anton Date: Tue Apr 16 00:24:42 2024 +0300 1 lab diff --git a/server.py b/server.py new file mode 100644 index 0000000..9b70885 --- /dev/null +++ b/server.py @@ -0,0 +1,29 @@ +import socket + +# Создаем сокет +server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + +# Задаем хост и порт для сервера +host = "localhost" +port = 12346 + +# Связываем сокет с адресом и портом +server_socket.bind((host, port)) + +# Слушаем входящие подключения +server_socket.listen(3) +print(f"Сервер запущен на http://{host}:{port}/") + +while True: + # Принимаем входящее подключение + client_socket, client_address = server_socket.accept() + + # Отвечаем клиенту + response_body = "Hello" + response_headers = "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: " + str(len(response_body)) + "\n\n" + response = response_headers + response_body + + client_socket.sendall(response.encode("utf-8")) + + # Закрываем соединение с клиентом + client_socket.close() \ No newline at end of file