要做什么
1,向指定URL发送请求: requests
2,将状态码保存在redis数据库中 : redis
3,定时启动 : celerybeat
4,返回给前端,以便生成图表: flask
5,注意有时需要附加头部信息: HTTPAuthBasic
6,一次性返回最近24小时数据
程序结构:
demo
├── celerybeat.pid
├── celerybeat-schedule
├── demo2.py
├── make_celery.py
└── monitor.py
make_celery.py #用于帮助配置 monitor 中的 celery
|
|
monitor.py #监控程序
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 #coding:utf-8from __future__ import absolute_importimport requestsimport base64import redisfrom requests.auth import HTTPBasicAuthfrom celery import Celeryfrom flask import Flask,jsonifyfrom celery.schedules import crontabfrom datetime import timedeltafrom os import sys,pathfrom make_celery import make_celeryfrom flask_script import Manager#每次检查间隔时间TIME_EVERY_CHECK=10#连接redispool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=1)r = redis.StrictRedis(connection_pool=pool)#图书馆头部信息Passlib ="2016000000:123456"b64Vallib = base64.b64encode(Passlib)#初始化APPapp = Flask(__name__)url01="https://taobao.com"url02="https://ccnubox.muxixyz.com/api/lib/login/"#配置app.config.update(CELERY_BROKER_URL='redis://127.0.0.1:6379',CELERY_RESULT_BACKEND='redis://127.0.0.1:6379/0',#TimezoneCELERY_TIMEZONE = 'Asia/Shanghai',#schedulesCELERYBEAT_SCHEDULE = {'request_taobao':{'task': 'login_xinximenhu','schedule': timedelta(seconds = TIME_EVERY_CHECK),},'login_library':{'task':'login_lib','schedule':timedelta(seconds = TIME_EVERY_CHECK),}})celery = make_celery(app)#访问淘宝def request_taobao():resp01 = requests.get(url01)statu01 = resp01.status_coder.set(url01,statu01)#登录CCNU图书馆def login_lib():resp02= requests.get(url02,headers = {"Authorization": "Basic %s" %b64Vallib})statu02 = resp02.status_coder.set(url02,statu02)def index():return jsonify({url01:r.get(url01),url02:r.get(url02)})if __name__ =='__main__':app.run(debug=True)demo2.py #一次返回最近XXX时间内的数据
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 #coding:utf-8from __future__ import absolute_importimport requestsimport base64import redisfrom requests.auth import HTTPBasicAuthfrom celery import Celeryfrom flask import Flask,jsonifyfrom celery.schedules import crontabfrom datetime import timedeltafrom os import sys,pathfrom make_celery import make_celeryfrom flask_script import Manager#每次检查间隔时间TIME_EVERY_CHECK=3#Control loop argi = 0#返回数据的总数TOTAL = 20#redis链接池pool01 = redis.ConnectionPool(host='127.0.0.1', port=6379, db=1)pool02 = redis.ConnectionPool(host='127.0.0.1', port=6379, db=2)r01 = redis.StrictRedis(connection_pool=pool01)r02 = redis.StrictRedis(connection_pool=pool02)#图书馆头部信息Passlib ="2016210942:123456"b64Vallib = base64.b64encode(Passlib)#初始化APPapp = Flask(__name__)url01="https://taobao.com"url02="https://ccnubox.muxixyz.com/api/lib/login/"#配置app.config.update(CELERY_BROKER_URL='redis://127.0.0.1:6379',CELERY_RESULT_BACKEND='redis://127.0.0.1:6379/0',#TimezoneCELERY_TIMEZONE = 'Asia/Shanghai',#schedulesCELERYBEAT_SCHEDULE = {'product':{'task': 'product','schedule': timedelta(seconds = TIME_EVERY_CHECK),},'login_library':{'task':'login_lib','schedule':timedelta(seconds = TIME_EVERY_CHECK),},'controli':{'task':'controli','schedule':timedelta(seconds = TIME_EVERY_CHECK),}})app.config['JSON_AS_ASCII'] = Falsecelery = make_celery(app)#木犀产品展示@celery.task(name='product')def product():resp01 = requests.get(url01)statu01 = resp01.status_coder01.set(i,statu01)#登录CCNU图书馆@celery.task(name='login_lib')def login_lib():resp02= requests.get(url02,headers = {"Authorization": "Basic %s" %b64Vallib})statu02 = resp02.status_coder02.set(i,statu02)@celery.task(name='controli')def controli():global iif i < TOTAL-1:i = i+1elif i == TOTAL-1:i = 0@app.route("/")def index():return jsonify({"木犀产品展示":[r01.get(k) for k in range(TOTAL)],"登录图书馆":[r02.get(k) for k in range(TOTAL)]})if __name__ =='__main__':app.run(debug=True)
程序运行步骤
1,flask project: python monitor.py runserver
2,redis: redis-server
3,celery(main process): celery worker - -app monitor.celery - -loglevel=info
4,celery(beat): celery beat - -app monitor.celery - -loglevel=info