Error: child process close a socket inherited from parent
narke
narkewoody at gmail.com
Sun May 29 09:52:45 EDT 2011
On 2011-05-29, narke <narkewoody at gmail.com> wrote:
> Hi,
>
> As illustrated in the following simple sample:
>
> import sys
> import os
> import socket
>
> class Server:
> def __init__(self):
> self._listen_sock = None
>
> def _talk_to_client(self, conn, addr):
> text = 'The brown fox jumps over the lazy dog.\n'
> while True:
> conn.send(text)
> data = conn.recv(1024)
> if not data:
> break
> conn.close()
>
> def listen(self, port):
> self._listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> self._listen_sock.bind(('', port))
> self._listen_sock.listen(128)
> self._wait_conn()
>
> def _wait_conn(self):
> while True:
> conn, addr = self._listen_sock.accept()
> if os.fork() == 0:
> self._listen_sock.close() # line x
> self._talk_to_client(conn, addr)
> else:
> conn.close()
>
> if __name__ == '__main__':
> Server().listen(int(sys.argv[1]))
>
> Unless I comment out the line x, I will get a 'Bad file descriptor'
> error when my tcp client program (e.g, telnet) closes the connection to
> the server. But as I understood, a child process can close a unused
> socket (file descriptor).
>
> Do you know what's wrong here?
>
>
I forgot to say, it's Python 2.6.4 running on linux 2.6.33
--
Life is the only flaw in an otherwise perfect nonexistence
-- Schopenhauer
narke
More information about the Python-list
mailing list