需要了解更多关注:https://blog.youkuaiyun.com/linbossdeboke
qq交流群:651587317
# -*- coding: utf_8 -*-
#python 响应微信公众号的库(pip 安装就行)
import itchatmp
#python 连接数据库
import MySQLdb
import re
import time
#参数初始化 ,参数:token,appid,appSecret
itchatmp.update_config(itchatmp.WechatConfig(
token='linbossdetoken',
appId = '###e3afb71137f940',
appSecret = '##18c4828df0d0ca090833282ed7894c7'))
@itchatmp.msg_register(itchatmp.content.TEXT)
#消息回应
def text_reply(msg):
#打印接收到微信端的消息
print msg
name= msg['FromUserName']
#需要 匹配的消息内容
com= msg['Content']
print name
print com
if (com==u'花盆土壤湿度'):
conn=MySQLdb.connect(
host='39.105.45.48',
port = 3306,
user='root',
passwd='1416520104',
db ='test',
)
print u'连接成功'
sql1='SELECT data from bs where time=(select max(time) from bs)'
sql2='SELECT time from bs where time=(select max(time) from bs)'
cursor =conn.cursor()
cursor.execute(sql1)
str1=cursor.fetchall()
sr1=str(str1[0])
num = re.sub(r'\D', "", sr1)
cursor.execute(sql2)
str2=cursor.fetchall()
#for row1 in str2:
sr2=str(str2[0])
num1=re.sub(r'\(|\)|datetime|\.', "",sr2)
str3=u"土壤湿度:"+num+u"数据获取时间:"+num1
# cursor.execute("insert into bs(data,time) values('"+value+"','"+times+"')")
conn.commit()
conn.close()
return str3
itchatmp.run()