flask跨域一键配置:
from flask import Flask, request
from flask_cors import CORS
app = Flask(__name__)
CORS(app, supports_credentials=True)
# supports_credentials=True 表示可以携带cookie,之前没加这个参数一直不生效,气死我
定时任务,并加到子线程里,定时任务的方法和多线程里一样,不能带(),不然会直接执行并报错
from apscheduler.schedulers.blocking import BlockingScheduler
def thread_get_jira():
# 创建调度器
scheduler = BlockingScheduler()
# 添加定时任务,每天下午6点触发一次
scheduler.add_job(CreateExcel.create, 'cron', hour=12, minute=0, second=0)
scheduler.add_job(CreateExcel.create, 'cron', hour=18, minute=0, second=0)
# 启动调度器
print('程序已启动,等待定时任务触发...')
scheduler.start()
thread = threading.Thread(target=thread_get_jira)
thread.daemon = True
thread.start()
一个把本地文件发出去的接口,前端直接 window.open(url, '_blank'); 就能下载了,方便快捷
@app.route('/download/bug_file', methods=['GET'])
def get_days_bug_file():
filename = request.args.get('filename')
print(filename)
# 定义ports文件夹路径
# 获取当前目录
current_dir = os.getcwd()
ports_path = Path(current_dir) / 'ports'
if (ports_path / '{}.xlsx'.format(filename)).is_file():
print('文件已生成,发送文件{}'.format(filename))
file_path = str(ports_path / '{}.xlsx'.format(filename))
return send_file(file_path, as_attachment=True, download_name='{}.xlsx'.format(filename))
else:
return '未找到该文件数据,日报最早生成于2023-06-10,其它情况联系'
mysql数据库操作类
class SqlHandle:
def __init__(self):
print('建立数据库连接')
self.today = date.today()
# 连接MySQL数据库
self.mydb = mysql.connector.connect(
host="10.228.161.54",
port="3406",
user="root",
password="xxx",
database="jira"
)
# 创建游标对象
self.mycursor_jira = self.mydb.cursor()
def __del__(self):
# 关闭游标和数据库连接
print('关闭数据库连接')
# self.mycursor_jira.close()
self.mydb.close()