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())
相关推荐
Wonderful U4 分钟前
基于Python+Django的智能在线考试系统:从题库管理到自动阅卷的全流程实战
开发语言·python·django
SilentSamsara8 分钟前
Python 服务的 K8s 部署:HPA/ConfigMap/Secret 完整配置
开发语言·python·青少年编程·容器·kubernetes
前端与小赵8 分钟前
数据库交互全链路实战:通用封装、批量优化与动态查询三大核心模块
数据库·python·sql
盼小辉丶9 分钟前
PyTorch强化学习实战(11)——N步DQN(N-step DQN)
pytorch·python·深度学习·强化学习
godspeed_lucip11 分钟前
LLM和Agent——专题6:Multi Agent 入门(1)
人工智能·python
郝学胜-神的一滴30 分钟前
系统设计 014:缓存深度实战:如何用 Cache 优雅优化数据库读写?
java·数据库·python·缓存·oracle·php·软件构建
Cloud_Shy61834 分钟前
解读《Effective Python 3rd Edition》:从练气到老魔(第三章 Item 17 - 20)
开发语言·笔记·python
ZHW_AI课题组1 小时前
使用Stable Diffusion v1.5文本引导与无分类器引导(CFG)算法实现条件生成图片
人工智能·python·算法·机器学习·stable diffusion
盼小辉丶1 小时前
OpenCV-Python实战(25)——基于深度传感器与凸性分析打造实时手势识别系统
人工智能·python·opencv·计算机视觉
金融大 k1 小时前
行情数据接入 MCP:Claude Code / Cursor 工具描述怎么写才不踩坑
人工智能·python·websocket·行情 api