views:

343

answers:

1

Suppose I have a socket. What is the difference between these two lines of code?

line 1:

os.read(some_socket.fileno(), 1024)

line 2:

some_socket.recv(1024)

...other than the fact that the first one doesn't work on Windows. In other words, can I substitute the second line for the first one? I've got a codebase that hasn't really been tested with Windows, and this is causing trouble.

+6  A: 

line 1 uses the underlining file descriptor to read the socket, so it is platform-dependant. Use line 2, since it is a portable, multi-platform way of accomplishing the same thing.

Obligatory: If you're doing anything serious, it's better to avoid having to deal with low-level sockets. They are hard to get right, it may seem things are working but there are many details. Those details are already solved in many networking frameworks and there's no reason to reinvent the wheel. I suggest twisted, it is pretty good.

nosklo
I would agree on the framework part. But this code is part of a test to determine how gracefully we can handle poor TCP connections, so I'd say the dirtier the better. :-)
Jason Baker