python asynchat:如何存储有关各个连接的信息,以及如何知道客户端何时断开连接

发布于 2024-11-25 19:53:44 字数 1442 浏览 1 评论 0原文

为了好玩,我正在编写一个带有异步聊天的最小 IRC 服务器。我正在尝试澄清一些基础知识(我的具体问题遵循代码)。我决定不在 Twisted 中使用任何东西,这样我就可以自己实现更多的功能。首先,我的代码:

import asyncore,asynchat
import socket

class Connection(asynchat.async_chat):
    def __init__(self, server, sock, addr):
        asynchat.async_chat.__init__(self, sock)
        self.set_terminator('\n')
        self.data = ""
        print "client connecting:",addr
        # do some IRC protocol initialization stuff here

    def collect_incoming_data(self, data):
        self.data = self.data + data

    def found_terminator(self):
        print self.data
        self.data = ''

class Server(asyncore.dispatcher):
    def __init__(self, host, port):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.bind((host, port))
        self.listen(5)

    def handle_accept(self):
        conn, addr = self.accept()
        Connection(self, conn, addr)

    def handle_close(self):
        self.close()

s = Server('127.0.0.1',5006)
asyncore.loop()

所以,在我看来,这个代码结构类似于 Twisted 客户端工厂:Server 类初始化一次,基本上每次都会实例化 Connection客户端连接。第一个问题:通过将所有连接存储在 Server 内的列表中来跟踪所有连接的客户端的最佳方法是什么?

另外,我不明白如何知道特定客户端何时关闭与我的套接字的连接? Connection 实现了 asynchat(并通过扩展 asyncore),但向 Connection 类添加 handle_close() 回调在客户端断开连接时不会触发。它似乎仅适用于服务器上绑定的套接字被破坏时。我没有看到任何用于此目的的方法。无论客户端是否连接,该套接字始终保持打开状态,对吧?

For fun, I'm writing a minimal IRC server with asynchat. I'm trying to clear up a few fundamentals (my specific questions follow the code). I've decided not to use anything in Twisted just so I can implement a little more myself. First, the code I have:

import asyncore,asynchat
import socket

class Connection(asynchat.async_chat):
    def __init__(self, server, sock, addr):
        asynchat.async_chat.__init__(self, sock)
        self.set_terminator('\n')
        self.data = ""
        print "client connecting:",addr
        # do some IRC protocol initialization stuff here

    def collect_incoming_data(self, data):
        self.data = self.data + data

    def found_terminator(self):
        print self.data
        self.data = ''

class Server(asyncore.dispatcher):
    def __init__(self, host, port):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.bind((host, port))
        self.listen(5)

    def handle_accept(self):
        conn, addr = self.accept()
        Connection(self, conn, addr)

    def handle_close(self):
        self.close()

s = Server('127.0.0.1',5006)
asyncore.loop()

So, in my mind, this code structure is similar to a Twisted client factory: the Server class is initialized once and basically instantiates Connection every time a client connects. First question: is the best way to keep track of all connected clients by storing all of the Connections in a list within Server?

Also, I don't understand how I am to know when a specific client closes their connection to my socket? Connection implements asynchat (and by extension asyncore) but adding the handle_close() callback to the Connection class doesn't fire when a client disconnects. It seems to be only for when the bound socket on the server is destroyed. I don't see any methods for this purpose. This socket always stays open, whether or not clients connect, right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠北森屋 2024-12-02 19:53:44

要处理客户端关闭的连接,请检查handle_error方法,您的客户端是否发出干净的关闭连接?
handle_error() :在引发异常且未进行其他处理时调用。默认版本打印压缩的回溯。

希望有帮助。

to handle client side closed connections check the handle_error method, does your client issue a clean close connection?
handle_error() :Called when an exception is raised and not otherwise handled. The default version prints a condensed traceback.

hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文