-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
39 lines (31 loc) · 1.14 KB
/
Copy pathmain.py
File metadata and controls
39 lines (31 loc) · 1.14 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
import http.server
import socketserver
import logging
import json
# Načítanie konfigurácie zo súboru
with open('config.json', 'r') as config_file:
config = json.load(config_file)
PORT = config['port']
HOST = config['host']
IP_TO_NAME = config['ip_to_name']
# Nastavenie logovania
logging.basicConfig(filename='server_log.txt', level=logging.INFO, format='%(message)s')
# Vlastný handler
class MyHandler(http.server.SimpleHTTPRequestHandler):
def log_request(self, code='-', size='-'):
client_ip = self.client_address[0]
device_name = IP_TO_NAME.get(client_ip, "Unknown Device")
request_path = self.path
status_code = code
# Formátovaný log
log_entry = (
f"[{self.log_date_time_string()}] "
f"MENO: {device_name} | IP: {client_ip} | SUBOR: {request_path} | STATUS: {status_code}"
)
logging.info(log_entry)
print(log_entry)
# Spustenie servera
Handler = MyHandler
with socketserver.TCPServer((HOST, PORT), Handler) as httpd:
print(f"✅ Server beží na http://{HOST}:{PORT}")
httpd.serve_forever()