123456789101112131415161718192021222324252627282930313233343536373839 |
- import json
- import requests
- from flask import Flask,render_template
- from flask_apscheduler import APScheduler
- app = Flask(__name__, template_folder="templates")
- app.jinja_env.variable_start_string = '[['
- app.jinja_env.variable_end_string = ']]'
- urls = []
- with open('data.json',encoding="utf-8") as f:
- urls = json.loads(f.read())
- # 实例化 APScheduler
- scheduler = APScheduler()
- @scheduler.task('interval', id='ping', args=(),seconds=10)
- def ping(): # 运行的定时任务的函数
- result = []
- for i in urls:
- name = i['name']
- url = i['url']
- r = requests.get(url,timeout=3)
- result.append({'url':url,'status_code':r.status_code,'name':name})
- with open("static/data.json",'w+',encoding="utf-8") as f:
- f.write(json.dumps(result))
- @app.route("/")
- def index():
- return render_template('index.html')
- if __name__ == '__main__':
-
- scheduler.start() # 启动任务列表
- app.debug=True
- app.run(host='0.0.0.0',port= 8000) # 启动 flask
|