我正在尝试在类中使用烧瓶,但我想使用装饰器
我在课堂上看到过这个主题 使用烧瓶
但到目前为止,内部还没有使用装饰器,如果别无选择,我将使用这个解决方案。
目前我的代码如下所示:
class DM():
def __init__(self, path=""):
self.cors = CORS(self.app, resources={r"/*": {"origins": "*"}})
self.host = "0.0.0.0"
self.port = 9001
class app(Flask):
pass
def PrintException(self):
exc_type, exc_obj, tb = sys.exc_info()
f = tb.tb_frame
lineno = tb.tb_lineno
filename = f.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
return 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(
filename, lineno, line.strip(), exc_obj
)
@app.route("/product", methods=["POST", "GET", "OPTION"])
def addProduct(self):
try:
data = request.data
document = json.loads(data.decode("utf-8"))
return jsonify({"status":"ok","_id":str(document)})
except Exception as e:
return jsonify({"status": "ko", "exception": self.PrintException() + " " + str(e),"document":document})
@app.after_request
def after_request(self,response):
response.headers.add("Access-Control-Allow-Origin", "*")
response.headers.add("Access-Control-Allow-Headers", "Content-Type,Authorization")
response.headers.add("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS")
return response
def run(self):
self.app.run(self.host, self.port)
Run Code Online (Sandbox Code Playgroud)
但我有这个错误:
@app.route("/product", methods=["POST", "GET", "OPTION"])
TypeError: route() missing 1 required positional argument: 'rule'
Run Code Online (Sandbox Code Playgroud)
编辑 :
我不能在装饰器中使用 self :
class DM():
def __init__(self, path=""):
self.app = Flask(__name__)
self.cors = CORS(self, resources={r"/*": {"origins": "*"}})
self.host = "0.0.0.0"
self.port = 9001
def PrintException(self):
exc_type, exc_obj, tb = sys.exc_info()
f = tb.tb_frame
lineno = tb.tb_lineno
filename = f.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
return 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(
filename, lineno, line.strip(), exc_obj
)
@app.route("/product", methods=["POST", "GET", "OPTION"])
def addProduct(self):
try:
data = request.data
document = json.loads(data.decode("utf-8"))
return jsonify({"status":"ok","_id":str(document)})
except Exception as e:
return jsonify({"status": "ko", "exception": self.PrintException() + " " + str(e),"document":document})
@app.after_request
def after_request(self,response):
response.headers.add("Access-Control-Allow-Origin", "*")
response.headers.add("Access-Control-Allow-Headers", "Content-Type,Authorization")
response.headers.add("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS")
return response
def run(self):
self.app.run(self.host, self.port)
Run Code Online (Sandbox Code Playgroud)
问候
小智 3
我不确定您到底想通过继承方法实现什么目标,但无论如何,这里有一个可能有所帮助的想法。您可能需要考虑工厂方法,而不是在类中使用 Flask 应用程序。您的示例中的代码可能如下所示:
def create_application(app_name, config_filename, host, port):
app = Flask(app_name)
app.config_from_pyfile(config_filename)
cors = CORS(app, resources={r"/*": {"origins": "*"}})
@app.route("/product", methods=["POST", "GET", "OPTION"])
def addProduct():
try:
data = request.data
document = json.loads(data.decode("utf-8"))
return jsonify({"status":"ok","_id":str(document)})
except Exception as e:
return jsonify({"status": "ko", "exception": self.PrintException() + " " + str(e),"document":document})
@app.after_request
def after_request(response):
response.headers.add("Access-Control-Allow-Origin", "*")
response.headers.add("Access-Control-Allow-Headers",
"Content-Type,Authorization")
response.headers.add("Access-Control-Allow-Methods",
"GET,PUT,POST,DELETE,OPTIONS")
return response
Run Code Online (Sandbox Code Playgroud)
此方法将允许您进行必要的自定义,将不同的参数传递给工厂函数或不同的配置文件。你仍然可以在这里使用装饰器
| 归档时间: |
|
| 查看次数: |
6051 次 |
| 最近记录: |