使用烧瓶在我的 Python 应用程序中启用 CORS 时缺少'logger_name'



我正在努力让CORS正常工作。一种变通方法是有效的,但并非适用于所有api。所以我希望有规律的工作方式。这让我已经在这里呆了一段时间了。我试着这样初始化CORS:

flask_app = Flask(__name__)
CORS(flask_app)

这给了我以下错误:

return logging.getLogger("%s.cors" % app.logger_name) AttributeError: 'Flask' object has no attribute 'logger_name'

完整的错误是:

2021-12-18 16:30:57 default[20211218t172924]  Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker      worker.init_process()    
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/workers/base.py", line 134, in init_process      self.load_wsgi()    
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi      self.wsgi = self.app.wsgi()    
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi      self.callable = self.load()    
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 58, in load      return self.load_wsgiapp()    
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp      return util.import_app(self.app_uri)    
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/util.py", line 412, in import_app      app = app(*args, **kwargs)    
File "/workspace/app.py", line 38, in get_flask_app      CORS(flask_app)    
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask_cors/extension.py", line 59, in __init__      self.init_app(app, **kwargs)    
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask_cors/extension.py", line 81, in init_app      getLogger(app).info("Configuring CORS with resources: %s", resources_human)    
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask_cors/core.py", line 356, in getLogger      
return logging.getLogger("%s.cors" % app.logger_name)  AttributeError: 'Flask' object has no attribute 'logger_name'

当我移除CORS(flask_app(时,一切都很好。除了我想解决的CORS问题。

这是我的完整应用程序.py:

# flask packages
from flask import Flask, request, app
from flask_cors import CORS
from flask_restful import Api
from flask_mongoengine import MongoEngine
from flask_jwt_extended import JWTManager
import logging

# local packages
from api.routes import create_routes
# external packages
import os
# default mongodb configuration
default_config = {'MONGODB_SETTINGS': {
'db': '(....)',
'host': '(....)',
'port': 27017,
'username': 'admin',
'password': 'password',
'authentication_source': 'admin'},
'JWT_SECRET_KEY': '(...)'}

def get_flask_app(config: dict = None) -> app.Flask:
"""
Initializes Flask app with given configuration.
Main entry point for wsgi (gunicorn) server.
:param config: Configuration dictionary
:return: app
"""
# init flask
logging.getLogger('flask_cors').level = logging.DEBUG
flask_app = Flask(__name__)
CORS(flask_app)
# configure app
config = default_config if config is None else config
flask_app.config.update(config)
# load config variables
if 'MONGODB_URI' in os.environ:
flask_app.config['MONGODB_SETTINGS'] = {'host': os.environ['MONGODB_URI'],
'retryWrites': False}
if 'JWT_SECRET_KEY' in os.environ:
flask_app.config['JWT_SECRET_KEY'] = os.environ['JWT_SECRET_KEY']
# init api and routes
api = Api(app=flask_app)
create_routes(api=api)
# init mongoengine
db = MongoEngine(app=flask_app)
# init jwt manager
jwt = JWTManager(app=flask_app)
return flask_app


if __name__ == '__main__':
# Main entry point when run in stand-alone mode.
app = get_flask_app()
app.run(host='0.0.0.0', port=8080)

使用谷歌云平台。这会是我问题的关键吗?我的应用程序是:

runtime: python38
entrypoint: gunicorn -b :$PORT 'app:get_flask_app()'

我会试着看看当地会发生什么,也许在这里更容易解决。在这里重新输入问题时要考虑。

已经更新了我的烧瓶胸衣(烧瓶胸衣(。尝试了很多东西,但都没有成功,因此产生了这个问题。

非常感谢您的帮助!!

添加此部分:

Flask.logger_name = "listlogger"

最终为我解决了这个问题。现在它能够找到logger_name。那个列表记录器只是一个随机的名字。

我在打开应用程序+CORS(应用程序(之前添加了它:

Flask.logger_name = "listlogger"
flask_app = Flask(__name__)
CORS(flask_app)

相关内容

  • 没有找到相关文章

最新更新