[Twisted-Python] Socket number

Brendon Colby brendoncolby at gmail.com
Sun Jun 10 13:10:50 EDT 2007

On 6/10/07, Christopher Armstrong <radix at twistedmatrix.com> wrote:
> If you only need this identifier for use in the implementation of the
> server, a much better way to identify players is by their Protocol
> object. On the other hand, if you do need to pass this identifier over
> the wire, then I agree with Jean-Paul that a small incremental or
> random number would be good.

This is what I do:

usersByID = {} # userID:user object
idRange = range(1,1001) # Arbitrary limit...

def getNextUserID():
    return list(set(idRange) - set(usersByID))[0]

When a user connects, I get the next available ID. If usersByID looks like:

{1:User1, 2:User2, 5:User5}

The next ID returned will be 3. I do this so I can keep reusing my IDs
instead of infinitely incrementing them.

More information about the Twisted-Python mailing list