How to run twisted with flask?

后端 未结 3 1765
温柔的废话
温柔的废话 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:20

    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 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('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

提交回复
热议问题