首先是pi的前10亿位的文本
wget https://jihulab.com/loc/pi/-/raw/main/pi.txt
- from flask import Flask, request
- import hashlib
- import xml.etree.ElementTree as ET
- import time
- app = Flask(__name__)
- with open('pi.txt', 'r') as f:
- pi = f.read()
- @app.route('/wechat', methods=['GET', 'POST'])
- def wechat():
- if request.method == 'GET':
- # 验证服务器
- token = 'xxxxxxxxxx'
- data = request.args
- signature = data.get('signature', '')
- timestamp = data.get('timestamp', '')
- nonce = data.get('nonce', '')
- echostr = data.get('echostr', '')
- s = [timestamp, nonce, token]
- s.sort()
- s = ''.join(s)
- if hashlib.sha1(s.encode('utf-8')).hexdigest() == signature:
- return echostr
- else:
- return 'signature verification failed'
- else:
- # 处理用户发送的消息
- xml_str = request.data
- root = ET.fromstring(xml_str)
- ToUserName = root.findtext('ToUserName')
- FromUserName = root.findtext('FromUserName')
- MsgType = root.findtext('MsgType')
- if MsgType == 'text':
- Content = root.findtext('Content')
- if Content.isdecimal():
- pos = pi.find(Content)
- if pos == -1:
- reply = '您查询的数字不在pi的前10亿位中'
- else:
- reply = '您好,您查询的数字在π的前10亿位中排第' + str(pos) + '位'
- else:
- reply = '您输入的不是数字,请重新输入数字'
- reply_xml = f"""
- <xml>
- <ToUserName><![CDATA[{FromUserName}]]></ToUserName>
- <FromUserName><![CDATA[{ToUserName}]]></FromUserName>
- <CreateTime>{int(time.time())}</CreateTime>
- <MsgType><![CDATA[text]]></MsgType>
- <Content><![CDATA[{reply}]]></Content>
- </xml>
- """
- return reply_xml
- else:
- return ''
- if __name__ == '__main__':
- app.run(host='0.0.0.0',port='80')
复制代码
这个是我搭建的公众号

去随便创建个公众号,把程序的token改一下,然后地址填ip/wechat,就行了,注意为了查询更快,这个flask会把文本读到内存
也就是会占用900多m的内存 |