基于flask的清除nginx缓存接口

#-*- coding:utf-8 _8_
import os
import shutil
from flask import Flask

app = Flask(__name__)

@app.route('/apiv1/clear/cache')
def clearcache():
    os.chdir("/tmp/cache")
    mypath = os.getcwd()
    filelist = os.listdir(mypath)   #清缓存前的缓存目录文件列表

    for dir in filelist:
        path = "/tmp/cache/%s" % dir
        if os.path.isdir(dir):
            shutil.rmtree(path,True)
        else:
            os.remove(path)

    nowlist = os.listdir(mypath)    #清缓存后的缓存目录文件列表
    if len(nowlist) == 0:
        return "已经成功清楚nginx缓存"
    else:
        return "清除缓存失败了,再给个机会嘛!"

if __name__ == '__main__':
    app.run('0.0.0.0', 8000, debug=True)

你可能感兴趣的:(基于flask的清除nginx缓存接口)