57个挑战之55-制作一个文件编辑器(6)python 实现-客户端+后端
off999 2024-10-19 07:14 29 浏览 0 评论
上题目:
前面积累了那么多,其实现在要写出来已经是水到渠成的事情。
直接贴客户端和服务端代码:
服务端代码:
服务端代码名字:57-55serversidev2.py
import redis
import re
import json
import time
import cgi
from redis import StrictRedis, ConnectionPool
from flask import Flask,jsonify,request
import requests
from hashlib import blake2b
app = Flask(__name__)
def create_url():
print("Come to the function create_url()")
prefix = "http://127.0.0.1/api/url/"
suffix = time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime())
url = prefix + suffix
print(url)
print("Come out of the function create_url()")
return url
def dohash(url):
print("----come to function--- dohash(url)")
FILES_HASH_PERSON = b'57challenges' #设置一个key
h = blake2b(digest_size=10,person=FILES_HASH_PERSON) #设置加密长度及指定key
h.update(url.encode())
primkey = h.hexdigest()
print("the hash of {0} is {1}".format(url,primkey))
print("----come out of function--- dohash(url)")
return primkey
def insert_into_redis(primkey,textcontent):
#mock 把数据插入数据库,primkey 和 textcontent
print("----come to function--- insert_into_redis(primkey,textcontent)")
pool = ConnectionPool(host='localhost', port=6379, db=0, decode_responses=True)
r = StrictRedis(connection_pool=pool)
try:
r.hset("document", primkey, json.dumps({"content": textcontent}))
except:
return 0
print("----come out of function--- insert_into_redis(primkey,textcontent)")
return 1
def check_url_if_exist(url):
# mock检查逻辑
print("----come to function---check_url_if_exist(url)")
print("The received url is {0}".format(url))
key = dohash(url)
print("to search this key {0},check if it exist".format(key))
pool = ConnectionPool(host='localhost', port=6379, db=0, decode_responses=True)
r = StrictRedis(connection_pool=pool)
if r.hexists("document",key):
result = 1
print("it exist")
else:
result = 0
print("it not exist")
print("----come out of function---check_url_if_exist(url)")
return result
def get_text(url):
print("----come to function---get_text(url)")
pool = ConnectionPool(host='localhost', port=6379, db=0, decode_responses=True)
r = StrictRedis(connection_pool=pool)
key = dohash(url)
textinfojson = r.hmget("document",key)
print(textinfojson) #debug , 整个信息内容展示
print(type(textinfojson)) # 看看类型,原来是List
print(textinfojson[0]) # 展示list 中第一个元素内容
print(type(textinfojson[0])) # 看看类型是str
print(json.loads(textinfojson[0])["content"]) #把str 类型转为字典,并读取字典里面key 为"content"的内容
textinfo = json.loads(textinfojson[0])["content"]
print("----come out of function---get_text(url)")
return textinfo
"""
1.保存文档:
功能逻辑:接收前端请求,把文字存到数据库,并返回成功信息到后端。
输入: {“text”: "this is the info for test"}
输出: {“info": "information has been successful saved"}
功能逻辑:
1. 获取输入
2. 把输入的text 文档生成一个url
3. 把URL 做hash ,并把hash(url)作为key
4. 把{hash(url): text} 存入数据库
5. 如果存储成功,则返回信息给到客户端
redis 表结构设计: {md5(url): text}
"""
@app.route('/api/storedoc',methods=['POST'])
def store_doc():
textcontent = request.json['text'] # 获取输入
url = create_url()
primkey = dohash(url)
if insert_into_redis(primkey,textcontent) == 1:
info =" insert into redis key {0} \n {1} pair success".format(url,textcontent)
else:
info = "something error has happened"
return jsonify({"info":info})
"""
2.编辑文档:
功能逻辑: 收集客户端的编辑请求,进入url 并找到对应的数据,把text 数据展示在前端,
输入:{“edit": "http://127.0.0.1/api/202206100906”}
输出:{“textinfo":"this is the info for test”}
供客户端逻辑把这个text 数据做展示。
2-1: 接收输入的URL
2-2: 把URL做hash,并到数据库查找数据
2-3: 如果存在则返回数据,如果不存在则返回信息告诉不存在 result = 0
"""
@app.route('/api/editdoc',methods=['POST'])
def edit_doc():
url = request.json['edit']
print("We have got the input url, it's {0}".format(url))
if check_url_if_exist(url) == 1:
textinfo = get_text(url)
print(" info: the text info is \n {0}".format(textinfo))
return jsonify({"info": "the url is exist","url":url,"textinfo":textinfo})
else:
return jsonify({"info": "the url {0} is not exist".format(url),"url":url,"textinfo":" "})
if __name__ == '__main__':
app.run(host='0.0.0.0',port=8008,debug = True)客户端代码:
客户端代码名字:57-55client.py
from flask import Flask,render_template,request,url_for,redirect
from datetime import date
import requests
def post_url(text):
print("----come to function---post_url(text)")
urlback = "http://127.0.0.1:8008/api/storedoc"
r = requests.post(urlback,json={"text":text})
info = r.json()["info"]
print(info)
print("----come out of the function---post_url(text)")
return info
def edit_url(url):
print("----come to function---edit_url(text)")
urlback = "http://127.0.0.1:8008/api/editdoc"
r = requests.post(urlback,json={"edit":url})
info = r.json()["textinfo"]
print("----come out of the function---edit_url(text)")
#服务端代码需要修改下,增加一个字段回参的: return jsonify({"info": "the url is exist","url":url ,"textinfo": textinfo})
return info
app = Flask(__name__)
"""
1. 保存文档
客户端功能逻辑:接受用户输入,把文字传递到服务端保存,并收集服务端产生的url信息,并展示到客户端。
输入: 在用户主页填入表,并点击保存。
输出: 保存成功,并返回url 在客户端。
1-1. 从前端获取输入;
1-2. 把输入信息提交给后端;
1-3. 后端返回url 给到前端,解析后,展示到display.html 中。
"""
@app.route('/',methods=['GET','POST'])
@app.route('/index')
@app.route('/home')
def index():
if request.method == 'POST':
text = request.form['text']
info = post_url(text)
return render_template('57-55-display.html',info=info)
if request.method == 'GET':
return render_template('57-55-index.html')
"""
2. 打开并编辑保存文档
客户端功能逻辑:用户打开1中对应的链接,打开后即产生一个Post请求到服务端,拿到服务端返回的信息后,重新展现到edit.html中。
逻辑1
输入: 用户点击对应的链接(get请求);
输出: 客户端根据请求做一个redirect 解析到一个新的index2.html中,其中index2里面包含了这个url在服务端的数据;
逻辑2
输入: 用户在index2.html 中做编辑,编辑后点击保存,信息会发送到后端。
输出: 后端能保存这段内容,并产生一个url 信息,
"""
@app.route('/api/url/<content>',methods=['GET'])
def edit(content):
url = request.url
print(url)
print("the content is {0}".format(content))
info = edit_url(url)
print("the info is {0}".format(info))
return render_template('57_55_edit.html', info=info)
@app.route('/api/url/<content2>',methods=['POST'])
def save(content2):
url = request.url
text = request.form['text']
info = post_url(text)
return render_template('57-55-display.html', info=info)
if __name__ == '__main__':
app.run(host='0.0.0.0',port=80,debug = True)客户端的html :57-55index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>57-55index.html</title>
</head>
<body>
<form method = "post">
<label for="text">提供想编辑的文档</label>
<input type="text" name="text" required><br>
<input type="submit" name="submit" value="保存">
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>57-55-display.html</title>
</head>
<body>
{{info}}
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>57-55index.html</title>
</head>
<body>
<form method = "post">
<label for="text">提供想编辑的文档</label> <br>
<textarea name="text" cols="30" rows="5"></textarea> <br>
<input type="submit" name="submit" value="保存">
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>57-55edit.html</title>
</head>
<body>
<form method = "post">
<label for="text">提供想编辑的文档</label> <br>
<textarea name="text" cols="30" rows="5">{{info}}</textarea>
<input type="submit" name="submit" value="保存">
</form>运行效果示意图:
打开页面
3可以看到,产生的url是 http://127.0.0.1/api/url/2022-06-19-09:30:07
分享这个url 给第三方,第三方在界面上打开url,看到老数据,这个时候做一个编辑并保存
在下面增加一行:
保存后产生一个新的url,http://127.0.0.1/api/url/2022-06-19-09:36:08
在后台看这个url
http://127.0.0.1/api/url/2022-06-19-09:36:08
后台数据库查询此url
键入url ,能查到这条数据
相关推荐
- usb系统盘下载(系统u盘之家)
-
手机不可以下载电脑系统到U盘里,这是跟系统文件的格式有直接关系。电脑的系统文件,它在下载安装的时候必须使用电脑版本的U盘才可以正确安装。手机的版本它和电脑的版本差别比较大,即使下载后也不可能正确安装。...
- windows8模拟器(国内版)(win8模拟器安卓版下载)
-
雷电模拟器能在win8系统运行,1、官网下载雷电模拟器,双击安装包进入安装界面。2、点击“自定义安装”修改安装路径,点击“浏览”选择好要安装的路径,默认勾选“已同意”,最后点击“立即安装”。...
- win10安装专业版还是家庭版(win10安装专业版还是家庭版好)
-
从Win10家庭版和专业版对比来看,Win10专业版要比家庭版功能更强大一些,不过价格更贵。另外Win10专业版的一系列Win10增强技术对于普通用户也基本用不到,多了也显得系统不那么精简,因此普通个...
- win10系统保护不见了(win10系统保护打不开怎么办)
-
1、启动计算机,启动到Windows10开机LOGO时就按住电源键强制关机,重复强制关机3次!2、重复步骤3次左右启动后出现“自动修复”界面,我们点击高级选项进入;3、接下来会到选择一个选项界面...
- 新手如何重装win8(怎么重新装系统win8)
-
要想重装回win8.1系统,首先你需要一个win8.1的系统安装盘,然后把你电脑的系统盘格式化一下,或者把你的win10系统删除了,再把win8.1系统安装盘插到电脑上,进行系统安装,等电脑安装系统完...
- 磁盘分区工具软件(硬盘分区工具软件)
-
如果说最安全的那就用电脑自带的吧,右键我的电脑,找到管理,然后进去磁盘管理,然后找到目前的一个磁盘,右键压缩卷,输入压缩空间就是你想要的一个盘的大小(1G=1024MB),然后压缩,然后找到你压缩出来...
- ftp手机客户端(ftp手机客户端存文件)
-
要想实现FTP文件传输,必须在相连的两端都装有支持FTP协议的软件,装在您的电脑上的叫FTP客户端软件,装在另一端服务器上的叫做FTP服务器端软件。 客户端FTP软件使用方法很简单,启动后首先要与...
- 原版xp系统镜像(原版xp系统镜像怎么设置)
-
msdnitellyou又可以上了,那里有。 制作需要的软件 在开始进行制作之前,我们首先需要下载几个软件,启动光盘制作工具:EasyBoot,UltraISO以及用来对制作好的ISO镜像进行测...
- office2007密钥 office2016(office2007ultimate密钥)
-
word2016激活密钥有两种类型:永久激活码和KMS期限激活密钥。其中,永久激活密钥可以使用批量授权版永久激活密钥进行激活,如所示;而KMS期限激活密钥需要使用KMS客户端密钥进行激活,如所示。另外...
- windows10系统启动盘制作(windows10启动盘制作教程)
-
Windows10系统更改启动磁盘的方法如下1、按快捷键Win+R,调出命令窗口2、输入msconfig,点【确定】3、在系统配置中,选择【引导】菜单4、选择要默认启动的磁盘,点【设置为默认值】,...
- 方正电脑怎么重装系统
-
购买一张系统盘,然后启动电脑,将购买的系统盘插入电脑光驱中,等待光驱读取系统盘后,点击安装系统,即可自动安装,等待安装完毕,电脑会自动重启,重新启动后,电脑的系统就安装完毕,可以使用了一、准备需要的软...
-
- qq邮箱怎么写才正确
-
步骤/方式1一般默认的QQ邮箱格式是:QQ号码@qq.com,即QQ账号+@qq.com后缀步骤/方式2若要发送邮件,也要在对方的qq帐号末尾加上@qq.com1.每个人在注册QQ时都会有关联的一个邮箱,它的格式就是“QQ号码@qq.com...
-
2025-12-21 18:51 off999
-
- 电脑怎么看配置信息
-
要查看Windows电脑的配置信息,可以通过按下Win键+R,然后在弹出的运行对话框中输入“dxdiag”并按回车键打开DirectX诊断工具,可以查看有关处理器、内存、显卡等硬件信息。另外,还可以右键点击“此电脑”,选择“属性”来查看...
-
2025-12-21 18:03 off999
- mpeg是什么格式(mpeg是什么格式和mp4的区别)
-
是视频格式,是电脑视频文件的一种,相对其它视频文件格式而言,mpeg格式占的存储空间相对比较小,那么像素也就相对比较低,图像也没有其它格式那么高清,不过一般情况下已经满足正常的使用。好多视频文件都是采...
欢迎 你 发表评论:
- 一周热门
-
-
抖音上好看的小姐姐,Python给你都下载了
-
全网最简单易懂!495页Python漫画教程,高清PDF版免费下载
-
Python 3.14 的 UUIDv6/v7/v8 上新,别再用 uuid4 () 啦!
-
飞牛NAS部署TVGate Docker项目,实现内网一键转发、代理、jx
-
python入门到脱坑 输入与输出—str()函数
-
宝塔面板如何添加免费waf防火墙?(宝塔面板开启https)
-
Python三目运算基础与进阶_python三目运算符判断三个变量
-
(新版)Python 分布式爬虫与 JS 逆向进阶实战吾爱分享
-
失业程序员复习python笔记——条件与循环
-
系统u盘安装(win11系统u盘安装)
-
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (77)
- python封装 (57)
- python写入txt (66)
- python读取文件夹下所有文件 (59)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)
