[英]How to Log output of Local Multithread HTTP Server python
我有简单的 http python 多线程服务器
#http_server_threads.py
from http.server import HTTPServer, BaseHTTPRequestHandler
from socketserver import ThreadingMixIn
import threading
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-Type',
'text/plain; charset=utf-8')
self.end_headers()
message = threading.currentThread().getName()
self.wfile.write(message.encode('utf-8'))
self.wfile.write(b'\n')
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
if __name__ == '__main__':
server = ThreadedHTTPServer(('localhost', 8080), Handler)
print('Starting server, use <Ctrl-C> to stop')
server.serve_forever()
如果我卷曲
(base) padmanabanpr@padmanaban ~ % curl localhost:8080
Thread-1
(base) padmanabanpr@padmanaban ~ % curl localhost:8080
Thread-2
(base) padmanabanpr@padmanaban ~ % curl localhost:8080
Thread-3
在运行这个 python 代码时,我得到了
% python3 http_server_threads.py
Starting server, use <Ctrl-C> to stop
127.0.0.1 - - [30/Jul/2021 10:13:54] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [30/Jul/2021 10:13:59] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [30/Jul/2021 10:14:01] "GET / HTTP/1.1" 200 -
我需要将此输出记录到文件而不是显示,
需要记录的输出: (ip - - datetime“请求类型”响应代码 - )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.