python钉钉机器人运维脚本监控实例

yipeiwu_com6年前Python基础

如下所示:

python钉钉机器人运维脚本监控

python钉钉机器人运维脚本监控

#!/usr/bin/python3
# -*- coding:UTF-8-*-
# Author: zhuhongqiang
 
from urllib import request
import json
from sys import argv
 
access_token = "xxx"
 
 
def send_msg(mobile, item_name):
  """
   钉钉机器人API接口地址:
   https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.karFPe&treeId=257&articleId=105735&docType=1
   :param mobile:
   :param itemName:
   :return:
  """
  url = "https://oapi.dingtalk.com/robot/send?access_token=" + access_token
 
  data = {
    "msgtype": "text",
    "text": {
      "content": item_name
    },
    "at": {
      "atMobiles": [
        mobile
      ],
      "isAtAll": "false"
    }
  }
  # 设置编码格式
  json_data= json.dumps(data).encode(encoding='utf-8')
  print(json_data)
  header_encoding = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko', "Content-Type": "application/json"}
  req = request.Request(url=url, data=json_data, headers=header_encoding)
  res = request.urlopen(req)
  res = res.read()
  print(res.decode(encoding='utf-8'))
 
 
if __name__ == "__main__":
  mobile = argv[1]
  item_name = argv[2]
  send_msg(mobile, item_name)

以上这篇python钉钉机器人运维脚本监控实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

一个基于flask的web应用诞生 bootstrap框架美化(3)

一个基于flask的web应用诞生 bootstrap框架美化(3)

经过上一章的内容,其实就页面层来说已结可以很轻松的实现功能了,但是很明显美观上还有很大的欠缺,现在有一些很好的前端css框架,如AmazeUI,腾讯的WeUI等等,这里推荐一个和flas...

详解使用python的logging模块在stdout输出的两种方法

详解使用python的logging模块在stdout输出 前言:   使用python的logging模块时,除了想将日志记录在文件中外,还希望在前台执行python脚本时,可以将日志...

状态机的概念和在Python下使用状态机的教程

什么是状态机? 关于状态机的一个极度确切的描述是它是一个有向图形,由一组节点和一组相应的转移函数组成。状态机通过响应一系列事件而“运行”。每个事件都在属于“当前”节点的转移函数的控制范围...

Python的Django框架中使用SQLAlchemy操作数据库的教程

零、SQLAlchemy是什么? SQLAlchemy的官网上写着它的介绍文字: SQLAlchemy is the Python SQL toolkit and Object Rela...

对pandas写入读取h5文件的方法详解

1、引言 通过参考相关博客对hdf5格式简要介绍。 hdf5在存储的是支持压缩,使用的方式是blosc,这个是速度最快的也是pandas默认支持的。 使用压缩可以提磁盘利用率,节省空间。...