Skip to content

Commit aba8e14

Browse files
committed
🎄 refactor(serve.py): modularize code into functions
Refactored serve.py, which serves the generated website, by modularizing the code into functions for better readability and maintainability.
1 parent 714bff4 commit aba8e14

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

‎docs/serve.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@
22
import http.server
33
import socketserver
44

5-
# tasklist
6-
# /IM py37.exe /F
7-
#
8-
hostname = socket.gethostname()
9-
PORT = 8000
10-
IP = socket.gethostbyname(hostname)
5+
def get_server_address():
6+
"""Returns the server's IP address and port."""
7+
hostname = socket.gethostname()
8+
ip = socket.gethostbyname(hostname)
9+
return ip, 8000
1110

12-
Handler = http.server.SimpleHTTPRequestHandler
13-
with socketserver.TCPServer(("", PORT), Handler) as httpd:
14-
print(f"Serving on: {IP}:{PORT}")
15-
try:
16-
httpd.serve_forever()
17-
except KeyboardInterrupt:
18-
print("Shutting down serve.py...")
11+
def run_server():
12+
"""Runs the HTTP server."""
13+
ip, port = get_server_address()
14+
handler = http.server.SimpleHTTPRequestHandler
15+
16+
with socketserver.TCPServer(("", port), handler) as httpd:
17+
print(f"Serving on: {ip}:{port}")
18+
try:
19+
httpd.serve_forever()
20+
except KeyboardInterrupt:
21+
print("Shutting down server...")
22+
23+
if __name__ == "__main__":
24+
run_server()

0 commit comments

Comments
 (0)