Wandering Thoughts archives

2006-05-20

Things that irritate me about Python's socket module

I am afraid that I am too busy listening to the Men Without Hats to write too much tonight, so I am just going to trot out some of my irritations with Python's socket module. All of these are on Unix machines.

(I suspect that portability to Windows is the reason for some of these, but it doesn't mean that I'm not irritated by them.)

  • socket.error. Let me count the ways:

    • most instances should actually be IOError or OSError instead.
    • it has two entirely different formats; often the only sensible thing you can do with a socket error is to str() it.
    As a minimum there could be a socket.ioerror sub-class, like socket.herror, that guaranteed a single (errno, string) format.
  • sockets do not support file-like .read() and .write(), so all code you write has to know that it is dealing specifically with a socket. Almost all code doesn't care, and would be better off without this difference. (A great deal of code is probably incorrectly using .send() instead of .sendall() as a result, too.)

  • SSL sockets reverse this; they have .read() and .write(), but not the usual socket set. So code you write has to care whether your socket connection is SSL-wrapped or not.
  • SSL sockets lack any means of closing down the connection. Not only do you not get a .shutdown(), you don't even get a .close(). Apparently you are supposed to shoot them in the head or something.
  • for extra fun, an SSL connection throws an exception when it closes down. Even if it closes in a perfectly orderly manner because the other end told it using the right SSL magic.

I have a Python program that tries to do a simple network cat for SSL connections; these interface issues make it absurdly annoying, and the comments are fully of grumpy rants about it. (The program does work, and I suppose I should put it up on the net sometime.)

Despite all this, I have to say that the socket module makes dealing with the BSD sockets API relatively simple and clear while keeping pretty much all of the features available. Writing socket using programs in Python is significantly easier than doing so in C. (And somewhat easier than doing so in Perl, because Perl forces a somewhat lower level view of the whole mess.)

(Update: I think I've unfairly maligned Perl in my aside; see the comments.)

python/SocketModuleIrritations written at 03:29:45; Add Comment


Page tools: See As Normal.
Search:
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.