加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 如何用烧瓶加捻?

发布时间:2020-12-20 11:51:29 所属栏目:Python 来源:网络整理
导读:我希望能够在多个不同的目录上运行扭曲的服务器(exp:/ example1,/ example2..etc),所以我想我会使用flask.这是我到目前为止: from flask import Flaskfrom twisted.web.server import Sitefrom twisted.web.wsgi import WSGIResourcefrom twisted.internet
我希望能够在多个不同的目录上运行扭曲的服务器(exp:/ example1,/ example2..etc),所以我想我会使用flask.这是我到目前为止:

from flask import Flask
from twisted.web.server import Site
from twisted.web.wsgi import WSGIResource
from twisted.internet import reactor
from twisted.web.proxy import ReverseProxyResource
from twisted.web.resource import Resource

app = Flask(__name__)
@app.route('/example1')
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,''.encode('utf-8'))
root.putChild('example1',site_example)

reactor.listenTCP(80,Site(root))
reactor.run()

唯一的问题是它不起作用,我不确定我做错了什么.我感谢任何帮助,谢谢!

解决方法

我的个人意见:在Twisted反应堆中运行Flask并不是一个好主意,因为当Flask处理请求时,Twisted的反应器被阻止.

我想你可能对Klein感兴趣,它提供类似于Flask的API,但是开箱即用的Twisted:http://klein.readthedocs.io/en/latest/

另一种选择:我将把nginx看作Flask应用程序的反向代理而不是Twisted. nginx在一个单独的进程中运行,并且在Flask处理请求时不会被阻止.
https://www.nginx.com/resources/admin-guide/reverse-proxy/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读