flask 自定义配置文件
- 在同一个目录路径:
全局都可以读取到配置
![]()
from flask import Flask
app = Flask(__name__)
#加载配置文件
app.config.from_object('settings')
print(app.config['XX'])
print(app.config['ABC'])
@app.route('/hello')
def hello():
return 'Hello World!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
执行结果:

类配置文件读取:
from flask import Config
#公共配置
class BaseConfig(object):
YY=123
#继承公共配置,避免重复写配置
class DevelopmentConfig(BaseConfig):
HOST = '1.1.1.1'
class ProductionConfig(object):
HOST = '2.2.2.2'
主程序:
from flask import Flask
app = Flask(__name__)
'''
#加载配置文件
app.config.from_object('settings')
print(app.config['XX'])
print(app.config['ABC'])
'''
#加载class配置信息
app.config.from_object('cls-settings.DevelopmentConfig')
#继承类配置文件读取
print(app.config['YY'])
print(app.config['HOST'])
#类配置文件读取
app.config.from_object('cls-settings.ProductionConfig')
print(app.config['HOST'])
@app.route('/hello')
def hello():
print(app.config['XX'])
print(app.config['ABC'])
return 'Hello World!'
''' '''
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
读取结果:



浙公网安备 33010602011771号