Best way to associate a player character with a server connection?
<p>I'm attempting to write a simple multiplayer type text game.  I currently have a simple chat server right now.  It's working just like it should, echoing the message to all the people connected.</p>

<p>I can't figure out how to take my next step.  I want to associate the client connected with a player object.  The player objects house the available commands someone can type in, so in order to move forward with parsing input I have to be able to do this - I just don't know how.</p>

<p>Currently the player object is just a standard object class with a few properties like 'name', 'id', 'location', that has a list of commands available to them.  If you need a code example of that I can provide one.</p>

<p>Any ideas?</p>

<pre><code>import socket, select
from helpers.colorize import colorize

class Server(object):

    def __init__(self, host, port):
        self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server.bind((host, port))
        self.server.listen(1)
        self.clients = [self.server]

    def start(self):
        while True:
            read_sockets,write_sockets,error_sockets = select.select(self.clients, [], [])

            for sock in read_sockets:

                #new conn
                if sock == self.server:
                    sockfd, addr = self.server.accept()
                    self.clients.append(sockfd)
                    print "Connection from %s %s" % addr

                #looks like a message
                else:
                    #data recieved, lets try and do something
                    try:
                        data = sock.recv(4096)

                        if data: #we have something, parse it
                            self.sendall(sock, data) #give it to everyone for now

                    except: #disconnection, remove from our lists
                        self.sendall(sock, ("%s %s disconnected" % addr))
                        print "%s %s disconnected." % addr
                        socket.close()
                        self.clients.remove(sock)
                        continue

        self.server.close()

    def send(self, sock, message):
        sock.send(colorize(message))

    def sendall(self, sock, message):
        #Do not send the message to master socket and the client who has send us the message
        for socket in self.clients:
            if socket != self.server:
                try :
                    socket.send(message)
                except :
                    # broken socket connection may be, chat client pressed ctrl+c for example
                    socket.close()
                    CONNECTION_LIST.remove(socket)


if __name__ == "__main__":
    s = Server('localhost', 2222)

    s.start()
</code></pre>

<p>You can use a dict to map <a href="https://docs.python.org/2/library/socket.html#socket.socket.fileno" rel="nofollow">fds of client sockets</a> to client objects.</p>

<p>When you receive some new messages, you can fetch the corresponding object from the dict and invoke methods on it.</p>

python python-2.7