How to run twisted with flask? How to run twisted with flask? flask flask

How to run twisted with flask?


You can use the WSGIResource from Twisted istead of a ReverseProxy.

UPDATE: Added a more complex example that sets up a WSGIResource at /my_flask and a ReverseProxy at /example

from flask import Flaskfrom twisted.internet import reactorfrom twisted.web.proxy import ReverseProxyResourcefrom twisted.web.resource import Resourcefrom twisted.web.server import Sitefrom twisted.web.wsgi import WSGIResourceapp = Flask(__name__)@app.route('/example')def index():    return 'My Twisted Flask'flask_site = WSGIResource(reactor, reactor.getThreadPool(), app)root = Resource()root.putChild('my_flask', flask_site)site_example = ReverseProxyResource('www.example.com', 80, '/')root.putChild('example', site_example)reactor.listenTCP(8081, Site(root))reactor.run()

Try running the above in your localhost and then visiting localhost:8081/my_flask/example or localhost:8081/example


You should give klein a try. It's made and used by most of the twisted core devs. The syntax is very much like flask so you won't have to rewrite much if you already have a working flask app. So something like the following should work:

from twisted.internet import reactorfrom twisted.web import proxy, serverfrom klein import Kleinapp = Klein()@app.route('/example')def home(request):    site = server.Site(proxy.ReverseProxyResource('www.example.com', 80, ''.encode("utf-8")))    reactor.listenTCP(80, site)app.run('localhost', 8000)        # start the klein app on port 8000 and reactor event loop

Links


The accepted answer does not cover how to run twisted with Flask, and points to a different framework. The answer with an example no longer works either.

Here are two different examples. The first one is the same as the first answer, but fixed to work on Python 3

from flask import Flaskfrom twisted.internet import reactorfrom twisted.web.proxy import ReverseProxyResourcefrom twisted.web.resource import Resourcefrom twisted.web.server import Sitefrom twisted.web.wsgi import WSGIResourceapp = Flask(__name__)@app.route('/example')def index():    return 'My Twisted Flask'flask_site = WSGIResource(reactor, reactor.getThreadPool(), app)root = Resource()root.putChild(b'my_flask', flask_site)site_example = ReverseProxyResource('www.example.com', 80, b'/')root.putChild(b'example', site_example)reactor.listenTCP(8081, Site(root))reactor.run()

For this example, run it and open any of these:

localhost:8081/my_flask/example

localhost:8081/example

This other example is recommended, since it sets up two services and provides them through a .tac file to twistd.Take the base code from here: https://github.com/pika/pika/blob/master/examples/twisted_service.py

"""Modify the bottom of the file to pick the new MultiService"""# ... all the code from twisted_service.py goes here.# Scroll to the bottom of the file and# remove everything below application = service.Application("pikaapplication")# You should keep the PikaService, PikaProtocol and PikaFactory# classes, since we need them for this code:from pika.connection import ConnectionParametersfrom pika.credentials import PlainCredentialsfrom twisted.application import service, strportsfrom twisted.internet import reactorfrom twisted.web.server import Sitefrom twisted.web.wsgi import WSGIResourcefrom flask import Flask# This IServiceCollection will hold Pika and Flaskflask_pika_multiservice = service.MultiService()# FLASK SERVICE SETUPapp = Flask("demoapp")@app.route('/')def hello_world():    return 'Hello, World!'flask_site = Site(WSGIResource(reactor, reactor.getThreadPool(), app))# New resources can be added, such as WSGI, or proxies by creating# a root resource in the place of the flask_site, and adding the# new resources to the root.# root = Resource()# root.putChild(b'my_flask_site', flask_site)# from twisted.web.proxy import ReverseProxyResource# site_example = ReverseProxyResource('www.example.com', 80, b'/')# root.putChild(b'example', site_example)i = strports.service(f"tcp:8088", flask_site)i.setServiceParent(flask_pika_multiservice)# PIKA SERVICE SETUPps = PikaService(    ConnectionParameters(        host="localhost",        virtual_host="/",        credentials=PlainCredentials("guest", "guest")))ps.setServiceParent(flask_pika_multiservice)# Application setupapplication = service.Application('flask_pika')flask_pika_multiservice.setServiceParent(application)

Now you can run it with:

PYTHONPATH=. twistd -ny twisted_service.py

you can skip the python path if you don't want to import anything from the same path. twisted expects projects to actually be installed, and does not support running them directly from the source folder unless you use that workaround.

This second example establishes two services, on different ports. It's for pika and flask running simultaneously on the same twisted server. The best part is that it shows how to set up flask as a service, that can be part of an IServiceCollection