有需要的拿走,搞运维的,代码写的很差劲,勿喷。
#!/usr/bin/env python
#coding:utf-8
#code by struggle
import urllib
import urllib2
import json
class Weixin(object):
def __init__(self, appid, secretid):
self.appid = appid
self.secretid = secretid
def get_token(self):
try:
tokenouput = urllib2.urlopen('
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s' % (self.appid, self.secretid))
return eval(tokenouput.read())['access_token']
except:
return
def send_wx(self, tokenid, tofakeid, subject, textinfo, tempid='默认的模板', color_text='#ff0000' ,sevrity='WARING'):
try:
send_url = '
https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' + tokenid
post_data =json.dumps({
'touser':tofakeid,
'template_id':tempid,
'url':'
http://weixin.qq.com/download',
'data':{
'first':{
'value':subject,
'color':color_text
},
'keyword1':{
'value':sevrity,
'color':'#173177'
},
'keyword2':{
'value':textinfo,
'color':'#173177'
}
}
})
send_req = urllib2.Request(send_url,post_data)
send_post = urllib2.urlopen(send_req)
return send_post.read()
except:
return
if __name__ == "__main__":
pass
#wx = Weixin(appid, secretid)
#token = wx.get_token()
#wx.send_wx(token, 'fakeid', 'class test', 'go to school')