How to run twisted with flask?

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

    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 reactor
    from twisted.web import proxy, server
    from klein import Klein
    
    app = 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

    • Klein Docs
    • Klein Github

提交回复
热议问题