From: David Kerkeslager Date: Tue, 22 Oct 2019 17:27:21 +0000 (-0400) Subject: Basic wsgi app working inside twisted X-Git-Url: https://code.kerkeslager.com/?p=fwx;a=commitdiff_plain;h=8b491e50b5a554643a068f66f7257d38c6ac7e7c Basic wsgi app working inside twisted --- 8b491e50b5a554643a068f66f7257d38c6ac7e7c 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