From 8b491e50b5a554643a068f66f7257d38c6ac7e7c Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Tue, 22 Oct 2019 13:27:21 -0400 Subject: [PATCH] Basic wsgi app working inside twisted --- .gitignore | 2 ++ main.py | 21 +++++++++++++++++++++ phial.py | 25 +++++++++++++++++++++++++ requirements.txt | 1 + 4 files changed, 49 insertions(+) create mode 100644 .gitignore create mode 100644 main.py create mode 100644 phial.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..896cdee --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env/ +__pycache__/ diff --git a/main.py b/main.py new file mode 100644 index 0000000..2fa7191 --- /dev/null +++ b/main.py @@ -0,0 +1,21 @@ +import phial + +def handler(request): + return phial.Response( + status='200 OK', + headers=[('Content-Type', 'text/plain')], + content=[b'Hello, world\n'], + ) + +app = phial.App(handler) + +if __name__ == '__main__': + from twisted.internet import reactor + from twisted.web.server import Site + from twisted.web.wsgi import WSGIResource + + reactor_args = {} + resource = WSGIResource(reactor, reactor.getThreadPool(), app) + site = Site(resource) + reactor.listenTCP(5000, site) + reactor.run(**reactor_args) diff --git a/phial.py b/phial.py new file mode 100644 index 0000000..3447057 --- /dev/null +++ b/phial.py @@ -0,0 +1,25 @@ +import collections + +Request = collections.namedtuple( + 'Request', + ( + 'environ', + ) +) + +Response = collections.namedtuple( + 'Response', + ( + 'status', + 'headers', + 'content', + ), +) + +def App(handler): + def app(environ, start_fn): + response = handler(Request(environ)) + + start_fn(response.status, response.headers) + return response.content + return app diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8926f62 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +Twisted==19.7.0 -- 2.20.1