24 Jun 2005 TazForEver   » (Journeyer)

Python : the socket.error trap

I spent my lunch hacking a dirty remote shell in python.

signal.signal(signal.SIGCHLD, reaper)
# ...
while True:
    client, who = server.accept()

Blam ! When the server process receives SIGCHLD, accept() is interrupted. So I added a try/except around the accept() :

  while True:
        try:
            client, who = server.accept()
        except OSError, e:
            if e.errno != errno.EINTR:
                sys.exit(e)
            else:
                continue

Re-blam. I didn't paid attention to the previous exception's type. It was socket.error. I though it was a subclass of OSError ... it's not. issubclass(socket.error, OSError) == False. pydoc socket :/

The correct except statement is :

   while True:
        try:
            client, who = server.accept()
        except socket.error, e:
            if e.args[0] != errno.EINTR:
                sys.exit(e)
            else:
                continue

The socket.* error classes are just Exception. It would be nice to turn them into OSError :)

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!