Harvey Harvey - 1 year ago 92
Python Question

Python socket select.select. Using list of objects instead of connections

Typically using

will require a list of connection objects to work like this:

read, write, error = select(self.all_connections, [], [], 0.1)

Say I have the below object:

class remoteDevice(object)

def __init___(self, connection):
self.connection = connection

I will create a list of remoteDevices before using select after accepting the connections and append them to a list:

conn = socket.accept()
newDevice = remoteDevice(conn)

will be alist of multiple devices, and their connection object is given to each remote device.

Is there a way I can pass in
into the select statement to iterate through the
property of each
object? Will I have to store the connection objects seperately just to use

Answer Source

According to the select.select() documentation you can supply a sequence of objects that have a fileno() method. You can easily add that method to your class:

class RemoteDevice(object):
    def __init__(self, connection):
        self.connection = connection

    def fileno(self):
        return self.connection[0].fileno()

The fileno() method simply returns the file descriptor of the connection's socket object. Since you instantiate RemoteDevice with the return value of socket.accept(), this a tuple in which the first item is a socket object.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download