python-使用bottle时间简易服务器

发布于:2024-04-18 ⋅ 阅读:(17) ⋅ 点赞:(0)

背景

202310.txt内容是一段json字符串,目的是通过浏览器get方法访问,获取查询的结果。总的来将python实现较java简单,特别是json的字段解析超级方便

调试

读取文本所有内容

f=open("./howtousercbow/data/other/yuedu/202310.txt",encoding="utf-8")
jsonstr=f.read()
f.close()

字段解释

qusType": “3”,判断题

“paperQusAns”: “正确”, 我得回答

“paperQusScore”: 1, 回答状态正确 0错误

“qusType”: “1”, 单选题

“qusType”: “2”,多选择题

json字符串解析

import json
data=json.loads(jsonstr)
histo=[]
for item in data["data"]["paperQusList"]:
    histo.append("{}@@@@{}@@@@{}".format(item["qusContent"],item["paperQusScore"],item["paperQusAns"]))
    

追加写入文件

with open("./howtousercbow/data/other/yuedu/sum.txt","a",encoding="utf-8") as f:
    for item in histo:
        f.write(item)
        f.write("\n")
        

整理后

整理后写入文件方法

# 单独或者新增使用
import json
def writeFileContentToSum(filepath):
    f=open(filepath,encoding="utf-8")
    jsonstr=f.read()
    f.close()
    
    data=json.loads(jsonstr)
    histo=[]
    for item in data["data"]["paperQusList"]:
        histo.append("{}@@@@{}@@@@{}@@@@{}".format(item["qusContent"],item["paperQusScore"],item["paperQusAns"],item["qusType"]))
        
    with open("./howtousercbow/data/other/yuedu/sum.txt","a",encoding="utf-8") as ff:
        for item in histo:
            ff.write(item)
            ff.write("\n")

将目录下所有文本的内容批量追加到一个文本

# 批量使用
import os
lists=os.listdir("./howtousercbow/data/other/yuedu/")
lists
for item in lists:
    if item!="sum.txt":
        writeFileContentToSum("./howtousercbow/data/other/yuedu/{}".format(item))
        print(f"写入{item}")

写入202310.txt
写入202311.json
写入202312.json
写入202401-1.json
写入202401-2.json
写入202402.json
写入202403.json

搜索字符串方法

# 第2种方式)在想终止的cell,按ESC键,让其脱离编辑状态,在命令状态中,连续按两次“I”键(interrupt的首字母)(简单:推荐
# bottle https://baijiahao.baidu.com/s?id=1741557564519996709&wfr=spider&for=pc

def getType(str):
    if str=="3":
        return "判断题"
    if str=="2":
        return "多选题"
    if str=="3":
        return "单项题"    
    
def parseSearch(searchstr):
    with open("./howtousercbow/data/other/yuedu/sum.txt","r",encoding="utf-8")as f:
        lines=f.readlines()
        
        res=""
        for line in lines:
            spp=line.split("@@@@")
            qusContent=spp[0]
            paperQusScore="这道题回答错误" if spp[1]=="0" else "【这道题回答正确】"
            paperQusAns=spp[2]
            qusType=getType(spp[3])
            if searchstr in qusContent:
                print(f"{qusContent}  {paperQusScore}   我得回答是:{paperQusAns}")   
                res=res+qusContent+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+paperQusScore+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+"我得回答是: "+paperQusAns+"<br><br>"
        return res
# while True:
#     user_input = input("请输入内容: ")
#     parseSearch(user_input)

实现简易服务器通过浏览器访问

from bottle import route, run,get
 
@route('/')
def index():
    return "你说说看先!"

@route('/:content')ii
def back(content):
    return '''回答记录如下:<br><br>%s '''% (parseSearch(content))
    
run(port=8001)
Bottle v0.12.25 server starting up (using WSGIRefServer())...
Listening on http://127.0.0.1:8001/
Hit Ctrl-C to quit.

在这里插入图片描述