线程UDP服务器错误

时间:2014-09-04 11:25:32

标签: python multithreading sockets udp

我已经以某种方式破坏了这段代码,我无法修复它。服务器/客户端代码是由其他人编写的(主要来自py手册中的示例),我无法解决问题。

我遇到了super和init以及爵士乐的问题,主要是因为我不完全理解并找到关于这个主题的大部分文档让我比起初时更加困惑。就目前而言,我会很开心,让它发挥作用。它可能是一个愚蠢的问题,固定在一行。

有什么想法吗?我已经尝试不粘贴不相关的代码,但如果有帮助,我可以添加更多或提供整个文件。在创建句柄线程时,代码特别落空。我的测试用例是运行代码实例,并在它们之间传递消息,并在收到第一条UDP消息时失败。

# Library imports
import threading
import SocketServer
import multiprocessing

# .. More code here ...

class ThreadedUDPServer(SocketServer.ThreadingMixIn, SocketServer.UDPServer):
    pass


class NodeDaemon(ThreadedUDPServer):
    def __init__(self, host, port, modules):
        ThreadedUDPServer.__init__(self, (host, port), NodeProtocolHandler)

        # Store parameters in the class
        self.modules    = modules
        self.port       = port
        self.ip         = host

        # Check if we have enabled multithreaded listen daemon
        if settings.MULTI:
            self.server_thread = multiprocessing.Process(target=self.serve_forever)
        else:
            self.server_thread = threading.Thread(target=self.serve_forever)

        # Make the server thread daemonic
        self.server_thread.daemon = True

        # Start the server thread
        self.server_thread.start()

        # Update our identity node info
        self.modules.identity = NodeInfo(host, port)

    def fetch_modules(self):
        return self.modules


class NodeProtocolHandler(SocketServer.BaseRequestHandler):
    """
        Handles nody things.
        Check https://docs.python.org/2/library/socketserver.html
        For more sweet deets.
    """
    def __init__(self,*args,**kwargs):
        super(SocketServer.BaseRequestHandler,self).__init__(args,kwargs)

        # Grab modules references
        self.modules = self.server.fetch_modules()


        # ... More code here .. #

    def handle(self):
        """
            Main routine to handle all incoming UDP packets.
        """
        # Grab the raw message data received
        raw_data = self.request[0].strip()

        # ... More code here ...

产生的错误是:

Exception happened during processing of request from ('127.0.0.1', 60377)
----------------------------------------
Traceback (most recent call last):
  File "C:\Python27\lib\SocketServer.py", line 593, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Python27\lib\SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\some_dir\node_daemon.py", line 60, in __init__
    super(SocketServer.BaseRequestHandler,self).__init__(args,kwargs)
TypeError: must be type, not classobj

1 个答案:

答案 0 :(得分:-1)

def __init__(self,*args,**kwargs):
-        super(SocketServer.BaseRequestHandler,self).__init__(args, kwargs)
+        SocketServer.BaseRequestHandler.__init__(self,*args, **kwargs)