|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
flaskwx.py
- # -*- coding: utf-8 -*-
- import time
- from flask import Flask, request, abort
- import hashlib
- import xmltodict
- # from wechatpy import parse_message, create_reply
- WECHAT_TOKEN = '37ck20200808'
- app = Flask(__name__)
- # 格式处理
- def make(xmldict, t_text):
- res_dict = {
- 'xml': {
- 'ToUserName': xmldict.get('FromUserName'),
- 'FromUserName': xmldict.get('ToUserName'),
- 'CreateTime': int(time.time()),
- 'MsgType': 'text',
- 'Content': t_text
- }
- }
- return res_dict
- # 微信入口
- @app.route('/wx', methods=['GET', 'POST'])
- def wx2024():
- signature = request.args.get("signature")
- timestamp = request.args.get("timestamp")
- nonce = request.args.get("nonce")
- echostr = request.args.get("echostr")
- if not all([signature, timestamp, nonce]):
- abort(400)
- li = [WECHAT_TOKEN, timestamp, nonce]
- li.sort()
- t_str = ''.join(li)
- sign = hashlib.sha1(t_str.encode('utf-8')).hexdigest()
- if signature != sign:
- abort(403)
- else:
- if request.method == 'GET':#GET获取数据和验证
- echostr = request.args.get('echostr')
- if not echostr:
- abort(400)
- return echostr
- elif request.method == 'POST':#数据提交交互部分,人机应答入口
- xml_s = request.data
- if not xml_s:
- abort(400)
- # 获取微信公众平台信息
- xml_dict = xmltodict.parse(xml_s)
- xml_dict = xml_dict.get('xml')
- msg_type = xml_dict.get('MsgType')
- Content = xml_dict.get('Content')
- # 各个成员功能识别入口
- if msg_type == 'text':
- if Content == '37创客':
- Content = '科技兴国从我做起'
- elif Content == '全梓豪':
- Content = '全梓豪已经通过测试'
- elif Content == '欧阳尚宏':
- Content = '欧阳尚宏已经通过测试'
- else:
- Content = '其他奥利给混进来了,请注意安全!'
- res_dict = make(xml_dict, Content)
- res_xml_str = xmltodict.unparse(res_dict)
- return res_xml_str
- # return 'success'
- if __name__ == '__main__':
- app.run(host='0.0.0.0', port=85, debug=True)
复制代码
|
|