Closing socket file descriptors

js ebgssth at gmail.com
Sun May 20 03:35:44 EDT 2007


Hello, Yang.

You're not supposed to use os.open there.
See the doc at http://docs.python.org/lib/os-fd-ops.html

Is there any reason you want to use os.close?

On 20 May 2007 04:26:12 GMT, Yang
<DELETETHISyang.news at mailnulldeletethis.com> wrote:
> Hi, I'm experiencing a problem when trying to close the file descriptor
> for a socket, creating another socket, and then closing the file
> descriptor for that second socket. I can't tell if my issue is about
> Python or POSIX.
>
> In the following, the first time through, everything works.  On the
> second connection, though, the same file descriptor as the first
> connection may be re-used, but for some reason, trying to do
> os.read/close on that file descriptor will cause an error.
>
> Thanks in advance for any help on why this problem is occurring and/or
> how to resolve it (preferrably, I can continue to use file descriptors
> instead of resorting to socket.recv/socket.close).
>
>     def handle( s ):
>         print id(s), s
>         print os.read( s.fileno(), 4096 )  # s.recv(4096)
>         os.close( s.fileno() )  # s.close()
>     svr = socket.socket()
>     svr.bind( ( 'localhost', 8003 ) )
>     svr.listen( 1 )
>     while True:
>         print 'accepting'
>         s,_ = svr.accept()
>         handle( s )
>
> # Traceback (most recent call last):
> #   File "./normal_server_close_error.py", line 25, in <module>
> #     handle( s )
> #   File "./normal_server_close_error.py", line 13, in handle
> #     print os.read( s.fileno(), 4096 )  # s.recv(4096)
> # OSError: [Errno 9] Bad file descriptor
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list