前言
![](https://img-blog.csdnimg.cn/img_convert/9a78de340cef0d83dfc38c36544bb444.png)
![](https://img-blog.csdnimg.cn/img_convert/7ca31cbd1cb981d069f4cccc72c80234.png)
python发送消息
![](https://img-blog.csdnimg.cn/img_convert/5df4a99b9445192a0ca810de7b948303.png)
# coding:utf-8
import requests
import json
# 这里的地址是机器人的地址webhook地址,自行申请使用
url = 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx'
headers = {'Content-Type': 'application/json'}
# 参数内容
data = {"msgtype":
"text","text":
{"content":"缺陷名称:今天你有学习吗?"},
"at": {
# 要@的人
"atMobiles": " ",
# 是否@所有人
"isAtAll": False
}
}
r = requests.post(url,headers=headers,data=json.dumps(data))
print(r.status_code)
print(r.text)
![](https://img-blog.csdnimg.cn/img_convert/228cc6a2cab6cf1a596c95f612f814e0.png)
![](https://img-blog.csdnimg.cn/img_convert/d4b7a9697244aea54556a7135f5309e1.png)
![](https://img-blog.csdnimg.cn/img_convert/3eefbc943ade545012df28586bd46ed0.png)
![](https://img-blog.csdnimg.cn/img_convert/d5cfcf26028d3e986fc05aa6c9c0edf9.png)
![](https://img-blog.csdnimg.cn/img_convert/c33d711e42658c4e92bc935552f10553.png)
import pymysql
# 连接数据库
count = pymysql.connect(
host = '127.0.0.1', # 数据库地址
port = 3306, # 数据库端口号
user='root', # 数据库账号
password='821006052', # 数据库密码
db= 'zentao', # 数据库表名
charset = 'gbk' # 中文乱码
)
# 完成mysql数据库实例化
db = count.cursor()
# sql语句
sql = 'select title from zt_bug'
# 执行sql
a = db.execute(sql)
# 查找所以内容
result = db.fetchall()
print(result)
# coding:utf-8
import pymysql
import requests
import json
# 连接数据库
def MYsql(sql):
a = []
count = pymysql.connect(
host = '127.0.0.1', # 数据库地址
port = 3306, # 数据库端口号
user='root', # 数据库账号
password='821006052', # 数据库密码
db = 'zentao') # 数据库表名
# 创建数据库对象
db = count.cursor()
# 写入SQL语句
sql_ = sql
# 执行sql命令
db.execute(sql_)
# 获取全部的查询内容
restul = db.fetchall()
# 将查询的结果添加到列表中
for i in restul:
a.append(i[0])
db.close()
return a
def DingDing(text):
# 钉钉机器人的webhook的地址
url = 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxx'
headers = {'Content-Type': 'application/json'}
data = {"msgtype":
"text", "text":
{"content": "缺陷名称:以下是缺陷标题%s" %text},
"at": {
# 要@的人
"atMobiles": "",
# 是否@所有人
"isAtAll": False
}
}
r = requests.post(url, headers=headers, data=json.dumps(data))
print(r.status_code)
print(text)
result = MYsql("select title from zt_bug")
DingDing(result)
![](https://img-blog.csdnimg.cn/img_convert/5981bbf285a15d991987e832a6cd3cb8.png)
![](https://img-blog.csdnimg.cn/img_convert/9524e27857a2a6d3d81f8380c0300640.png)
![](https://img-blog.csdnimg.cn/img_convert/3b70a0b88bc11ae4dbf4e3e44d102b53.png)