繁体   English   中英

如何用烧瓶扭曲运行?

[英]How to run twisted with flask?

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

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

但每当我运行该脚本,我得到一个内部服务器错误,我假设,因为当app.run被称为端口80, reactor.run不能在80端口,以及听。 我想知道是否有某种解决方法,或者我做错了什么。 非常感谢任何帮助,谢谢!!

您可以使用来自 Twisted 的 WSGIResource 而不是 ReverseProxy。

更新:添加了一个更复杂的示例,该示例在 /my_flask 设置 WSGIResource 并在 /example 设置 ReverseProxy

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

尝试在您的本地主机中运行上述内容,然后访问 localhost:8081/my_flask/example 或 localhost:8081/example

你应该试试klein 它是由大多数twisted核心开发人员制作和使用的。 语法与flask非常相似,因此如果您已经有一个可用的flask应用程序,则不必重写太多。 所以像下面这样的东西应该可以工作:

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

链接

接受的答案不包括如何使用 Flask 运行扭曲,而是指向不同的框架。 带有示例的答案也不再有效。

这里有两个不同的例子。 第一个与第一个答案相同,但适用于 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()

对于此示例,运行它并打开其中任何一个:

本地主机:8081/my_flask/示例

本地主机:8081/示例

推荐使用另一个示例,因为它设置了两个服务并通过 .tac 文件将它们提供给 twind。 从这里获取基本代码: 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)

现在你可以运行它:

PYTHONPATH=. twistd -ny twisted_service.py

如果您不想从同一路径导入任何内容,则可以跳过 python 路径。 扭曲期望实际安装项目,并且不支持直接从源文件夹运行它们,除非您使用该解决方法。

第二个示例在不同的端口上建立两个服务。 它适用于在同一台扭曲服务器上同时运行的鼠兔和烧瓶。 最好的部分是它展示了如何将烧瓶设置为服务,它可以是 IServiceCollection 的一部分

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM