summaryrefslogtreecommitdiffstats
path: root/.local/bin/http-dir
blob: a1dd19da4d11d8a041a9b88188c8d7bddac85397 (plain) (blame)
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
#!/usr/bin/env python3

import argparse
import http.server
import os 
import sys

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--bind', '-b', metavar='ADDRESS',default='', help='specify alternate bind address (default: all interfaces)')
    parser.add_argument('--directory', '-d', default=os.getcwd(), help='specify alternate directory (default: current directory)')
    parser.add_argument('port', action='store', default=8000, type=int,
                     nargs='?', help='specify alternate port (default: 8000)')
    args = parser.parse_args()

    class app(http.server.SimpleHTTPRequestHandler):
        def __init__(self, request, client_address, server):
            super().__init__(request, client_address, server, directory=args.directory)

    with http.server.ThreadingHTTPServer((args.bind, args.port), app) as httpd:
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            sys.exit()



if __name__ == '__main__':
    main()