Non blocking sockets with select.poll() ?
Maxim Veksler
hq4ever at gmail.com
Fri May 4 07:57:21 EDT 2007
On 5/4/07, Jean-Paul Calderone <exarkun at divmod.com> wrote:
> On Fri, 4 May 2007 13:04:41 +0300, Maxim Veksler <hq4ever at gmail.com> wrote:
> >Hi,
> >
> >I'm trying to write a non blocking socket port listener based on
> >poll() because select is limited to 1024 fd.
> >
> >Here is the code, it never gets to "I did not block" until I do a
> >telnet connection to port 10000.
> >
>
> What were you expecting?
>
I'll try to explain.
I'm doing a little experiment: Capturing the whole tcp 1-65535 range
of the machine, allowing me to connect to the my service on the
machine on every port. I know that it's probably the most dumb thing
to do with TCP/IP communication please don't forget it's an
experiment.
My first attempt was made with select.select please see here
http://article.gmane.org/gmane.comp.python.general/516155/ and the
next attempt for slice-by 1024 which also didn't work, here:
http://article.gmane.org/gmane.comp.python.general/517036/
Now, after I realized I won't be able to implement this using
select.select I turned to select.poll(). My problem now it that I'm
able to register more then 1024 socket fd but now I can't replicate
the behaviour select gave me. When I used select for <1024 sockets, I
was able to telnet to each of the 1024 ports and select.select would
return the proper object to do accept() on it, shortly speaking: no
exception was thrown, you can see the working code in the second link
above. The situation I have now with the code attached below is that
if I try querying one of the registered ports I get the following :
TERM1:
"""
./listener_sockets_range_poll.py
.
.
Asking 10182
Asking 10183
Asking 10184
Asking 10185
Asking 10186
Found 10186
Traceback (most recent call last):
File "./listener_sockets_range_poll.py", line 35, in <module>
conn, addr = nb_active_socket.accept()
File "/usr/lib/python2.5/socket.py", line 167, in accept
sock, addr = self._sock.accept()
socket.error: (11, 'Resource temporarily unavailable')
"""
TERM2:
"""
telnet localhost 10100
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
"""
and the code running is this:
"""
#!/usr/bin/env python
import socket
import select
class PollingSocket(socket.socket):
__poll = select.poll()
def __init__(self, port_number):
self.tcp_port_number = port_number
socket.socket.__init__(self, socket.AF_INET, socket.SOCK_STREAM)
self.setblocking(0)
self.bind(('0.0.0.0', self.tcp_port_number))
self.listen(5)
self.__poll.register(self)
def poll(self, timeout = 0):
return self.__poll.poll(timeout)
def debugPollingSocket(port_num):
print "BIND TO PORT: ", port_num
return PollingSocket(port_num)
all_sockets = map(debugPollingSocket, xrange(10000, 19169))
print "We have this in stock:"
for nb_active_socket in all_sockets:
print nb_active_socket.tcp_port_number
while 1:
for nb_active_socket in all_sockets:
print "Asking", nb_active_socket.tcp_port_number
if nb_active_socket.poll(1):
print "Found", nb_active_socket.tcp_port_number
conn, addr = nb_active_socket.accept()
while 1:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()
"""
> Jean-Paul
Thank you,
Maxim.
--
Cheers,
Maxim Veksler
"Free as in Freedom" - Do u GNU ?
More information about the Python-list
mailing list