3 # Copyright (c) 2021 bandali
5 # Copying and distribution of this file, with or without modification,
6 # are permitted in any medium without royalty provided the copyright
7 # notice and this notice are preserved. This file is offered as-is,
8 # without any warranty.
11 from http
.server
import SimpleHTTPRequestHandler
15 PORT
= int(os
.getenv('PORT', 8000))
17 print('PORT must be in integer')
20 ADDR
= os
.getenv('ADDR', '127.0.0.1')
22 class HttpRequestHandler(SimpleHTTPRequestHandler
):
23 extensions_map
= dict(SimpleHTTPRequestHandler
.extensions_map
)
24 extensions_map
.update({'': 'text/plain;charset=UTF-8'})
25 extensions_map
.update({'.txt': 'text/plain;charset=UTF-8'})
28 if not os
.path
.isfile(os
.getcwd() + self
.path
):
29 exts
= ['html', 'txt']
31 p
= ".".join((self
.path
, ext
))
32 if os
.path
.isfile("".join((os
.getcwd(), p
))):
37 class ReuseAddrTCPServer(socketserver
.TCPServer
):
38 allow_reuse_address
= True
40 with
ReuseAddrTCPServer((ADDR
, PORT
), HttpRequestHandler
) as httpd
:
42 print("serving at port", PORT
)
44 except KeyboardInterrupt:
46 except Exception as e
: