The code so far is fine, as far as it goes (except that a backlog of 1 seems unduly strict), the problem of course comes when you try to accept
a connection on either listening socket, since accept
is normally a blocking call (and "polling" by trying to accept with short timeouts on either socket alternately will burn machine cycles to no good purpose).
select to the rescue!-) select.select
(or on the better OSs select.poll
or even select.epoll
or select.kqueue
... but, good old select.select
works everywhere!-) will let you know which socket is ready and when, so you can accept
appropriately. Along these lines, asyncore
and asynchat
provide a bit more organization (and third-party framework twisted
, of course, adds a lot of such "asynchronous" functionality).
Alternatively, you can devote separate threads to servicing the two listening sockets, but in this case, if the different sockets' functionality needs to affect the same shared data structures, coordination (locking &c) may become ticklish. I would certainly recommend trying the async approach first -- it's actually simpler, as well as offering potential for substantially better performance!-)