1lab/server.py
2024-04-16 00:24:42 +03:00

29 lines
972 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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()