Python asyncore.loop in client

983 Views Asked by At

I've googled many articles, but still without significant information.

My question is about how to stop asyncore.loop() in client side.

My code is the following 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()

Thanks in advance!

0

There are 0 best solutions below