diff options
| author | Terry Truong <terry06890@gmail.com> | 2022-09-07 11:37:37 +1000 |
|---|---|---|
| committer | Terry Truong <terry06890@gmail.com> | 2022-09-07 11:37:37 +1000 |
| commit | daccbbd9c73a5292ea9d6746560d7009e5aa666d (patch) | |
| tree | 9156bf011ab6302eb3c0d219d40587d594f51841 /backend/server.py | |
| parent | 1a7fe33edafa68a6f759d124bdeee673ff9cf9ff (diff) | |
Add python type annotations
Also use consistent quote symbols
Also use 'is None' instead of '== None'
Also use 'if list1' instead of 'if len(list1) > 0'
Diffstat (limited to 'backend/server.py')
| -rwxr-xr-x | backend/server.py | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/backend/server.py b/backend/server.py index 48d6c3f..5b0d26b 100755 --- a/backend/server.py +++ b/backend/server.py @@ -1,6 +1,7 @@ #!/usr/bin/python3 -import sys, os +from typing import Iterable +import os from wsgiref import simple_server, util import mimetypes from tilo import application @@ -11,26 +12,26 @@ Runs a basic dev server that serves a WSGI script and image files """) parser.parse_args() -# WSGI handler that uses 'application', but also serves image files -def wrappingApp(environ, start_response): - urlPath = environ["PATH_INFO"] - if urlPath.startswith("/data/"): +def wrappingApp(environ: dict[str, str], start_response) -> Iterable[bytes]: + """ WSGI handler that uses 'application', but also serves image files """ + urlPath = environ['PATH_INFO'] + if urlPath.startswith('/data/'): # Run WSGI script return application(environ, start_response) - elif urlPath.startswith("/tolData/img/"): + elif urlPath.startswith('/tolData/img/'): # Serve image file imgPath = os.path.join(os.getcwd(), urlPath[1:]) if os.path.exists(imgPath): imgType = mimetypes.guess_type(imgPath)[0] - start_response("200 OK", [("Content-type", imgType)]) - return util.FileWrapper(open(imgPath, "rb")) + start_response('200 OK', [('Content-type', imgType)]) + return util.FileWrapper(open(imgPath, 'rb')) else: - start_response("404 Not Found", [("Content-type", "text/plain")]) - return [b"No image found"] + start_response('404 Not Found', [('Content-type', 'text/plain')]) + return [b'No image found'] else: - start_response("404 Not Found", [("Content-type", "text/plain")]) - return [b"Unrecognised path"] + start_response('404 Not Found', [('Content-type', 'text/plain')]) + return [b'Unrecognised path'] # Start server with simple_server.make_server('', 8000, wrappingApp) as httpd: - print("Serving HTTP on port 8000...") + print('Serving HTTP on port 8000...') httpd.serve_forever() |
