摘要:本文主要向大家介绍了Python语言微信后台开发--环境搭建与接入指南,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。
本文主要向大家介绍了Python语言微信后台开发--环境搭建与接入指南,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。
0x00 背景及介绍
申请一个微信公众平台订阅号,将后台接入到服务器上,验证服务器地址的有效性,实现简单的业务逻辑,根据用户发送不同类型的消息做出不同的反应。
0x01 语言和框架
语言:Python
框架:Django
开发包:wechat-python-sdk
Github源码地址:
https://github.com/PorridgeEater/WeChat
0x02 参考文档
微信公众平台开发文档:
//mp.weixin.qq.com/wiki/home/index.html
sdk开发包文档:
//wechat-python-sdk.com/
0x03 服务器配置
系统:CentOS
配置过程:
更新系统
yum update
安装python依赖包
yum groupinstall "Development tools"yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
安装python和pip及更新
yum install python
yum install python-pip
(sudo) pip install --upgrade pip
安装Django框架
pip install Django
安装wechat-python-sdk开发包
pip install wechat-sdk
新建一个Django实例
django-admin.py startproject PROJECT_NAMEcd PROJECT_NAME
python manage.py startapp APP_NAME
python manage.py makemigrations
python manage.py migrate
添加url规则(urls.py)
urlpatterns = [
url(r'^wechat/', views.wechat_home),
]
编写views逻辑(views.py)
#-*- coding:utf-8 -*-import sys
reload(sys)
sys.setdefaultencoding('utf8')from django.http.response import HttpResponse, HttpResponseBadRequestfrom django.views.decorators.csrf import csrf_exemptfrom wechat_sdk import WechatConffrom wechat_sdk import WechatBasicfrom wechat_sdk.exceptions import ParseErrorfrom wechat_sdk.messages import (TextMessage, VoiceMessage, ImageMessage, VideoMessage, LinkMessage, LocationMessage, EventMessage, ShortVideoMessage)
conf = WechatConf(
token='YOUR_TOKEN_HERE',
appid='YOUR_APPID',
appsecret='YOUR_APPSECRET',
encrypt_mode='YOUR_MODE',
encoding_aes_key='YOUR_AES_KEY')@csrf_exemptdef wechat_home(request):
signature = request.GET.get('signature')
timestamp = request.GET.get('timestamp')
nonce = request.GET.get('nonce')
wechat_instance = WechatBasic(conf=conf) if not wechat_instance.check_signature(signature=signature, timestamp=timestamp, nonce=nonce): return HttpResponseBadRequest('Verify Failed') else: if request.method == 'GET':
response = request.GET.get('echostr', 'error') else: try:
wechat_instance.parse_data(request.body)
message = wechat_instance.get_message()
if isinstance(message, TextMessage):
reply_text = 'text'
elif isinstance(message, VoiceMessage):
reply_text = 'voice'
elif isinstance(message, ImageMessage):
reply_text = 'image'
elif isinstance(message, LinkMessage):
reply_text = 'link'
elif isinstance(message, LocationMessage):
reply_text = 'location'
elif isinstance(message, VideoMessage):
reply_text = 'video'
elif isinstance(message, ShortVideoMessage):
reply_text = 'shortvideo'
else:
reply_text = 'other'
response = wechat_instance.response_text(content=reply_text) except ParseError:
return HttpResponseBadRequest('Invalid XML Data') return HttpResponse(response, content_type="application/xml")
开启django app,后台挂载在80端口
sudo python manage.py runserver 0.0.0.0:80 &
0x04 微信后台配置
记录APPID和APPSecret填入views.py的conf属性
填写服务器配置
注意URL最后带上/,否则django会报POST URL error
自定义token,填入views.py的conf属性
自定义EncodingAESKey,填入views.py的conf属性
基本配置
0x05 遇到的坑
runserver后本地能够访问,外网不能访问
绑定ip到0.0.0.0,设置为对公监听即可
输入中文无法响应
import os后设置编码为utf8
端口号被占用
ps aux | grep manage后然后kill -9 相应进程号
0x06 后记
能够识别不同的消息类型并进行相应回复
本文由职坐标整理并发布,希望对同学们学习Python有所帮助,更多内容请关注职坐标编程语言Python频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号