Tornado 微信企业号给特定人发送信息

# -*- coding: utf-8 -*-
__author__ = 'liwf'
import tornado.ioloop
import tornado.web
import os.path
import tornado.httpserver
import tornado.options
from tornado.options import define, options
from wechatpy.enterprise.crypto import WeChatCrypto
from wechatpy.enterprise import parse_message, create_reply
from wechatpy.enterprise.client import WeChatClient

import tornado.web
import tornado.gen
import json

define("port", default=40001, help="run port", type=int)

define("wx_TOKEN", default="dsafsadf")
define("wx_EncodingAESKey", default="sdsdsd")
define("wx_CorpId", default="wxdsfafdfdffsfa")
define("wx_SecretTXL", default="dfsafddsafdsfdsaf")
define("wx_Agent_id", default="11")

class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r'/sendText$', SendTextHandler)
        ]
        settings = dict(
            template_path=os.path.join(os.path.dirname(__file__), "templates"),
            static_path=os.path.join(os.path.dirname(__file__), "static"),
            #xsrf_cookies=True,
            cookie_secret="dsafdsafsfaaaaa",
            debug=True,
        )
        super(Application, self).__init__(handlers, **settings)

        self.wx_client = WeChatClient(options.wx_CorpId, options.wx_SecretTXL)
        self.crypto = WeChatCrypto(options.wx_TOKEN, options.wx_EncodingAESKey, options.wx_CorpId)

class BaseHandler(tornado.web.RequestHandler):
    #@property
    #def db(self):
    #    return self.application.db
    @property
    def wx_client(self):
        return self.application.wx_client

    @property
    def crypto(self):
        return self.application.crypto


class SendTextHandler(BaseHandler):
    def post(self):
        try:
            print self.request.body
            j = json.loads(self.request.body)
            print j
            self.wx_client.message.send_text(options.wx_Agent_id, j['tousers'], j['msg'])
        except Exception,e:
            print e.message
            self.wx_client.message.send_text(options.wx_Agent_id, 'liwfwwww', e.message)

class WxVerifyHandler(BaseHandler):
    def get(self):
        signature = self.get_argument('msg_signature')
        timestamp = self.get_argument('timestamp')
        nonce = self.get_argument('nonce')
        echostr = self.get_argument('echostr')
        try:
            echostr = self.crypto.check_signature(
                signature,
                timestamp,
                nonce,
                echostr
            )
        except Exception as e:
            print e.message
        print echostr
        self.write(echostr)
    def post(self):
        body = self.request.body
        signature = self.get_argument('signature')
        timestamp = self.get_argument('timestamp')
        nonce = self.get_argument('nonce')
        msg = self.crypto.decrypt_message(
            body,
            signature,
            timestamp,
            nonce
        )
        print  "msg:-------::::" + msg
        msg = parse_message(msg)
        print  "msg parse_message--------------:"
        print msg
        reply = None
        if msg.event == 'scancode_waitmsg' and msg.key == 'key_scan_baodao':
            #报名
            print "报到处理..."
        self.write("xxx")



def main():
    tornado.options.parse_command_line()
    http_server = Application()
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()

if __name__ == "__main__":
    main()

调用方式

# -*- coding: utf-8 -*-
__author__ = 'liwf'

import urllib2
import urllib
import json
def postHttp(tousers,msg):
    url="http://wwwwwwwwxxxxxx/sendText"
    postdata={"tousers":tousers,"msg":msg}
    #postdata=urllib.urlencode(postdata)
    request = urllib2.Request(url, json.dumps(postdata))
    response=urllib2.urlopen(request)
    print response


postHttp("liwfwwww|sfdsjf020202","测试消息发送")