Is socket.shutdown(1) useless

P

pyguy2

Issues of socket programming can be wierd, so I'm looking for some
comments.

In my python books I find exclusive use of socket.close(). From my
other readings, I know about a "partial close operation". So, I figured
it would be useful to post some code about how socket.close() has an
implicit send in it and you can actually gain some clarity by being
more explicit with the partial close which means splitting
socket.close() up into socket.shutdown(1) and socket.close().

And got a response in essence saying, why bother, socket.shutdown,
isn't useful.

Here is my thinking:

With a standard socket.close(), the client closes the socket
immediately after the implicit send. This means the client assumes it
was ok to actually close the socket, independant of how the server
reacts to that last bit of data. To me that is an assumption you may
not always want to make.

If, instead, the client does a socket.shutdown(1) to say it is done
sending, it can still recv and wait for the server to respond with
either:
1)yep, I agree you finished sending
or
2)I know you are done,and I got your data, but I do not think you are
done

To me these seem like a very useful distinction, since now if the
client cares, it can find out if it's final communication did matter.
It helps avoid what I call the princess bride phenomenon of #2:

"You keep using that word. I do not think it means what you think it
means."

So, is this whole business with socket.shutdown mostly useless? So
useless that I cannot find any mention of it in 2nd edition,
Programming Python.

john
 
S

Steven Bethard

In my python books I find exclusive use of socket.close(). From my
other readings, I know about a "partial close operation". So, I figured
it would be useful to post some code about how socket.close() has an
implicit send in it and you can actually gain some clarity by being
more explicit with the partial close which means splitting
socket.close() up into socket.shutdown(1) and socket.close().

And got a response in essence saying, why bother, socket.shutdown,
isn't useful.

I had to use socket.shutdown once because the socket architecture we had
relied on sending a piece of data, waiting for it to be processed, and
then receiving a result back. The server on the other end of the socket
wouldn't process anything until it was sure that all data had been sent.
So our code basically looked like:

s = socket.socket(...)
s.connect(...)
s.send(...)
s.shutdown(1)
data = s.makefile().read()
s.close()

STeVe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,234
Messages
2,571,179
Members
47,811
Latest member
GregoryHal

Latest Threads

Top