Skip to content

Commit

Permalink
style: blackify non-caching-server-3.7.py
Browse files Browse the repository at this point in the history
  • Loading branch information
joanise committed Sep 29, 2021
1 parent fe7df92 commit ee480fb
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions misc-utils/non-caching-server-3.7.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
from functools import partial
from http import HTTPStatus
from http.server import test # type: ignore
from http.server import SimpleHTTPRequestHandler, CGIHTTPRequestHandler
from http.server import CGIHTTPRequestHandler, SimpleHTTPRequestHandler


class NonCachingHTTPRequestHandler(SimpleHTTPRequestHandler):
Expand Down Expand Up @@ -144,11 +144,10 @@ def send_head(self):
f = None
if os.path.isdir(path):
parts = urllib.parse.urlsplit(self.path)
if not parts.path.endswith('/'):
if not parts.path.endswith("/"):
# redirect browser - doing basically what apache does
self.send_response(HTTPStatus.MOVED_PERMANENTLY)
new_parts = (parts[0], parts[1], parts[2] + '/',
parts[3], parts[4])
new_parts = (parts[0], parts[1], parts[2] + "/", parts[3], parts[4])
new_url = urllib.parse.urlunsplit(new_parts)
self.send_header("Location", new_url)
self.end_headers()
Expand All @@ -162,7 +161,7 @@ def send_head(self):
return self.list_directory(path)
ctype = self.guess_type(path)
try:
f = open(path, 'rb')
f = open(path, "rb")
except OSError:
self.send_error(HTTPStatus.NOT_FOUND, "File not found")
return None
Expand All @@ -181,26 +180,35 @@ def send_head(self):
raise


if __name__ == '__main__':
if __name__ == "__main__":
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--cgi', action='store_true',
help='Run as CGI Server')
parser.add_argument('--bind', '-b', default='', metavar='ADDRESS',
help='Specify alternate bind address '
'[default: all interfaces]')
parser.add_argument('--directory', '-d', default=os.getcwd(),
help='Specify alternative directory '
'[default:current directory]')
parser.add_argument('port', action='store',
default=8000, type=int,
nargs='?',
help='Specify alternate port [default: 8000]')
parser.add_argument("--cgi", action="store_true", help="Run as CGI Server")
parser.add_argument(
"--bind",
"-b",
default="",
metavar="ADDRESS",
help="Specify alternate bind address " "[default: all interfaces]",
)
parser.add_argument(
"--directory",
"-d",
default=os.getcwd(),
help="Specify alternative 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()
if args.cgi:
handler_class = CGIHTTPRequestHandler
else:
handler_class = partial(NonCachingHTTPRequestHandler,
directory=args.directory)
handler_class = partial(NonCachingHTTPRequestHandler, directory=args.directory)
test(HandlerClass=handler_class, port=args.port, bind=args.bind)

0 comments on commit ee480fb

Please sign in to comment.