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())
相关推荐
Ronin-Lotus1 小时前
深度学习篇---Opencv中的机器学习和深度学习
python·深度学习·opencv·机器学习
信阳农夫1 小时前
Django解析跨域问题
后端·python·django
m0_371356152 小时前
【测试语言基础篇】Python基础之List列表
开发语言·python·list
大0马浓2 小时前
训练大模型LLM选择哪种开发语言最好
人工智能·python·训练
风筝超冷3 小时前
AttributeError: module ‘backend_interagg‘ has no attribute ‘FigureCanvas‘
python
onejason3 小时前
如何利用爬虫获取腾讯新闻详情数据:实战指南
前端·python
yicode3 小时前
Python基础:列表与元组详解
后端·python
梦丶晓羽3 小时前
自然语言处理:无监督朴素贝叶斯模型
人工智能·python·自然语言处理·tf-idf·贝叶斯定理·词袋模型·无监督朴素贝叶斯模型
Y雨何时停T4 小时前
使用 Python 批量提取 PDF 书签:一款实用工具的实现
python·pdf