python3判断正式和测试参数来发送钉钉通知
最近搞了几个脚本,但配置钉钉报警通知时,老是要把加签这几个参数改来改去,对于我这样的懒人来说,这不能忍受啊,直接就整了个判断,测试脚本的时候就把通知发送到我测试的钉钉机器人那边,测试完成后再改成正式的钉钉机器人通知就好了.
#!/usr/bin/env python # -*- coding: UTF-8 -*- ''' @IDE :PyCharm @Author :rocdk890 @Date :2021/12/29 17:32 ''' import sys import time import hmac import hashlib import base64 import urllib.parse import requests import json project = "dev" if project == 'dev': timestamp = str(round(time.time() * 1000)) secret = '*****' #改成你自己的 secret_enc = secret.encode('utf-8') string_to_sign = '{}\n{}'.format(timestamp, secret) string_to_sign_enc = string_to_sign.encode('utf-8') hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest() sign = urllib.parse.quote(base64.b64encode(hmac_code)) ddtoken = "*******" #改成你自己的 elif project == 'pro': timestamp = str(round(time.time() * 1000)) secret = '*******' #改成你自己的 secret_enc = secret.encode('utf-8') string_to_sign = '{}\n{}'.format(timestamp, secret) string_to_sign_enc = string_to_sign.encode('utf-8') hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest() sign = urllib.parse.quote(base64.b64encode(hmac_code)) ddtoken = "*********" #改成你自己的 else: print("请检查project参数是否正确!") sys.exit(0) def dingding_robot(data): headers = {'Content-Type': 'application/json'} webhook = 'https://oapi.dingtalk.com/robot/send?access_token=' + ddtoken + "×tamp=" + timestamp + "&sign=" + sign r = requests.post(webhook, headers=headers, data=json.dumps(data)) r.encoding = 'utf-8' return (r.text) def get_msg(text_content): text = { "msgtype": "text", "text": { "content": text_content }, } return (text) if __name__ == '__main__': text_content = "测试" data = get_msg(text_content) dingding_robot(data)
评论: