Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import mimetypes
- class ServeStatic:
- def __init__(self, static_dir, wrapped_app):
- self.static_dir = os.path.abspath(static_dir) + os.sep
- self.wrapped_app = wrapped_app
- def __call__(self, env, resp):
- filepath = os.path.join(self.static_dir, env["PATH_INFO"][1:])
- if os.path.normpath(filepath).startswith(self.static_dir) and os.path.isfile(filepath):
- mime, _ = mimetypes.guess_type(filepath)
- resp('200 OK', [('Content-type', mime or 'application/octet-stream')])
- with open(filepath, 'rb') as file:
- return [file.read()]
- else:
- return self.wrapped_app(env, resp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement