我有一个有很多线程的应用程序。其中之一是用于实现(辅助) API的烧瓶。它是用于低负荷和从来没有暴露在互联网,所以内置的瓶web服务器是完全好的。
我的当前代码如下:
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是大型应用程序的一小部分,它有自己的结构和约定(每个“应用程序”是一个或多个线程中运行的单独类)。
我怎么才能在课堂上使用酒瓶?
发布于 2016-11-07 13:35:38
虽然这是可行的,但它感觉不符合酒瓶风格的指南。如果您需要在项目中包装一个Flask应用程序,请根据您的需要创建一个单独的类,并添加应该执行的函数。
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__方法的任何东西。发布于 2020-05-15 06:27:15
所以我偶然发现了图书馆酒瓶-一流
相对来说真的很简单
要在类中创建一个简单的web应用程序,如下所示:
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) 处理多个路由和动态路由也是简单的
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 = '/')用另一种方法添加自己的路由名,这也是可能的
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 = '/')这使您可以为一个独立的独立进程创建单独的类和处理程序,并将它们作为包导入,以便在主文件或包装文件上运行。
from package import Classname
Classname.register(app,route_base = '/')它非常简单,并且面向对象。
发布于 2019-05-13 06:30:40
要完成Kostas的回答,因为我很难找到为什么响应没有直接使用Action返回值。
下面是另一种没有装饰器的烧瓶类:
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_Xhttps://stackoverflow.com/questions/40460846
复制相似问题