Py学习记录
Python 常规学习
Python学习笔记(一)
Python学习笔记(二)
Python学习笔记(三)
Python学习笔记(四)
Python学习笔记(五)
Python学习笔记(六)
Python学习笔记(七)
Python习题(一)
Python习题(二)
Python习题(三)
Python习题(四)
Python习题(五)
Python常见Bug
Python编程环境
Python-依赖安装(三方库)
Python-VS Code
pip-换源
py 程序转 exe
Python-打开选择文件对话框
Python 项目
Python-密码学
Python-与佛伦禅
Python-喵语翻译
Python-翻译服务器
Python-邮件发送
Python-自动签到
Python-自动签到(Post请求)
Python-自动签到(模拟操作)
Python-图片添加二维码
Python-数据可视化
Python-端口扫描器
Python-未测试项目
Python-虚拟环境
Python-临时环境
Python-venv虚拟环境
Python-Conda
Python-OpenCV
OpenCV-人脸识别
Python-PyTorch
本文档使用 MrDoc 发布
-
+
首页
Python-与佛伦禅
这个是自制Python服务器版,提供了 web 和 api 两种使用方式,由 [简易Python版](/doc/581/) 修改而来 `app.py` ```python from flask import Flask, render_template, request from Crypto.Cipher import AES from random import choice from Crypto.Util.Padding import pad, unpad IV = b'zwzwzwzwzwzwzwzw' TUDOU = [ '滅', '苦', '婆', '娑', '耶', '陀', '跋', '多', '漫', '都', '殿', '悉', '夜', '爍', '帝', '吉', '利', '阿', '無', '南', '那', '怛', '喝', '羯', '勝', '摩', '伽', '謹', '波', '者', '穆', '僧', '室', '藝', '尼', '瑟', '地', '彌', '菩', '提', '蘇', '醯', '盧', '呼', '舍', '佛', '參', '沙', '伊', '隸', '麼', '遮', '闍', '度', '蒙', '孕', '薩', '夷', '迦', '他', '姪', '豆', '特', '逝', '朋', '輸', '楞', '栗', '寫', '數', '曳', '諦', '羅', '曰', '咒', '即', '密', '若', '般', '故', '不', '實', '真', '訶', '切', '一', '除', '能', '等', '是', '上', '明', '大', '神', '知', '三', '藐', '耨', '得', '依', '諸', '世', '槃', '涅', '竟', '究', '想', '夢', '倒', '顛', '離', '遠', '怖', '恐', '有', '礙', '心', '所', '以', '亦', '智', '道', '。', '集', '盡', '死', '老', '至' ] BYTEMARK = ['冥', '奢', '梵', '呐', '俱', '哆', '怯', '諳', '罰', '侄', '缽', '皤'] app = Flask(__name__) def Encrypt(plaintext, key): if not plaintext: return '' # 如果 plaintext 为空,则返回空字符串并跳过程序执行 key = key[:32].ljust(32, b'\x00') data = plaintext.encode('utf-16le') pads = (-len(data)) % 16 data = data + bytes(pads * [pads]) cryptor = AES.new(key, AES.MODE_CBC, IV) result = cryptor.encrypt(data) return '佛曰:' + ''.join([TUDOU[i] if i < 128 else choice(BYTEMARK) + TUDOU[i-128] for i in result]) def Decrypt(ciphertext, key): key = key[:32].ljust(32, b'\x00') if ciphertext.startswith('佛曰:'): ciphertext = ciphertext[3:] if not ciphertext: return '' # 如果 ciphertext 为空,则返回空字符串并跳过程序执行 data = b'' i = 0 while i < len(ciphertext): if ciphertext[i] in BYTEMARK: i = i + 1 data = data + bytes([TUDOU.index(ciphertext[i]) + 128]) else: data = data + bytes([TUDOU.index(ciphertext[i])]) i = i + 1 cryptor = AES.new(key, AES.MODE_CBC, IV) result = cryptor.decrypt(data) flag = result[-1] if flag < 16 and result[-flag] == flag: result = result[:-flag] return result.decode('utf-16le') else: return '' # 密文格式不正确,必须为"佛曰:"开头 @app.route('/', methods=['GET','POST']) def index(): if request.method == 'POST': bear = request.form.get('bear') pw = request.form.get('pw') moeEncode = request.form.get('moeEncode') moeDecode = request.form.get('moeDecode') key = request.form.get('key') if key == "": key == "zwzw" print(request.form) if 'moeEncode' in request.form: pw = Encrypt(bear, key.encode()) print(moeDecode) elif 'moeDecode' in request.form: bear = Decrypt(pw, key.encode()) print(moeEncode) return render_template('index.html', bear=bear, pw=pw) return render_template('index.html') # API请求,提供了GET和POST两种方式,方便适配开发板的请求 @app.route('/api', methods=['GET','POST']) def api(): # 判断请求类型,post用于处理json方式 if request.method == 'POST': # 获取json json_data = request.get_json() # web访问属于get方式,http://localhost:5050/api?pt=你好呀&key=233 这种格式 elif request.method == 'GET': # 获取参数 json_data = request.args print(json_data) pt = json_data.get('pt') # 明文 ct = json_data.get('ct') # 密文 key = json_data.get('key') # key if key == None: key = "zwzw" if not pt == None: ct = Encrypt(pt, key.encode()) print(ct) return ct elif not ct == None: pt = Decrypt(ct, key.encode()) print(pt) return pt else: return "格式不正确", 404 # 判断当前文件是否作为脚本被执行 print("启动服务...") print("Web访问:http://localhost:5050 \n\ API(Get)使用:http://localhost:5050/api?pt=你好呀&key=233 \n\ API(Post)使用:http://localhost:5050,json发送,pt(明文),ct(密文),key(键值)") if __name__ == '__main__': app.run(host='0.0.0.0', port=5050) ``` `templates\index.html`,web主页 ```html <!DOCTYPE html> <html> <head> <!-- <meta name="viewport" content="width=device-width,initial-scale=1.0" /> --> <title>与佛论禅-造物者W</title> <style> body { justify-content: center; } h1 { text-align: center; font-weight: bold; font-size: calc(5vw + 0.5rem); } .container { width: 100%; } .container .control { width: 100%; display: grid; grid-template-columns: 1fr 1fr 1fr; margin-bottom: 10px; margin-top: 10px; } @media (max-width:400px) { .container .control { display: grid; grid-template-columns: 1fr; } } .container .control .moeBtn { margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; } .form-group { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: flex-start; margin-bottom: 10px; border: 2px solid #0089F6; } .form-group textarea { width: 100%; height: calc(50vh - 5vw - 0.5rem - 80px); padding: 5px; font-size: calc(2vw + 0.5rem); } .container .control #key { min-height: 30px; font-size: 25px; } </style> </head> <body> <form action="/" method="post"> <div class="container"> <h1>与佛论禅</h1> <div class="form-group"> <textarea id="bear" name="bear" placeholder="施主,此次前来,不知有何贵干?">{{ bear }}</textarea> </div> <div class="control"> <input name="moeEncode" type="submit" class="moeBtn" value="听佛说世间的奥秘 ↓↓"> <textarea name="key" id="key" style="height: 2vh;" placeholder="键值(可选)">{{ key }}</textarea> <input name="moeDecode" type="submit" class="moeBtn" value="领悟佛所言的真谛 ↑↑"> </div> <div class="form-group"> <textarea id="pw" name="pw" placeholder="佛说世间妙语">{{ pw }}</textarea> </div> </div> </form> </body> </html> ``` `Dockerfile` 部署文件 ```bash # 使用 python 作为底层镜像 FROM python:3.8-slim-buster # 设置工作目录 WORKDIR /app # 安装所需环境 RUN pip install flask pycryptodome -i https://pypi.tuna.tsinghua.edu.cn/simple # 复制当前目录所有文件到工作目录 COPY . . # 设置外部访问端口提示(需和内容一致) EXPOSE 5050 # 容器执行时运行命令 CMD ["python", "app.py"] ``` 构建并部署 ```bash docker build -t dhyana:latest . # 构建镜像 docker run -d --name dhyana -p 5050:5050 dhyana:latest # 部署镜像 # 直接使用构建好的 docker run -d --name dhyana -p 5050:5050 nas.918178.xyz:10088/library/dhyana:latest ```
造物者W
2023年6月1日 11:14
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码