python http服务屏蔽响应日志输出,终端不输出时间日志

一、http_server总是在收到消息时打印返回的日志时间戳

通过http.server启动服务器时,每次访问服务器时都会写入ip-address和请求时间。是否可以在控制台中禁用这些日志的显示?

二、解决方案

官方文档中指明了 log_message 默认输出是终端-sys.stderr。如果不想要终端打印日志信息,需要重新此函数。
详情见官方文档

python 复制代码
class Resquest(BaseHTTPRequestHandler):
    server_version = "Apache"  # 设置服务器返回的的响应头

    def log_message(self, format: str, *args: Any) -> None:
        return
        return super().log_message(format, *args)
    
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "application/json")
        self.send_header("test", "This is get!")
        self.end_headers()
        self.wfile.write(json.dumps(data).encode())

    def do_POST(self):
        # path = self.path
        # print(path)
        # 获取post提交的数据
        datas = self.rfile.read(int(self.headers['Content-Length']))
            
        print(result)
            
        self.send_response(200)
        self.send_header("Content-type", "application/json")
        self.send_header("test", "This is post!")
        self.end_headers()
        self.wfile.write(json.dumps(data).encode())
相关推荐
CODER030412 分钟前
win11系统编译安装cuda版llama-cpp-python(踩完所有的坑)
开发语言·python·llama
严谨的麻辣烫19 分钟前
批量静态 IP 如何管理?用 Python 建立一个简单的 IP 资源监控方案
运维·服务器·网络·python·tcp/ip
卷无止境24 分钟前
FastAPI生产环境密钥管理全解析,从一个.env文件说起
后端·python·fastapi
卷无止境29 分钟前
SigV4与HTTPS,两套完全不同维度的安全机制
后端·python·fastapi
qq_4260039631 分钟前
多语言新增语种全量测试策略的测试范围
前端·javascript·python·自动化
SamChan9034 分钟前
Python+ReportLab自动生成PDF翻译质量审计报告:从数据到可视化的完整方案
开发语言·python·ai·pdf·wpf
whcyhhh35 分钟前
头歌实践教学平台:数据科学与大数据技术导论(十八2)
大数据·开发语言·python
一晌小贪欢37 分钟前
Python办公18:PDF 转 Word——利用 OCR 技术批量提取不可编辑的文档内容
开发语言·python·pdf·word·excel·数据可视化·python办公
wtGEOyh1 小时前
2026:劲豆如何用科技“种”出大豆芯?
python·科技
SamChan901 小时前
用Prometheus+Grafana搭建PDF翻译服务监控看板:指标采集与告警实战
python·ai·pdf·grafana·prometheus·机器翻译