I was looking at some Python code the other day and came across this:
s.sendall(req % (len(body), body))
in which len(body) resolved to over 500K bytes. This was being sent to an Apache server, which should cap at 8190 bytes (I found it did when I tried to issue a similar request using C’s write() function). So what is so special about sendall() in Python?
It doesn’t matter if you’re sending data to Apache or anything else. The software on the remote end of the socket we’re talking about has essentially no direct impact on the difference in behavior between
write(2)andsocket.sendall.The difference is that
write(2)writes as many bytes as it can, then returns an integer indicating how many it wrote. It can’t write more than you pass it, of course. But it might write fewer. There may not be room in the kernel send buffer for all of the bytes passed to it.Contrast this with
socket.sendallwhich writes all the bytes you pass to it. It does this by callingwrite(2)multiple times, if necessary.