如何用烧瓶扭曲运行? [英] How to run twisted with flask?

查看:70
本文介绍了如何用烧瓶扭曲运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想能够同时在同一端口的不同目录上运行多个扭曲的代理服务器,所以我想我可以使用flask. 所以这是我的代码:

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:

from flask import Flask
from twisted.internet import reactor
from twisted.web import proxy, server

app = Flask(__name__)
@app.route('/example')
def index():
    site = server.Site(proxy.ReverseProxyResource('www.example.com', 80, ''.encode("utf-8")))
    reactor.listenTCP(80, site)
    reactor.run()

app.run(port=80, host='My_IP')

但是,每当我运行此脚本时,我都会收到内部服务器错误,这是因为在端口80上调用app.run时,reactor.run也无法在端口80上侦听.我想知道是否有某种工作可以解决这个问题,或者我做错了什么.非常感谢您的任何帮助,谢谢!

But whenever I run this script, I get an Internal Server Error, I'm assuming because when app.run is called on port 80, the reactor.run can't be listening on port 80 as well. I wondering if there is some kind of work around to this, or what it is I'm doing wrong. Any help is greatly appreciated, Thanks!!

推荐答案

您应该尝试klein.它是由大多数twisted核心开发人员制作和使用的.语法非常类似于flask,因此,如果您已经有一个可用的flask应用程序,则无需重写太多内容.因此,以下应该应该可以正常工作:

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

链接

  • 克莱因文档
  • Klein Github
  • Links

    • Klein Docs
    • Klein Github
    • 这篇关于如何用烧瓶扭曲运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆