flask的脚本控制flask_script
flask_script用法:
#_*_ encoding: utf-8 _*_ @author: ty hery 2019/1/12
from flask_script import Manager
from flask import Flask
import sqlite3
app = Flask(__name__)
manager = Manager(app)
@manager.command
def hello():
print('hello world')
@manager.option('-m','--msg',dest='msg_val',default='world') # -m :参数 default:默认参数
def hello_world(msg_val):
print('hello ' + msg_val)
@manager.command
def init_db():
sql = 'create table user (id INT,name TEXT)'
conn = sqlite3.connect("flask_test.db")
cursor = conn.cursor()
cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()
if __name__ == "__main__":
manager.run()
在该文件同等级目录下: python manager.py hello 打印出 hello world
python manager.py hello 打印出 hello world
(py2_for_websocket) X:\python3.64\Flask>python manager.py hello_world 运行 hello world函数不带参数,使用默认参数,
hello world
(py2_for_websocket) X:\python3.64\Flask>python manager.py hello_world -m jikexueyuan 运行 hello world函数带参数jikexueyuan
hello jikexueyuan
(py2_for_websocket) X:\python3.64\Flask>python manager.py init_db # 在当前目录下新建了flask_test.db文件
#_*_ encoding: utf-8 _*_ @author: ty hery 2019/12/20
from flask import Flask, session, current_app, g
from flask_script import Manager # 启动命令的管理器类
app = Flask(__name__)
# 创建Manager管理器类的对象
manager = Manager(app)
@app.route('/index')
def index():
print('index 被执行')
return 'index page'
if __name__ == '__main__':
# print('--哈哈01--',app.url_map,'--哈哈01--')
# app.run(debug=True)
# app.run(debug=False)
# 通过管理器对象来启动flask
manager.run()
可以在这个文件所在目录下 运行: python 10_flask_script.py runserver -h 127.0.0.1 -p 5000
启动项目文件方式: -d指的是开启debug模式 在 X:\python3.64\anzhuangweizhi目录(pyhton3.6的默认环境下启动python
python X:/python3.64/Flask/jikexueyuan_flask/10_flask_script.py runserver - h 0.0.0.0 - p 8000 - d
X:\python3.64\Flask\flask类视图和restful>python 10_flask_script.py runserver -h 127.0.0.1 -p 5000 启动命令
- Serving Flask app "10_flask_script" (lazy loading)
- Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead. - Debug mode: off
- Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [12/Apr/2021 22:25:18] "GET /index/ HTTP/1.1" 404 -
127.0.0.1 - - [12/Apr/2021 22:27:36] "GET /index HTTP/1.1" 200 -
127.0.0.1 - - [12/Apr/2021 22:27:37] "GET /favicon.ico HTTP/1.1" 404 -
写入自己的博客中才能记得长久
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!