-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
36 lines (28 loc) · 897 Bytes
/
server.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import http.server
from http.server import SimpleHTTPRequestHandler as HTTPHandler
"""
A simple http server (python3)
"""
class CustomHttpRequestHandler(HTTPHandler):
def do_GET(self):
if (self.path == "/"):
self.path = "index.html"
elif (self.path == "/favicon.ico"):
self.path = "img/favicon.ico"
return HTTPHandler.do_GET(self)
port = 8080 if not sys.argv[1:] \
else int(sys.argv[1])
server_addr = ("127.0.0.1", port)
handler_class = CustomHttpRequestHandler
handler_class.protocol_version = "HTTP/1.0"
httpd = http.server.HTTPServer(server_addr, handler_class)
# print server info
sock_addr = httpd.socket.getsockname()
print(f"Serving HTTP server on {sock_addr[0]}:{sock_addr[1]}")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("end server")