如何避免http.server的控制台日志记录

时间:2019-05-20 20:40:06

标签: python python-3.x

我在代码中运行着version: 0.2 env: variables: AWS_ACCOUNT_ID: "<account_id>" AWS_DEFAULT_REGION: "<aws_region>" IMAGE_REPO_NAME: "my-repo" IMAGE_TAG: "my-tag" CONNECTION_STRING: "Server=mssql,1433;Database=My.Database;User Id=sa;Password=SecurePassword!" DB_PASSWORD: "SecurePassword!" phases: install: runtime-versions: docker: 18 pre_build: commands: - echo Logging into Amazon ECR... - $(aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION) - echo Spinning up database for integration tests... - docker run --name mssql -e ACCEPT_EULA=Y -e SA_PASSWORD="$DB_PASSWORD" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2017-latest build: commands: - echo Building the Docker images - docker build --build-arg ENVIRONMENT=Development --build-arg CONNECTION_STRING="$CONNECTION_STRING" -t $IMAGE_REPO_NAME:$IMAGE_TAG --target final . - docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazon.aws.com/$IMAGE_REPO_NAME:$IMAGE_TAG post_build: commands: - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG 来加载本地html,但是我不希望服务器每次发出请求时都登录控制台。

我调查了这篇文章: How to silent/quiet HTTPServer and BasicHTTPRequestHandler's stderr output?

尝试了一下,但返回错误:http.server

missing 3 required positional arguments

我希望它能正常工作,但不会在控制台中登录class main() def openHttpServer(self): port = 8000 Handler = http.server.SimpleHTTPRequestHandler self.httpd = socketserver.TCPServer(("", port), Handler) self.httpd.serve_forever()

1 个答案:

答案 0 :(得分:1)

我按照几篇文章中的建议通过子类对其进行了修复

因此,如果我将http.server.SimpleHTTPRequestHandler用作Handler,则现在将其作为子类传递给这里:

class quietServer(http.server.SimpleHTTPRequestHandler):

只需用log_messagepass编写函数,这样它就不会返回登录请求。

def log_message(self, format, *args):
        pass

整个代码如下:

import http.server
import socketserver

PORT = 8080

class quietServer(http.server.SimpleHTTPRequestHandler):
    def log_message(self, format, *args):
        pass

with socketserver.TCPServer(("", PORT), quietServer) as httpd:
    httpd.serve_forever()