How can my Twisted Klein app listen on multiple ports?

619 views Asked by At

I've got a Twisted Klein app which needs to listen on more than one port in our deployment environment. With Flask I'd deploy with gunicorn and it seems like you could run Klein in a wsgi container but it'd defeat the async nature of it.

1

There are 1 answers

2
patricksurry On

Inspired by this question about Twisted listening on multiple ports I dug into the Klein app.run() method and it seems to work if you just set up another Twisted endpoint before you call app run. Any insight into whether this is the right approach would be appreciated.

from twisted.web.server import Site
from twisted.internet import reactor, endpoints

...

admin_endpoint = "tcp:port={0}:interface={1}".format(8888, '0.0.0.0')
endpoints.serverFromString(reactor, admin_endpoint).listen(Site(app.resource()))

...

app.run('0.0.0.0', port=9999)