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())
相关推荐
沃洛德.辛肯32 分钟前
PyTorch 的 F.scaled_dot_product_attention 返回Nan
人工智能·pytorch·python
noravinsc44 分钟前
人大金仓数据库 与django结合
数据库·python·django
豌豆花下猫1 小时前
Python 潮流周刊#102:微软裁员 Faster CPython 团队(摘要)
后端·python·ai
yzx9910131 小时前
Gensim 是一个专为 Python 设计的开源库
开发语言·python·开源
麻雀无能为力2 小时前
python自学笔记2 数据类型
开发语言·笔记·python
Ndmzi2 小时前
matlab与python问题解析
python·matlab
懒大王爱吃狼2 小时前
怎么使用python进行PostgreSQL 数据库连接?
数据库·python·postgresql
猫猫村晨总2 小时前
网络爬虫学习之httpx的使用
爬虫·python·httpx
web150854159352 小时前
Python线性回归:从理论到实践的完整指南
python·机器学习·线性回归
ayiya_Oese2 小时前
[训练和优化] 3. 模型优化
人工智能·python·深度学习·神经网络·机器学习