全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

IP归属甄别会员请立即修改密码
查看: 608|回复: 2
打印 上一主题 下一主题

借着chatgpt搞了个无聊的东西,公众号查询数字在π的位置

[复制链接]
跳转到指定楼层
1#
发表于 2023-2-25 15:49:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
首先是pi的前10亿位的文本

wget https://jihulab.com/loc/pi/-/raw/main/pi.txt

  1. from flask import Flask, request
  2. import hashlib
  3. import xml.etree.ElementTree as ET
  4. import time

  5. app = Flask(__name__)

  6. with open('pi.txt', 'r') as f:
  7.     pi = f.read()

  8. @app.route('/wechat', methods=['GET', 'POST'])
  9. def wechat():
  10.     if request.method == 'GET':
  11.         # 验证服务器
  12.         token = 'xxxxxxxxxx'
  13.         data = request.args
  14.         signature = data.get('signature', '')
  15.         timestamp = data.get('timestamp', '')
  16.         nonce = data.get('nonce', '')
  17.         echostr = data.get('echostr', '')
  18.         s = [timestamp, nonce, token]
  19.         s.sort()
  20.         s = ''.join(s)
  21.         if hashlib.sha1(s.encode('utf-8')).hexdigest() == signature:
  22.             return echostr
  23.         else:
  24.             return 'signature verification failed'
  25.     else:
  26.         # 处理用户发送的消息
  27.         xml_str = request.data
  28.         root = ET.fromstring(xml_str)
  29.         ToUserName = root.findtext('ToUserName')
  30.         FromUserName = root.findtext('FromUserName')
  31.         MsgType = root.findtext('MsgType')
  32.         if MsgType == 'text':
  33.             Content = root.findtext('Content')
  34.             if Content.isdecimal():                
  35.                 pos = pi.find(Content)
  36.                 if pos == -1:
  37.                     reply = '您查询的数字不在pi的前10亿位中'
  38.                 else:
  39.                     reply = '您好,您查询的数字在π的前10亿位中排第' + str(pos) + '位'
  40.             else:
  41.                 reply = '您输入的不是数字,请重新输入数字'
  42.             reply_xml = f"""
  43.             <xml>
  44.                 <ToUserName><![CDATA[{FromUserName}]]></ToUserName>
  45.                 <FromUserName><![CDATA[{ToUserName}]]></FromUserName>
  46.                 <CreateTime>{int(time.time())}</CreateTime>
  47.                 <MsgType><![CDATA[text]]></MsgType>
  48.                 <Content><![CDATA[{reply}]]></Content>
  49.             </xml>
  50.             """
  51.             return reply_xml
  52.         else:
  53.             return ''

  54. if __name__ == '__main__':
  55.     app.run(host='0.0.0.0',port='80')
复制代码



这个是我搭建的公众号



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

也就是会占用900多m的内存
2#
发表于 2023-2-25 17:35:07 | 只看该作者
用它来锻炼机器么
3#
发表于 2023-2-25 18:32:47 | 只看该作者
的确是无聊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2025-11-7 21:42 , Processed in 0.058834 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表