Python: logging and TCP handler

时间:2016-10-19 13:40:23

标签: python python-2.7 logging tcp

I wrote my TCP handler as follows (adapted from: https://docs.python.org/2/library/socketserver.html#socketserver-tcpserver-example):

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import SocketServer

from MyModule import myFunction

class MyHandler(SocketServer.StreamRequestHandler):

    def handle(self):
        self.data = self.rfile.readline().strip()
        result = myFunction(self.data)
        self.wfile.write(result)

if __name__ == "__main__":
    HOST, PORT = myhost, myport
    server = SocketServer.TCPServer((HOST, PORT), MyHandler)
    server.serve_forever()

It works perfectly and now I'm trying to add a logger:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import SocketServer
import logging
from logging.handlers import TimedRotatingFileHandler

from MyModule import myFunction

class MyHandler(SocketServer.StreamRequestHandler):

    def __init__(self):
        self.logger = logging.getLogger()
        self.logger.setLevel(logging.DEBUG)
        self.formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s')
        self.file_handler = TimedRotatingFileHandler('my_log_file.log', when='D', interval=1, utc=True)
        self.file_handler.setLevel(logging.DEBUG)
        self.file_handler.setFormatter(self.formatter)
        self.logger.addHandler(self.file_handler)

    def handle(self):
        self.data = self.rfile.readline().strip()
        result = myFunction(self.data)
        self.wfile.write(result)

        self.logger.info(result)

if __name__ == "__main__":
    HOST, PORT = myhost, myport
    server = SocketServer.TCPServer((HOST, PORT), MyHandler)
    server.serve_forever()

When I run it I get the following error:

TypeError: __init__() takes exactly 1 argument (4 given)

I don't understand what the 4 arguments given are. Is there anything wrong with the code other than that?

EDIT: Full TraceBack:

Exception happened during processing of request from ('MyIP', 54028)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 290, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 318, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 331, in finish_request
    self.RequestHandlerClass(request, client_address, self)
TypeError: __init__() takes exactly 1 argument (4 given)

1 个答案:

答案 0 :(得分:2)

MyHandlerSocketServer.StreamRequestHandler的子类,它是BaseRequestHandler的子类。 call signature of BaseRequestHandler.__init__

def __init__(self, request, client_address, server):

回溯错误消息显示在BaseServer.finish_request method

self.RequestHandlerClass(request, client_address, self)

被调用。 self.RequestHandlerClassMyHandler。因此, MyHandler.__init__应该有来电签名

class MyHandler(SocketServer.StreamRequestHandler):
    def __init__(self, request, client_address, server):

而不是

class MyHandler(SocketServer.StreamRequestHandler):
    def __init__(self):

调用self.RequestHandlerClass(request, client_address, self)时,Python调用 以RequestHandlerClass作为第一个参数的self方法。换一种说法, RequestHandlerClass(self, request, client_address, self)被调用。 self, request, client_address, self是传递给MyHandler的四个参数。 错误消息

TypeError: __init__() takes exactly 1 argument (4 given)

抱怨MyHandler.__init__被定义为只期望1个参数,但它被传递了4个参数。