-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-http-server.py
executable file
·68 lines (55 loc) · 1.98 KB
/
simple-http-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
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
#!/usr/bin/env python
# SPDX-License-Identifier: GPL-3.0-or-later
# -*- coding: utf-8 -*-
import argparse
import logging
import os
import re
import socket
from http.server import HTTPServer, SimpleHTTPRequestHandler
class HTTPServerV6(HTTPServer):
address_family = socket.AF_INET6
class MyHandler(SimpleHTTPRequestHandler):
def do_GET(self):
try:
address = self.address_string()
if not address.startswith("47.128."):
logging.debug(self.requestline)
return SimpleHTTPRequestHandler.do_GET(self)
except BrokenPipeError:
logging.info("pipe broken")
except Exception as e:
logging.exception("MyHandler: %s", format(e), exc_info=False)
return None
def main():
logging.basicConfig(
format="%(asctime)s %(levelname)s %(message)s", level=logging.INFO
)
logging.debug("Parsing args...")
parser = argparse.ArgumentParser()
parser.add_argument(
"--address", type=str, help="default: localhost", default="localhost"
)
parser.add_argument(
"--directory", type=str, help="default: /dev/null", default="/dev/null"
)
parser.add_argument("--port", type=int, help="default: 1234", default=1234)
parser.add_argument(
"--is_ipv6", type=bool, help="mark ADDRESS as IPv6", default=False
)
args = parser.parse_args()
os.chdir(args.directory)
if args.is_ipv6 or re.match(":", args.address):
address = str(args.address).replace("[", "").replace("]", "")
server = HTTPServerV6((address, args.port), MyHandler)
else:
server = HTTPServer((args.address, args.port), MyHandler)
logging.info("running at %s at %i in %s", args.address, args.port, args.directory)
try:
server.serve_forever()
except KeyboardInterrupt:
logging.info("catched Ctrl-C")
except Exception as e:
logging.exception("main: %s", format(e), exc_info=False)
if __name__ == "__main__":
main()