为什么在asynchat的服务器上创建一个没有引用的实例就足够了?

2024-05-26 07:45:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在阅读关于如何使用模块asynchat的帖子(http://pymotw.com/2/asynchat/#module-asynchat)。这是服务器的代码

import asyncore
import logging
import socket

from asynchat_echo_handler import EchoHandler

class EchoServer(asyncore.dispatcher):
    """Receives connections and establishes handlers for each client.
    """

    def __init__(self, address):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.bind(address)
        self.address = self.socket.getsockname()
        self.listen(1)
        return

    def handle_accept(self):
        # Called when a client connects to our socket
        client_info = self.accept()
        EchoHandler(sock=client_info[0])
        # We only want to deal with one client at a time,
        # so close as soon as we set up the handler.
        # Normally you would not do this and the server
        # would run forever or until it received instructions
        # to stop.
        self.handle_close()
        return

    def handle_close(self):
        self.close()

为什么“EchoHandler(sock=client_info[0])”就足够了?创建的对象没有名称。如何调用EchoHandler对象中定义的方法


Tags: toimportselfinfoclientcloseaddressdef
1条回答
网友
1楼 · 发布于 2024-05-26 07:45:56

“EchoHandler(sock=client_info[0])”就足够了,因为在本例中不需要对其进行方法调用。您只需将处理程序连接到客户机,然后忘记它。它将应答来自客户端本身的呼叫,并在客户端关闭时关闭。 当你以后想打电话的时候,就照杰瓦兹写的那样做

相关问题 更多 >