• - import base64
  • import hmac
  • import random
  • import time
  • import hashlib
  • - import requests
  • - # 申请地址:https://console.cloud.tencent.com/cam
  • SECRET_ID = ''
  • SECRET_KEY = ''
  • # 策略ID请去https://console.cloud.tencent.com/monitor/message 创建
  • POLICY_ID = ''
  • - - def tx_push(text):
  • params = {
  • 'Action': 'SendCustomAlarmMsg',
  • 'Region': 'ap-shanghai',
  • 'Timestamp': str(int(time.time())),
  • 'Nonce': str(random.randint(0, 65535)),
  • 'SecretId': SECRET_ID,
  • 'policyId': POLICY_ID,
  • 'msg': text
  • }
  • keys = sorted(params.keys())
  • src_str = 'GETmonitor.api.qcloud.com/v2/index.php?'
  • for key in keys:
  • src_str += '%s=%s&' % (key, params)
  • src_str = src_str[:-1]
  • sign_str = base64.b64encode(
  • hmac.new(SECRET_KEY.encode('utf-8'), src_str.encode('utf-8'), hashlib.sha1).digest()).decode()
  • params['Signature'] = sign_str
  • try:
  • requests.get('https://monitor.api.qcloud.com/v2/index.php', params=params)
  • except requests.RequestException as e:
  • pass
  • 复制代码

调用方法

  • from tx_push import tx_push
  • tx_push(text)复制代码