本文介绍了如何使HTTPServer和BasicHTTPRequestHandler的stderr输出静默/安静?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我的项目的一部分,我正在编写一个简单的http服务器.以下是我的脚本的框架:

I am writing a simple http server as part of my project. Below is a skeleton of my script:

from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler

class MyHanlder(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write('<html><body><p>OK</p></body></html>')

httpd = HTTPServer(('', 8001), MyHanlder)
httpd.serve_forever()

我的问题:每当客户端连接到服务器时,如何抑制脚本生成的stderr日志输出?

My question: how do I suppress the stderr log output my script produces every time a client connects to my server?

我已经查看了HTTPServer类,直到其父类为止,但是无法找到任何标志或函数调用来实现此目的.我还查看了BaseHTTPRequestHandler类,但找不到线索.我相信一定有办法.如果这样做,请与我和其他人分享;感谢您的努力.

I have looked at the HTTPServer class up to its parent, but was unable to find any flag or function call to achieve this. I also looked at the BaseHTTPRequestHandler class, but could not find a clue. I am sure there must be a way. If you do, please share with me and others; I appreciate your effort.

推荐答案

这可能会做到:

from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler

class MyHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write('<html><body><p>OK</p></body></html>')
    def log_message(self, format, *args):
        return

httpd = HTTPServer(('', 8001), MyHandler)
httpd.serve_forever()

这篇关于如何使HTTPServer和BasicHTTPRequestHandler的stderr输出静默/安静?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 12:48