-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer_server.py
More file actions
102 lines (79 loc) · 2.59 KB
/
analyzer_server.py
File metadata and controls
102 lines (79 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import socket
import threading
from datetime import datetime
import json
HOST = "127.0.0.1"
PORT = 8080
request_count = 0
path_stats = {}
blocked_paths = ["/admin"]
log_file = "logs.txt"
lock = threading.Lock()
def handle_client(client_socket, addr):
global request_count
request = client_socket.recv(1024).decode(errors="ignore")
lines = request.split("\r\n")
if not lines or len(lines[0].split()) < 3:
client_socket.close()
return
method, path, version = lines[0].split()
with lock:
request_count += 1
path_stats[path] = path_stats.get(path, 0) + 1
# Log entry
log_entry = {
"time": str(datetime.now()),
"ip": addr[0],
"method": method,
"path": path
}
with open(log_file, "a", encoding="utf-8") as f:
f.write(json.dumps(log_entry) + "\n")
print(f"[{datetime.now()}] {addr[0]} -> {method} {path}")
# Blocked path
if path in blocked_paths:
response_body = "<h1>403 Forbidden</h1><p>Bu sayfa engellendi.</p>"
response = (
"HTTP/1.1 403 Forbidden\r\n"
"Content-Type: text/html\r\n\r\n"
+ response_body
)
elif path == "/stats":
stats_html = "<h1>Server Stats</h1>"
stats_html += f"<p>Total Requests: {request_count}</p><ul>"
for p, count in path_stats.items():
stats_html += f"<li>{p} : {count}</li>"
stats_html += "</ul>"
response = (
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n\r\n"
+ stats_html
)
elif path == "/logs":
try:
with open(log_file, "r", encoding="utf-8") as f:
logs = f.read()
except:
logs = "No logs yet."
response = (
"HTTP/1.1 200 OK\r\n"
"Content-Type: application/json\r\n\r\n"
+ logs
)
else:
response_body = f"<h1>Merhaba Mahir 😎</h1><p>Girdigin path: {path}</p>"
response = (
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n\r\n"
+ response_body
)
client_socket.send(response.encode())
client_socket.close()
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
server.listen(5)
print(f"Advanced Analyzer Server {HOST}:{PORT} calisiyor...")
while True:
client_socket, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(client_socket, addr))
thread.start()