注意缩进和冒号,交流q群:287714361
#coding: utf-8
import sae
import web
import xml.etree.ElementTree as ET
urls = (
'/', 'Hello'
)
class Hello:
def GET(self):
data=web.input()
echostr=data.echostr
return echostr
def POST(self):
data=web.data()
root = ET.fromstring(data)
fromUser=root.findtext(".//FromUserName")
toUser=root.findtext(".//ToUserName")
CreateTime=root.findtext(".//CreateTime")
MsgType=root.findtext(".//MsgType")
Event=root.findtext(".//Event")
if MsgType=="text":
reply="文本消息"
elif MsgType=="image":
reply="图片消息"
elif MsgType=="voice":
reply="语音消息"
elif MsgType=="video":
reply="视频消息"
elif MsgType=="location":
reply="地理消息"
elif MsgType=="link":
reply="链接消息"
elif MsgType=="event" and Event=="subscribe":
reply="关注消息"
else:
reply="未知类型"
texttpl='''<xml>
<ToUserName>'''+fromUser+'''</ToUserName>
<FromUserName>'''+toUser+'''</FromUserName>
<CreateTime>'''+CreateTime+'''</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content>'''+reply+'''</Content>
</xml>'''
return texttpl
app = web.application(urls, globals()).wsgifunc()
application = sae.create_wsgi_app(app)