Skip to content

Instantly share code, notes, and snippets.

@FND

FND/README.md Secret

Last active October 6, 2020 10:10
Show Gist options
  • Save FND/76cf34ddca1e258aa502d70ec98d551d to your computer and use it in GitHub Desktop.
Save FND/76cf34ddca1e258aa502d70ec98d551d to your computer and use it in GitHub Desktop.
WSGI streaming from disk (test case)
#!/usr/bin/env python3
import os
ROOT_DIR = os.path.dirname(__file__)
FILENAME = "empty.html"
def handler(environ, start_response):
method = environ["REQUEST_METHOD"]
if method != "GET":
start_response("405 Method Not Allowed", [])
return []
start_response("200 OK", [("Content-Type", "text/html")])
filepath = os.path.abspath(os.path.join(ROOT_DIR, FILENAME))
fh = open(filepath)
return (chunk.encode("utf-8") for chunk in fh)
if __name__ == "__main__":
from wsgiref.simple_server import make_server
host = "localhost"
port = 8080
srv = make_server(host, port, handler)
print("→ http://%s:%s" % (host, port))
srv.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment