Linux平台python3基于tornado微信号二次开发

**STEP1:**
首先我们需要安装python、tornado、ngrok(或者其他映射工具)、wechatpy。
安装python3
```shell
sudo apt-get install python3
```
在安装tornado和wechat时需要用pip,所以还需要安装pip
```shell
sudo apt-get install python3-pip
```
接下来安装tornado和wechatpy
```shell
pip3 install tornado wechatpy
```
下载ngrok工具可以自己到官方网站下载
ngrok官网: [ngrok](https://ngrok.com/).
在官网找到自己需要的版本复制下载链接下载。
![ngrok官网下载](https://img-blog.csdnimg.cn/2020042220224010.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzg4MjUwNw==,size_16,color_FFFFFF,t_70)
例如:
```shell
wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
```
ngrok工具配置好后使用映射功能
```shell
./ngrok http 80
```
可以得到一个公网的映射url
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200422203618669.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzg4MjUwNw==,size_16,color_FFFFFF,t_70)
tips:也可以自己网上搜索安装教程,例如git clone安装,记得对应版本
至此准备工作基本完成,按照过程中出现问题可以尝试自行搜索解决
********************************分割线---------------------------------------------
**STEP2:**
申请注册公众号,个人一般只能注册订阅号......
进入公众号管理界面。
![公众号管理配置](https://img-blog.csdnimg.cn/20200422203017128.png)
配置界面
![具体配置内容](https://img-blog.csdnimg.cn/20200422203304206.png)
第一行填写利用ngrok工具映射的公网url加上你想设定的设定的表述层名称
例如:
```shell
http://dea517cf.ngrok.io/connect
```
connect就是我们为微信公众号提供的restful资源
第二行设定Token(也就是我们设定的密码)
第三行自动生成即可
第四行加密方式选择明文
此时提交还不能成功,因为我们的tornado服务还未启动,上面设定的url访问也没有处理反馈。
********************************分割线---------------------------------------------
接下来我们进行tornado程序的编写
**STEP3:**
创建一个叫my_test.py的文件
```python
import tornado.web
import tornado.ioloop
import tornado.options
import tornado.httpserver
from wechatpy.utils import check_signature
from wechatpy.exceptions import InvalidSignatureException
from wechatpy import parse_message
from wechatpy.replies import TextReply, ImageReply

#以上是我们需要用到的库文件或函数
class ConnectHandler(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')
try:
result = check_signature(token='你的Token',signature=signature,timestamp=timestamp,nonce=nonce)
self.write(echostr)
if result:
print(echostr)
else:
print("failed")
except InvalidSignatureException:
pass

def post(self):
body = self.request.body
msg = parse_message(body)
query = msg.content
print(query)
if query !='' and msg.type == 'text':
"""
完成你想实现的内容,此处我们是发回hello world
可以尝试接入百度云、阿里云等平台的接口处理
"""
reply = TextReply(content="hello world", message=msg)
xml = reply.render()
self.write(xml)
else:
reply = TextReply(content="error", message=msg)
xml = reply.render()
self.write(xml)
application = tornado.web.Application([
(r"/connect",ConnectHandler)])#设定表述层名
def start_server():
port = '80'
try:
application.listen(int(port))
tornado.ioloop.IOLoop.instance().start()
except Exception as e:
print(e)
if __name__=="__main__":
start_server()#启动tornado循环
```
接下来我们运行代码
```shell
#因为端口小于1024所以需要root权限,linux小知识
sudo python3 my_test.py
```
接下来去提交微信平台点击提交,在微信上即可开始对话了,不过现在它只会回应你hello world
********************************分割线---------------------------------------------
**成果展示**
![成果内容](https://img-blog.csdnimg.cn/2020042221114669.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzg4MjUwNw==,size_16,color_FFFFFF,t_70)
点个赞再走吧,哈哈哈
#Python#
全部评论

相关推荐

9 7 评论
分享
牛客网
牛客企业服务