Skip to content

Instantly share code, notes, and snippets.

@laozhu
Last active October 21, 2016 23:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laozhu/4471752 to your computer and use it in GitHub Desktop.
Save laozhu/4471752 to your computer and use it in GitHub Desktop.
# -*- 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)
@Kingson
Copy link

Kingson commented Mar 12, 2013

可以使用request.body.read()方法拿到微信Post过来的XML数据
value = request.body.read() 这样就可以了
本地调试的问题:你可以使用Chrome或Firefox上的Advanced Rest Client插件来模拟微信服务器向你的应用发送请求,然后把Bottle的Debug功能打开,就可以看到详细的报错信息,具体的你可以看我的Gist,我的里面有具体的代码。或者联系我,我的微博是:唐僧之妈

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment