首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在类内使用烧瓶

在类内使用烧瓶
EN

Stack Overflow用户
提问于 2016-11-07 08:35:25
回答 5查看 85K关注 0票数 56

我有一个有很多线程的应用程序。其中之一是用于实现(辅助) API的烧瓶。它是用于低负荷和从来没有暴露在互联网,所以内置的瓶web服务器是完全好的。

我的当前代码如下:

代码语言:javascript
运行
复制
class API:
    # ... all other stuff here, skipped
    def run():
        app = flask.Flask('API')

        @app.route('/cmd1')
        def cmd1():
            self.cmd1()

        @app.route('/cmd2')
        def cmd2()
            self.cmd2()

        app.run()

我觉得我做错了,因为所有的文档都说“在模块级创建烧瓶应用程序”。但我不想这样做--它会扰乱我的测试,API是大型应用程序的一小部分,它有自己的结构和约定(每个“应用程序”是一个或多个线程中运行的单独类)。

我怎么才能在课堂上使用酒瓶?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2016-11-07 13:35:38

虽然这是可行的,但它感觉不符合酒瓶风格的指南。如果您需要在项目中包装一个Flask应用程序,请根据您的需要创建一个单独的类,并添加应该执行的函数。

代码语言:javascript
运行
复制
from flask import Flask, Response


class EndpointAction(object):

    def __init__(self, action):
        self.action = action
        self.response = Response(status=200, headers={})

    def __call__(self, *args):
        self.action()
        return self.response


class FlaskAppWrapper(object):
    app = None

    def __init__(self, name):
        self.app = Flask(name)

    def run(self):
        self.app.run()

    def add_endpoint(self, endpoint=None, endpoint_name=None, handler=None):
        self.app.add_url_rule(endpoint, endpoint_name, EndpointAction(handler))


def action():
    # Execute anything

a = FlaskAppWrapper('wrap')
a.add_endpoint(endpoint='/ad', endpoint_name='ad', handler=action)
a.run()

这里要注意的是:

  • EndpointAction应该是一个包装器,它将执行您的函数并生成一个空的200个响应。如果需要,可以编辑该功能。
  • 端点处理程序可以是定义了__call__方法的任何东西。
  • 端点名称应该是唯一的,因为它表示视图名。
  • 在应用程序之后添加端点是不可能的,因为一旦应用程序启动,线程将阻塞。您可以通过在单独的线程上运行应用程序来启用它,但是不建议动态更改URL映射,这两个线程都不安全
票数 51
EN

Stack Overflow用户

发布于 2020-05-15 06:27:15

所以我偶然发现了图书馆酒瓶-一流

相对来说真的很简单

要在类中创建一个简单的web应用程序,如下所示:

代码语言:javascript
运行
复制
from flask import Flask
from flask_classful import FlaskView

app = Flask(__name__)

class TestView(FlaskView):

    def index(self):
    # http://localhost:5000/
        return "<h1>This is my indexpage</h1>"

TestView.register(app,route_base = '/')

if __name__ == '__main__':
    app.run(debug=True) 

处理多个路由和动态路由也是简单的

代码语言:javascript
运行
复制
class TestView(FlaskView):

    def index(self):
    # http://localhost:5000/
        return "<h1>This is my indexpage</h1>"

    def secondpage(self):
    # http://localhost:5000/secondpage
        return "<h1>This is my second</h1>"
    
    def thirdpage(self,name):
    # dynamic route
    # http://localhost:5000/thirdpage/sometext
        return "<h1>This is my third page <br> welcome"+name+"</h1>"

TestView.register(app,route_base = '/')

用另一种方法添加自己的路由名,这也是可能的

代码语言:javascript
运行
复制
from flask_classful import FlaskView,route

class TestView(FlaskView):

    def index(self):
    # http://localhost:5000/
        return "<h1>This is my indexpage</h1>"


    @route('/diffrentname')
    def bsicname(self):
    # customized route
    # http://localhost:5000/diffrentname
        return "<h1>This is my custom route</h1>"
TestView.register(app,route_base = '/')

这使您可以为一个独立的独立进程创建单独的类和处理程序,并将它们作为包导入,以便在主文件或包装文件上运行。

代码语言:javascript
运行
复制
from package import Classname
Classname.register(app,route_base = '/')

它非常简单,并且面向对象。

票数 15
EN

Stack Overflow用户

发布于 2019-05-13 06:30:40

要完成Kostas的回答,因为我很难找到为什么响应没有直接使用Action返回值。

下面是另一种没有装饰器的烧瓶类:

代码语言:javascript
运行
复制
class EndpointAction(object):

    def __init__(self, action):
        self.action = action

    def __call__(self, *args):
        # Perform the action
        answer = self.action()
        # Create the answer (bundle it in a correctly formatted HTTP answer)
        self.response = flask.Response(answer, status=200, headers={})
        # Send it
        return self.response

class FlaskAppWrapper(object):

    def add_all_endpoints(self):
        # Add root endpoint
        self.add_endpoint(endpoint="/", endpoint_name="/", handler=self.action)

        # Add action endpoints
        self.add_endpoint(endpoint="/add_X", endpoint_name="/add_X", handler=self.add_X)
        # you can add more ... 

    def add_endpoint(self, endpoint=None, endpoint_name=None, handler=None):
        self.app.add_url_rule(endpoint, endpoint_name, EndpointAction(handler)) 
        # You can also add options here : "... , methods=['POST'], ... "

    # ==================== ------ API Calls ------- ====================
    def action(self):
        # Dummy action
        return "action" # String that will be returned and display on the webpage
        # Test it with curl 127.0.0.1:5000

    def add_X(self):
        # Dummy action
        return "add_X"
        # Test it with curl 127.0.0.1:5000/add_X
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40460846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档