标签:https port ons ini sys.argv load return ken lib
#!/usr/bin/env python
# coding:utf-8
import sys
import urllib2
import time
import json
import requests
reload(sys)
sys.setdefaultencoding(‘utf-8‘)
title = sys.argv[1]
content = sys.argv[2]
class Token(object):
# 获取token
def __init__(self, corpid, corpsecret):
self.baseurl = ‘https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}‘.format(corpid, corpsecre
t)
self.expire_time = sys.maxint
def get_token(self):
if self.expire_time > time.time():
request = urllib2.Request(self.baseurl)
response = urllib2.urlopen(request)
ret = response.read().strip()
ret = json.loads(ret)
if ‘errcode‘ in ret.keys():
print >> ret[‘errmsg‘], sys.stderr
sys.exit(1)
self.expire_time = time.time() + ret[‘expires_in‘]
self.access_token = ret[‘access_token‘]
return self.access_token
def send_msg(title, content):
# 发送消息
corpid = "在微信企业号后台获取"
corpsecret = "在微信企业号后台获取"
qs_token = Token(corpid=corpid, corpsecret=corpsecret).get_token()
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}".format(qs_token)
payload = {
"touser": "@all",
"msgtype": "text",
"agentid": "0",
"text": {
"content": "标题:{0}\n 内容:{1}".format(title, content)
},
"safe": "0"
}
ret = requests.post(url, data=json.dumps(payload, ensure_ascii=False))
print ret.json()
if __name__ == ‘__main__‘:
# print title, content
send_msg(title, content)
标签:https port ons ini sys.argv load return ken lib
原文地址:http://www.cnblogs.com/palmxin/p/7158657.html