python刷新腾讯云cdn

python root 56℃ 0评论

程序需要使用python3,使用前需获取腾讯云SecretID和SecretKey。

# -*- coding:utf-8 -*-

__author__ = ‘wx’

import hashlib

import requests

import hmac

import random

import time

import getopt,sys

import base64

from urllib import parse

from datetime import datetime, timedelta

 

class CdnHelper(object):

SecretId = ‘XXXXX’

SecretKey = ‘XXXXX’

requestHost = ‘cdn.api.qcloud.com’

requestUri = ‘/v2/index.php?’

def __init__(self, **args):

self.type = args[‘type’]

self.url = args[‘url’]

self.startDate = args[‘startDate’]

self.endDate = args[‘endDate’]

 

def flushurl_dict(self):

keydict = {

‘Action’: ‘RefreshCdnUrl’,

‘Timestamp’: str(int(time.time())),

‘Nonce’: str(int(random.random() * 1000)),

‘SecretId’: CdnHelper.SecretId,

‘urls.0’: self.url

}

sortlist = sorted(zip(keydict.keys(), keydict.values()))

return sortlist

def flushdir_dict(self):

keydict = {

‘Action’: ‘RefreshCdnDir’,

‘Timestamp’: str(int(time.time())),

‘Nonce’: str(int(random.random() * 1000)),

‘SecretId’: CdnHelper.SecretId,

‘dirs.0’: self.url

}

sortlist = sorted(zip(keydict.keys(), keydict.values()))

return sortlist

 

def get_str_sign(self):

if self.type == ‘url’:

sortlist = self.flushurl_dict()

if self.type == ‘dir’:

sortlist = self.flushdir_dict()

sign_str_init = ”

for value in sortlist:

sign_str_init += str(value[0]) + ‘=’ + str(value[1]) + ‘&’

sign_str = ‘GET’ + CdnHelper.requestHost + CdnHelper.requestUri + sign_str_init[:-1]

return sign_str, sign_str_init

 

def get_result_url(self):

sign_str, sign_str_init = self.get_str_sign()

secretkey = CdnHelper.SecretKey

signature = bytes(sign_str, encoding=’utf-8′)

secretkey = bytes(secretkey, encoding=’utf-8′)

my_sign = hmac.new(secretkey, signature, hashlib.sha1).digest()

my_sign = base64.b64encode(my_sign)

result_sign = parse.quote(my_sign)

result_url = ‘https://’ + CdnHelper.requestHost + CdnHelper.requestUri + sign_str_init + ‘&Signature=’ + result_sign

return result_url

def flush_url(self):

result_url = self.get_result_url()

try:

r = requests.get(result_url)

print(r.json()[‘codeDesc’])

except Exception as e:

print(e)

 

def usage():

print(‘usage: FlushCDN.py [options] arg1\n’ \

‘\n’ \

‘options:\n’ \

‘ -h, –help show this help message and exit \n’ \

‘ -v, –verbose make lots of noise [default]\n’ \

‘ -u, –url Flush URL\n’ \

‘ -d, –dir Flush Dir\n’ \

”)

exit(0)

 

if __name__ == ‘__main__’:

tm = datetime.now() + timedelta(hours=-2)

startDate = endDate = tm.strftime(“%Y-%m-%d %H:00:00”)

if len(sys.argv) == 1:

usage()

exit(1)

try:

options, args = getopt.getopt(sys.argv[1:], “hvu:d:”, [‘help’, ‘verbose’, “url=”, “dir=”])

except getopt.GetoptError as e:

print(e)

exit(1)

flush_url = sys.argv[-1]

for name, value in options:

if name in (‘-h’, ‘–help’):

usage()

elif name in (‘-v’, ‘–verbose’):

print(‘Version: 1.0. Date: 2018-9-20’)

exit(0)

elif name in (‘-u’, ‘–url’):

flush_type = ‘url’

print(‘Flushing ‘ + flush_type + ‘: %s’ % flush_url)

elif name in (‘-d’, ‘–dir’):

flush_type = ‘dir’

print(‘Flushing ‘ + flush_type + ‘: %s’ % flush_url)

 

obj = CdnHelper(type=flush_type, url=flush_url, startDate=startDate, endDate=endDate)

obj.flush_url()

帮助信息如下:

主要用到两个参数-u和-d,前者是URL刷新,后者是目录刷新。

尝试刷新

腾讯云CDN管理页面能查到刷新纪录

 

本文链接地址: python刷新腾讯云cdn

转载请注明:蛋疼实验室 » python刷新腾讯云cdn

喜欢 (0)
发表我的评论
取消评论

*

表情 签到

Hi,请填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址