import time import json import requests import os from tencentcloud.common import credential from tencentcloud.common.profile.client_profile import ClientProfile from tencentcloud.common.profile.http_profile import HttpProfile from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException from tencentcloud.cdn.v20180606 import cdn_client, models try:
#####通过访问指定url获取当前IP,如果不匹配直接退出,注意生成的腾讯云的key,访问权限除了增加cdn权限,还要增加允许该IP访问的权限。其他IP无法访问############
r = requests.get('http://www.ip138.com') if r.text != '1.1.1.1': print('您使用的是非指定网络,程序即将退出!') time.sleep(2) os._exit(0) current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) print('当前时间:' + current_time) print() ####################### #####刷新URL############ ####################### def flush_url(): # 实例化一个请求对象,每个接口都会对应一个request对象 req = models.PurgeUrlsCacheRequest() params = { "Urls": [ url] } req.from_json_string(json.dumps(params)) # 返回的resp是一个PurgeUrlsCacheResponse的实例,与请求对象对应 resp = client.PurgeUrlsCache(req) # 输出json格式的字符串回包 print('刷新链接:' + url) print() ####################### #####查询剩余刷新次数##### ####################### def flush_path(): # 实例化一个请求对象,每个接口都会对应一个request对象 req = models.PurgePathCacheRequest() params = { "Paths": [ url], "FlushType": "delete" } req.from_json_string(json.dumps(params)) # 返回的resp是一个PurgePathCacheResponse的实例,与请求对象对应 resp = client.PurgePathCache(req) # 输出json格式的字符串回包 print('刷新目录:' + url) print() ####################### #####查询剩余刷新次数##### ####################### def query_quota(): # 实例化一个请求对象,每个接口都会对应一个request对象 req = models.DescribePurgeQuotaRequest() params = { } req.from_json_string(json.dumps(params)) # 返回的resp是一个DescribePurgeQuotaResponse的实例,与请求对象对应 resp = client.DescribePurgeQuota(req) # 输出json格式的字符串回包 result = json.loads(resp.to_json_string()) [print('URL刷新今日可用次数:' + str(i['Available']), ' URL刷新今日总数:' + str(i['Total'])) for i in result['UrlPurge'] if i['Area'] == 'mainland'] [print('目录刷新今日可用次数:' + str(i['Available']), ' 目录刷新今日总数:' + str(i['Total'])) for i in result['PathPurge'] if i['Area'] == 'mainland'] print() # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密,这个key只能上述的IP下访问cdn资源 # 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取 cred = credential.Credential("secretId", "secretKey") # 实例化一个http选项,可选的,没有特殊需求可以跳过 httpProfile = HttpProfile() httpProfile.endpoint = "cdn.tencentcloudapi.com" # 实例化一个client选项,可选的,没有特殊需求可以跳过 clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile # 实例化要请求产品的client对象,clientProfile是可选的 client = cdn_client.CdnClient(cred, "", clientProfile) url = '' PurgeType = '' while True: input1 = input('选择刷新URL或者刷新目录(输入数字1刷新URL或者数字2刷新目录):', ) if input1 == '1': url = input('URL 必须包含 http:// 或 https://且至少含有1个路径(域名后至少含有1个"/"),例如 http://www.test.com/test.html\n输入URL链接:') print() PurgeType = 'url' query_quota() flush_url() break elif input1 == '2': url = input('目录 URL 必须包含 http:// 或 https://,例如 http://www.test.com/test/\n输入目录链接:') print() PurgeType = 'path' query_quota() flush_path() break else: print('输入错误') ####################### #####查询刷新历史######## ####################### def query_history(PurgeType): # 实例化一个请求对象,每个接口都会对应一个request对象 req = models.DescribePurgeTasksRequest() params = { "StartTime": time.strftime("%Y-%m-%d", time.localtime()) + ' 00:00:00', "EndTime": current_time, "PurgeType": PurgeType } req.from_json_string(json.dumps(params)) # 返回的resp是一个DescribePurgeTasksResponse的实例,与请求对象对应 resp = client.DescribePurgeTasks(req) # 输出json格式的字符串回包 # print(resp.to_json_string()) result = json.loads(resp.to_json_string())['PurgeLogs'] print('刷新历史:') [print(i) for i in [('URL:' + i['Url'], '刷新时间:' + i['CreateTime'], '状态:' + i['Status']) for i in result if i['PurgeType'] == 'url'][0:3]] [print(i) for i in [('目录:' + i['Url'], '刷新时间:' + i['CreateTime'], '状态:' + i['Status']) for i in result if i['PurgeType'] == 'path'][0:3]] print() time.sleep(3) query_quota() while True: current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) print('当前时间:' + current_time) query_history(PurgeType) input('按“回车键”继续刷新历史记录查询,状态process表示刷新中,状态done表示成功') except TencentCloudSDKException as err: print(err) print('10秒后退出程序... ...') time.sleep(10)