在Python的Bottle框架中使用微信API的示例
作者:chairo 时间:2022-06-02 00:12:47
微信这个东西估计宅男没几个不熟悉的吧,微信经过这么两年多的发展终于向开放平台跨出了友好的一步。蛋疼的以为微信会出一个详细的api等接口,兴奋不已的去申请了微信公共平台,然后开始找各种api的位置……
花费了近一个小时,依然没找到……
最后动用Google大杀器,终于找到了这么个链接。我了个去的,没比这还简单的api文档了吧。
最让人无法理解的是:居然没有本地开发环境支持,每次都要放在生产环境去调试。
最让人欣慰的是:就那么俩方法,生产环境调试几次也就完事了。
Python(bottle)版代码如下:
# -*- coding:utf-8 -*-
from bottle import debug, default_app, run, get, request, post
import sys, os, time, libxml2dom
@get('/')
def index():
return request.GET.get('echostr')
@post('/')
def index_post():
for key, value in request.POST.allitems():
doc = libxml2dom.parseString(key)
_to = doc.xpath('//FromUserName')[0].textContent
_from = doc.xpath('//ToUserName')[0].textContent
#_content = doc.xpath('//Content')[0].textContent
return """<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>"""%(_to, _from, int(time.time()), u'我了个去啊')
if __name__ == "__main__":
# Interactive mode
debug(True)
port = int(sys.argv[1] if len(sys.argv) > 1 else 8888)
run(host='0.0.0.0', port=port, reloader=True)
else:
# Mod WSGI launch
os.chdir(os.path.dirname(__file__))
app = default_app()
演示直接看图:
标签:Python,微信
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python Cookie 读取和保存方法
2021-01-21 15:57:51
类似google的ASP分页代码[测试通过]
2009-03-13 12:43:00
Python2和Python3中@abstractmethod使用方法
2021-12-29 08:55:36
Django实现跨域的2种方法
2021-11-27 00:15:42
13个超酷的js显示时间效果
2007-08-30 09:52:00
![](https://img.aspxhome.com/file/UploadPic/20078/30/2007830103514730s.jpg)
如何用 Python 处理不平衡数据集
2023-02-21 07:35:31
![](https://img.aspxhome.com/file/2023/9/101209_0s.png)
让你的空间支持域名绑定子目录的解决办法
2010-09-15 10:03:00
python爬虫用request库处理cookie的实例讲解
2023-07-26 03:57:53
CentOS 6.3安装配置nginx+php+mysql
2023-11-24 11:11:33
python读取当前目录下的CSV文件数据
2022-04-06 16:50:35
![](https://img.aspxhome.com/file/2023/6/87496_0s.jpg)
Python实现的径向基(RBF)神经网络示例
2022-03-06 23:44:35
Python对列表的操作知识点详解
2022-05-08 09:06:39
![](https://img.aspxhome.com/file/2023/9/82939_0s.png)
浅析BootStrap模态框的使用(经典)
2023-08-20 02:21:19
Python Requests安装与简单运用
2023-09-26 22:02:15
python工具快速为音视频自动生成字幕(使用说明)
2021-04-14 15:15:26
巧用Dreamweaver制作复杂图像
2010-09-02 12:34:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113139567s.jpg)
python卸载后再次安装遇到的问题解决
2022-06-10 01:58:09
![](https://img.aspxhome.com/file/2023/1/89231_0s.png)
用asp获取微软安全更新列表的代码 小偷程序
2011-02-24 11:19:00
Oracle 数据库 临时数据的处理方法
2009-07-02 11:48:00
Jquery中Ajax 缓存带来的影响的解决方法
2011-05-21 16:14:00