Last active
October 21, 2016 23:14
-
-
Save laozhu/4471752 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from lxml import etree | |
from bottle import Bottle, run, request | |
import sae | |
app = Bottle() | |
""" | |
此处省略若干行 | |
""" | |
@app.post('/wx') | |
def wechatPost(): | |
value = request.POST.values() | |
root = etree.fromstring(value[0]) | |
child= list(root) | |
wechatRecv = {} | |
for item in child: | |
wechatRecv[item.tag] = item.text | |
textTpl = """<xml> | |
<ToUserName><![CDATA[%s]]></ToUserName> | |
<FromUserName><![CDATA[%s]]></FromUserName> | |
<CreateTime>%s</CreateTime> | |
<MsgType><![CDATA[%s]]></MsgType> | |
<Content><![CDATA[%s]]></Content> | |
<FuncFlag>0</FuncFlag> | |
</xml>""" | |
wechatReply = textTpl % ( | |
wechatRecv['FromUserName'], | |
wechatRecv['ToUserName'], | |
wechatRecv['CreateTime'], | |
wechatRecv['MsgType'], | |
wechatRecv['Content'] | |
) | |
return wechatReply | |
application = sae.create_wsgi_app(app) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
可以使用request.body.read()方法拿到微信Post过来的XML数据
value = request.body.read() 这样就可以了
本地调试的问题:你可以使用Chrome或Firefox上的Advanced Rest Client插件来模拟微信服务器向你的应用发送请求,然后把Bottle的Debug功能打开,就可以看到详细的报错信息,具体的你可以看我的Gist,我的里面有具体的代码。或者联系我,我的微博是:唐僧之妈