-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.py
More file actions
executable file
·36 lines (32 loc) · 1.02 KB
/
serve.py
File metadata and controls
executable file
·36 lines (32 loc) · 1.02 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import http.server
import socketserver
import argparse
import sys
parser = argparse.ArgumentParser(description='Simple web server.')
parser.add_argument('--port', type=int, nargs='?', help='port', default=8080)
parser.add_argument('--host', type=str, nargs='?', help='host', default='')
args = parser.parse_args()
PORT = args.port
HOST = args.host
Handler = http.server.SimpleHTTPRequestHandler
for attempt in range(5):
try:
with socketserver.TCPServer((HOST, PORT), Handler) as httpd:
h = httpd.socket.getsockname()[0]
host = 'localhost' if h == '0.0.0.0' else h
print(f'serving at http://{host}:{PORT}')
httpd.serve_forever()
except OSError as e:
if e.errno == 48:
print(f'Port {PORT} in use, trying port {PORT + 1}')
PORT += 1
continue
else:
raise e
except KeyboardInterrupt:
print('KeyboardInterrupt')
sys.exit(0)
else:
break