python asyncore keep track of clients

送分小仙女□ 提交于 2019-12-01 12:21:40

You do know the ID of the client when you receive the connection from the repr(addr). The tuple returned is an IP and a unique number to that client that can be used when you want to send data back to that client. If you wanted to keep a list of clients, you should consider a dictionary that, when receiving an incoming connection, stores that client's information.

If you wanted that information passed on to the handle_read function, your code would look something like below:

class EchoHandler(asyncore.dispatcher_with_send):
    def setAddr(self, addr):
        self.addr = addr

    def handle_read(self):
        data = self.recv(8192)
        print '%s from %s' % (data, self.addr)
        self.send(data)

class EchoServer(asyncore.dispatcher):

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

    def handle_accept(self):
        pair = self.accept()
        if pair is None:
            pass
        else:
            sock, addr = pair
            print 'Incoming connection from %s' % repr(addr)
            handler = EchoHandler(sock)
            handler.setAddr(addr) #Set the address of the sender in the EchoHandler

Simply send the address of the client to EchoHandler, and now you know where it's coming from. Hope this helps/works!

The only "id" that you can get from sockets directly is the reference to the socket and the sender's address. Everything else is based on the data that you receive - your client could e.g. send its id as the first 16 bytes, anything after that would be data. This means effectively implementing a protocol that fulfills your requirements.

An easier and better solution for authentication etc. is to use some library such as ssl in python standard library. The ssl library provides encryption and authentication mechanisms in a form that can easily be used with python sockets. If you have any need for secure authentication, I strongly recommend using existing solutions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!