微信开发入门(一)用python开发微信公众号

需要了解更多关注: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()
 
        
        
        
        
        
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值