v.2
This commit is contained in:
parent
69eda915c7
commit
412ae04909
34
main.py
34
main.py
@ -1,15 +1,37 @@
|
|||||||
import socket
|
import socket
|
||||||
|
|
||||||
|
def generate_html_response(count):
|
||||||
|
html_content = """
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Страница счетчика</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Счетчик: {}</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
""".format(count)
|
||||||
|
return html_content
|
||||||
|
|
||||||
serv_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
serv_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
serv_sock.bind(('0.0.0.0', 12345))
|
||||||
serv_sock.bind(('127.0.0.1', 12345))
|
|
||||||
|
|
||||||
serv_sock.listen(5)
|
serv_sock.listen(5)
|
||||||
|
|
||||||
|
count = 0
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
client_sock, client_addr = serv_sock.accept()
|
client_sock, client_addr = serv_sock.accept()
|
||||||
data = client_sock.recv(1024)
|
data = client_sock.recv(1024)
|
||||||
print(data.decode())
|
request = data.decode()
|
||||||
#client_sock.sendall(data)
|
print(request)
|
||||||
client_sock.close()
|
|
||||||
|
|
||||||
|
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()
|
Loading…
Reference in New Issue
Block a user