利用微信公众平台实现服务器交互(python+tornado)

之前看到很多篇调用微信公众平台接口的文章,今天小试了一把,确实很不错。

实现的功能如下:

代码如下:

# -- coding: utf-8 --
import tornado.ioloop
import tornado.web
import hashlib
import commands
import xml.etree.ElementTree as ET
import time

def checksignature(signature, timestamp, nonce):
args = []
args.append(‘Your Token’) ####这里输入你的Token
args.append(timestamp)
args.append(nonce)
args.sort()
mysig = hashlib.sha1(‘’.join(args)).hexdigest()
return mysig == signature

class MainHandler(tornado.web.RequestHandler):
def get(self): ########验证时用
signature = self.get_argument(‘signature’)
timestamp = self.get_argument(‘timestamp’)
nonce = self.get_argument(‘nonce’)
echostr = self.get_argument(‘echostr’)
if checksignature(signature, timestamp, nonce):
self.write(echostr)
else:
self.write(‘fail’)

def post(self): #######简单接收和发送消息
    body = self.request.body
    data = ET.fromstring(body)
    tousername = data.find('ToUserName').text
    fromusername = data.find('FromUserName').text
    createtime = data.find('CreateTime').text
    msgtype = data.find('MsgType').text
    content = data.find('Content').text
    msgid = data.find('MsgId').text
    #print 'fromusername: %s' % fromusername
    #print 'tousername: %s' % tousername
    #print 'createtime: %s' % createtime
    #print 'msgtype: %s' % msgtype
    #print 'msgid: %s' % msgid
    if content.strip() in ('ls','pwd','w','uptime'):
        result = commands.getoutput(content)
    else:
        result = '不可以哦!!!'
    textTpl = """<xml>
        <ToUserName><![CDATA[%s]]></ToUserName>
        <FromUserName><![CDATA[%s]]></FromUserName>
        <CreateTime>%s</CreateTime>
        <MsgType><![CDATA[%s]]></MsgType>
        <Content><![CDATA[%s]]></Content>
        </xml>"""
    out = textTpl % (fromusername, tousername, str(int(time.time())), msgtype, result)
    self.write(out)

application = tornado.web.Application([
(r”/“, MainHandler),
])

if name == “main“:
application.listen(80)
tornado.ioloop.IOLoop.instance().start()