客户端中的Python asyncore.loop

时间:2017-06-09 09:39:40

标签: python sockets asyncore

我搜索过很多文章,但仍然没有重要信息。

我的问题是如何在客户端停止asyncore.loop()。

我的代码如下 import socket,asyncore

class Client(asyncore.dispatcher):
    def __init__(self, host, port):
        self.buffer = ''
        asyncore.dispatcher.__init__(self)

        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect((host, port))

    def writable(self):
        return len(self.buffer) > 0

    def handle_connect(self):
        print "handle_connect"

    def handle_close(self):
        print "handle_close"
        self.close()

    def handle_write(self):
        print "handle_write"
        sent = self.send(self.buffer)
        self.buffer = self.buffer[sent:]
        print sent

    def command(self, data):
        self.buffer = data

    def close(self):
        self.close()


mysocket = Client("127.0.0.1", 8888)
mysocket.command("asfas\n")

asyncore.loop()

mysocket.close()

提前致谢!

0 个答案:

没有答案