Cri*_*an 7 python reverse-proxy twisted 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')
Run Code Online (Sandbox Code Playgroud)
但每当我运行此脚本时,我都会收到内部服务器错误,我假设因为app.run在端口80上调用时,reactor.run也无法在端口80上进行侦听.我想知道是否有某种工作要做,或者我做错了什么.非常感谢任何帮助,谢谢!
Edu*_*rdo 10
您可以使用来自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()
Run Code Online (Sandbox Code Playgroud)
尝试在localhost中运行上面的内容,然后访问localhost:8081/my_flask/example或localhost:8081/example
not*_*.no 10
你应该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
Run Code Online (Sandbox Code Playgroud)
接受的答案不包括如何使用 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()
Run Code Online (Sandbox Code Playgroud)
对于此示例,运行它并打开其中任何一个:
本地主机: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)
Run Code Online (Sandbox Code Playgroud)
现在你可以运行它:
PYTHONPATH=. twistd -ny twisted_service.py
Run Code Online (Sandbox Code Playgroud)
如果您不想从同一路径导入任何内容,则可以跳过 python 路径。扭曲期望实际安装项目,并且不支持直接从源文件夹运行它们,除非您使用该解决方法。
第二个示例在不同的端口上建立两个服务。它适用于在同一台扭曲服务器上同时运行的鼠兔和烧瓶。最好的部分是它展示了如何将烧瓶设置为服务,它可以是 IServiceCollection 的一部分
| 归档时间: |
|
| 查看次数: |
7257 次 |
| 最近记录: |