From 412ae04909d3e5aa6dc54c331f0596ea1c8060e2 Mon Sep 17 00:00:00 2001 From: Dima Date: Mon, 15 Apr 2024 19:10:56 +0300 Subject: [PATCH] v.2 --- main.py | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index e0b8028..aec96c5 100644 --- a/main.py +++ b/main.py @@ -1,15 +1,37 @@ import socket +def generate_html_response(count): + html_content = """ + + + Страница счетчика + + +

Счетчик: {}

+ + + """.format(count) + return html_content + serv_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - -serv_sock.bind(('127.0.0.1', 12345)) - +serv_sock.bind(('0.0.0.0', 12345)) serv_sock.listen(5) +count = 0 + while True: client_sock, client_addr = serv_sock.accept() data = client_sock.recv(1024) - print(data.decode()) - #client_sock.sendall(data) - client_sock.close() + request = data.decode() + print(request) + if "GET /count" in request: + count += 1 + html_response = generate_html_response(count) + http_response = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n" + html_response + client_sock.sendall(http_response.encode()) + else: + http_response = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n " + client_sock.sendall(http_response.encode()) + + client_sock.close() \ No newline at end of file