Advertisement
extrn

Untitled

Nov 2nd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. import os
  2. import mimetypes
  3.  
  4. class ServeStatic:
  5.     def __init__(self, static_dir, wrapped_app):
  6.         self.static_dir = os.path.abspath(static_dir) + os.sep
  7.         self.wrapped_app = wrapped_app
  8.  
  9.     def __call__(self, env, resp):
  10.         filepath = os.path.join(self.static_dir, env["PATH_INFO"][1:])
  11.  
  12.         if os.path.normpath(filepath).startswith(self.static_dir) and os.path.isfile(filepath):
  13.             mime, _ = mimetypes.guess_type(filepath)
  14.             resp('200 OK', [('Content-type', mime or 'application/octet-stream')])
  15.             with open(filepath, 'rb') as file:
  16.                 return [file.read()]
  17.         else:
  18.             return self.wrapped_app(env, resp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement