How to run twisted with flask?

后端 未结 3 1717
温柔的废话
温柔的废话 2021-02-06 08:31

I wanna be able to run multiple twisted proxy servers on different directories on the same port simultaneously, and I figured I might use flask. so here\'s my code:



        
3条回答
  •  北恋
    北恋 (楼主)
    2021-02-06 09:27

    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 Flask
    from twisted.internet import reactor
    from twisted.web.proxy import ReverseProxyResource
    from twisted.web.resource import Resource
    from twisted.web.server import Site
    from twisted.web.wsgi import WSGIResource
    
    app = 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 ConnectionParameters
    from pika.credentials import PlainCredentials
    from twisted.application import service, strports
    from twisted.internet import reactor
    from twisted.web.server import Site
    from twisted.web.wsgi import WSGIResource
    from flask import Flask
    
    # This IServiceCollection will hold Pika and Flask
    flask_pika_multiservice = service.MultiService()
    
    # FLASK SERVICE SETUP
    app = 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 SETUP
    ps = PikaService(
        ConnectionParameters(
            host="localhost",
            virtual_host="/",
            credentials=PlainCredentials("guest", "guest")))
    ps.setServiceParent(flask_pika_multiservice)
    
    # Application setup
    application = 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

提交回复
热议问题