Python socket select.select. Using list of objects instead of connections
<p>Typically using <code>select.select()</code> will require a list of connection objects to work like this:</p>

<p><code>read, write, error = select(self.all_connections, [], [], 0.1)</code></p>

<p>Say I have the below object:</p>

<pre><code>class remoteDevice(object)

    def __init___(self, connection):
        self.connection = connection
</code></pre>

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

<pre><code>conn  = socket.accept()
newDevice = remoteDevice(conn)
all_devices.append(newDevice)
</code></pre>

<p>Now <code>all_devices</code> will be  alist of multiple devices, and their connection object is given to each remote device.</p>

<p>Is there a way I can pass in <code>all_devices</code> into the select statement to iterate through the <code>connection</code> property of each <code>remoteDevice</code> object? Will I have to store the connection objects seperately just to use <code>select.select()</code>?</p>

<p>According to the <a href="https://docs.python.org/3/library/select.html#select.select" rel="nofollow"><code>select.select()</code></a> documentation you can supply a sequence of objects that have a <code>fileno()</code> method. You can easily add that method to your class:</p>

<pre><code>class RemoteDevice(object):
    def __init__(self, connection):
        self.connection = connection

    def fileno(self):
        return self.connection[0].fileno()
</code></pre>

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

python sockets python-3.x