【问题标题】:How to run twisted with flask?如何用烧瓶扭曲运行?
【发布时间】:2016-04-27 04:14:14
【问题描述】:

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

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 上监听。我想知道是否有某种解决方法,或者我做错了什么。非常感谢任何帮助,谢谢!

【问题讨论】:

  • 您是否尝试使用其他端口?
  • 是的,我尝试使用不同的端口。它导致网站根本不出现

标签: python flask twisted reverse-proxy


【解决方案1】:

您可以使用来自 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 中运行上述内容,然后访问 localhost:8081/my_flask/example 或 localhost:8081/example

【讨论】:

  • 谢谢,这实际上效果很好,唯一的问题是,我不确定我将在哪里或如何实现代理功能以呈现来自其他服务器的资源?
  • 我正在尝试那些路径和端口,使用那个确切的代码,但我每次都收到No Such Resourceno such child resource
  • 尝试在 putChild 参数上使用字节:root.putChild(b'my_flask', flask_site)
  • 我可以确认它在 putChild 中发送字节时可以正常工作。但它无法代理该站点。
【解决方案2】:

你应该试试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

链接

【讨论】:

  • 我实际上已经尝试过使用Klein,但是我尝试时总是出错,请转到stackoverflow.com/questions/37013869/…
  • 这个问题的答案是准确的。我忽略了这样一个事实,即您试图在同一个接口上监听同一个端口,这会引发错误。理论上,您可以对端口 80 上的流量进行负载平衡,但这可能有点过头了。
【解决方案3】:

接受的答案不包括如何使用 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()

对于本例,运行它并打开以下任何一个:

localhost:8081/my_flask/example

localhost:8081/示例

推荐使用另一个示例,因为它设置了两个服务并通过 .tac 文件将它们提供给 twistd。 从这里获取基本代码: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 路径。 twisted 期望实际安装项目,并且不支持直接从源文件夹运行它们,除非您使用该解决方法。

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

【讨论】:

    猜你喜欢
    • 2016-08-26
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 2020-05-22
    • 2022-01-19
    相关资源
    最近更新 更多