== Things that irritate me about Python's socket module I am afraid that I am too busy listening to the [[Men Without Hats http://en.wikipedia.org/wiki/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 http://www.python.org/doc/current/lib/module-socket.html]]. 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.)