app.py 1013 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import json
  2. import requests
  3. from flask import Flask,render_template
  4. from flask_apscheduler import APScheduler
  5. app = Flask(__name__, template_folder="templates")
  6. app.jinja_env.variable_start_string = '[['
  7. app.jinja_env.variable_end_string = ']]'
  8. urls = []
  9. with open('data.json',encoding="utf-8") as f:
  10. urls = json.loads(f.read())
  11. # 实例化 APScheduler
  12. scheduler = APScheduler()
  13. @scheduler.task('interval', id='ping', args=(),seconds=10)
  14. def ping(): # 运行的定时任务的函数
  15. result = []
  16. for i in urls:
  17. name = i['name']
  18. url = i['url']
  19. r = requests.get(url,timeout=3)
  20. result.append({'url':url,'status_code':r.status_code,'name':name})
  21. with open("static/data.json",'w+',encoding="utf-8") as f:
  22. f.write(json.dumps(result))
  23. @app.route("/")
  24. def index():
  25. return render_template('index.html')
  26. if __name__ == '__main__':
  27. scheduler.start() # 启动任务列表
  28. app.debug=True
  29. app.run(host='0.0.0.0',port= 8000) # 启动 flask