This article mainly introduced the Python programming the Push template message function, combined with the instance form analysis Python pushes the message interface the call related operation skill, needs the friend can refer to the next
This example describes the Python push template message feature. Share to everyone for your reference, as follows:
The specific code is as follows:
#!/usr/bin/env python#-*-coding:utf-8-*-import httplibimport jsonimport mysqldb# get access_tokenaccess_token= from the database "" Try:conn=mysqldb.connect (host= ' 192.168.1.1 ', user= ' root ', passwd= ' root ', db= ' db_weixin ', port=3306) cur=conn.cursor () Cur.execute (' Select Access_token from Weixin_public ') Result=cur.fetchone () #print result #print Result[0] Access_ Token=result[0] Cur.close () conn.close () except Mysqldb.error,e:print "Mysql Error%d:%s"% (E.args[0], e.args[1]) #根据接 The port push message if not Access_token is None:conn = Httplib. Httpconnection ("api.weixin.qq.com:80") #接口链接 headers = {"Content-type": "Application/json"} #application/ x-www-form-urlencoded params = ({' Touser ': "Oegz4johnkotayjbneveuazr6zq0", #用户openid ' template_id ': ' Atfuydv8k_ 15ugzufntabzjrcshckjnm1dcwd3a-11y ', #模板消息ID ' url ': ' Http://www.jb51.net ', #跳转链接 "Topcolor": "#667F00", #颜色 "data": {#模板 Content "First": {"value": "Respectable 710.so: your website http://www.jb51.net (192.168.1.1) has unusual access", "Color": "#173177"}, "Keyword1" : {"Value":"Access time 2015-04-05 15:30:59 access IP 192.168.1.2", "Color": "#173177"}, "Keyword2": {"value": "Access link http://www.jb51.net", "Color": "#173177"}, "remark": {"value": "Access Frequency 10/s", "Color": "#173177"}}) Conn.request ("POST", "/cgi-bi N/message/template/send?access_token= "+access_token, JSON. Jsonencoder (). Encode (params), headers) #推送消息请求 response = Conn.getresponse () data = Response.read () #推送返回数据 if RESPONSE.S Tatus = = 200:print ' success ' Print data else:print ' fail ' conn.close ()