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())
相关推荐
xwill*3 小时前
分词器(Tokenizer)-sentencepiece(把训练语料中的字符自动组合成一个最优的子词(subword)集合。)
开发语言·pytorch·python
咖啡の猫4 小时前
Python列表的查询操作
开发语言·python
Chiandra_Leong4 小时前
Python-Pandas、Numpy
python·pandas
BoBoZz194 小时前
ParametricObjectsDemo多种参数曲面展示及面上部分点法线展示
python·vtk·图形渲染·图形处理
quikai19814 小时前
python练习第三组
开发语言·python
ULTRA??5 小时前
初学protobuf,C++应用例子(AI辅助)
c++·python
CHANG_THE_WORLD5 小时前
Python 字符串全面解析
开发语言·python
甄心爱学习6 小时前
CSP认证 备考(python)
数据结构·python·算法·动态规划
databook7 小时前
数据会说谎?三大推断方法帮你“审问”数据真相
后端·python·数据分析
是Dream呀7 小时前
Python圣诞特辑:打造一棵会唱歌、会下雪的魔法圣诞树
开发语言·python·pygame