Skip to content

Commit 7527e9d

Browse files
committed
HW 07: init
1 parent fb28e24 commit 7527e9d

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

homework_07/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Задача
2+
Реализовать сервер, частично реализующий протокол HTTP, в частности методы GET и HEAD, добиться того, что код проходит предоставленные функциональны тесты. Архитектуру выбрать на свое усмотрение, исходя из вариантов, рассмотренных на занятии. Провести нагрузочное тестирование с помощью ab или wrk
3+
4+
# Запуск тестов
5+
```
6+
sh ab -n 1000 -c 10 http://localhost:8080/index.html
7+
8+
wrk -t12 -c400 -d30s http://localhost:8080/index.html
9+
```

homework_07/main.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import os
2+
import socket
3+
import threading
4+
5+
HOST = "localhost"
6+
PORT = 8080
7+
DOCUMENT_ROOT = "./www"
8+
9+
10+
def handle_request(client_socket):
11+
request = ""
12+
try:
13+
while True:
14+
data = client_socket.recv(1024)
15+
if not data:
16+
break
17+
request += data.decode()
18+
# 2. get headers
19+
if not request:
20+
return
21+
headers = request.split("\r\n")
22+
# 3. split headers to methods
23+
method, path, protocol = headers[0].split(" ")
24+
# 4. give back response
25+
if method in ["GET", "HEAD"]:
26+
if path == "/":
27+
path = os.path.join(DOCUMENT_ROOT, "index.html")
28+
if os.path.exists(path):
29+
with open(path, "rb") as f:
30+
content = f.read()
31+
response = (
32+
f"{protocol} 200 OK\r\nContent-Length: {len(content)}\r\n".encode()
33+
)
34+
if method == "GET":
35+
response = response + content
36+
else:
37+
response = b"HTTP/1.1 404 Not Found\r\nFile Not Found"
38+
39+
client_socket.sendall(response)
40+
finally:
41+
client_socket.close()
42+
43+
44+
def start_server():
45+
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
46+
server_address = (HOST, PORT)
47+
server_socket.bind(server_address)
48+
server_socket.listen(5) # Allows up to 5 queued connections
49+
print(f"Server listening on {server_address}")
50+
try:
51+
while True:
52+
client_socket, _ = server_socket.accept()
53+
client_thread = threading.Thread(
54+
target=handle_request, args=(client_socket,)
55+
)
56+
client_thread.start()
57+
finally:
58+
server_socket.close()
59+
60+
61+
if __name__ == "__main__":
62+
start_server()

homework_07/pyproject.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[tool.poetry]
2+
name = "homework-07"
3+
version = "0.1.0"
4+
description = "Реализовать сервер, частично реализующий протокол HTTP. Провести нагрузочное тестирование"
5+
authors = ["Vladislav Kozlov <[email protected]>"]
6+
license = "MIT"
7+
readme = "README.md"
8+
9+
[tool.poetry.dependencies]
10+
python = "^3.12"
11+
12+
13+
[build-system]
14+
requires = ["poetry-core"]
15+
build-backend = "poetry.core.masonry.api"

homework_07/www/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title> Hello from a simple web server! </title>
6+
</head>
7+
<body>
8+
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)