Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

what’s the difference between socket.send() and socket.sendall() ?

3,108 views
Skip to first unread message

iMath

unread,
Jan 7, 2013, 5:35:20 AM1/7/13
to Python List

what’s the difference between socket.send() and socket.sendall() ?

It is so hard for me to tell the difference between them from the python doc

so what is the difference between them ?

and each one is suitable for which case ?

Thomas Rachel

unread,
Jan 7, 2013, 8:17:50 AM1/7/13
to
The docs are your friend. See

http://docs.python.org/2/library/socket.html#socket.socket.sendall

| [...] Unlike send(), this method continues to send data from string
| until either all data has been sent or an error occurs.

HTH,

Thomas

Steven D'Aprano

unread,
Jan 7, 2013, 10:28:18 AM1/7/13
to
On Mon, 07 Jan 2013 18:35:20 +0800, iMath wrote:

> <p class="MsoNormal"><tt><span lang="EN-US" style="font-size: 12pt;
> color: white; background-color: rgb(68, 110, 248); background-position:
> initial initial; background-repeat: initial initial; ">what’s the
> difference between socket</span></tt><tt><span lang="EN-US"
> style="font-size: 12pt; ">.send() and&nbsp;<span style="color: white;
> background-color: rgb(68, 110, 248); background-position: initial
> initial; background-repeat: initial initial; ">socket</span>.sendall()
> ?</span></tt> </p>


Please re-send your question as text, instead of as HTML (so-called "rich
text"). Since many people are reading this forum via Usenet, sending HTML
is considered abusive. This is a text newsgroup, not a binary newsgroup.

If you *must* use a client that sends HTML, please make sure that it
ALWAYS sends a plain text version of your message as well. But here are
eight reasons you should not rely on fancy formatting (colours, fonts,
bold, etc.) in text-based media such as email (or news):

- HTML code in email is one of the top 3 signs of spam. Many people
send "rich text" email straight to the trash as a way of eliminating
spam.

- HTML code in email is a privacy and security risk. For example,
that means that the sender can track whether or not you have read
the email using "web bugs" whether or not you consent to being
tracked. There are viruses, spyware and other malware that can be
transmitted through HTML code in email. For this reason, many
people filter HTML email straight to the trash.

- HTML code forces your choice in font, font size, colours, etc. on
the reader. Some people prefer to read emails using their own
choice of font rather than yours, and consider it rude for others
to try to force a different font. Sending white text on coloured
background is especially nasty, because it hurts readability of
even for people with perfect vision.

- Even if readers don't mind the use of "rich text" in principle, in
practice once they have received enough emails with pink text on a
purple and yellow background with blinking stars and dancing fairies
all over the page, in pure self-defence they may disable or delete
HTML emails.

- Use of colour for emphasis discriminates against the approximately
10% of the male population who are colour-blind.

- Use of italics or other formatting may discriminate against those
who are blind and using screen readers to "read" their email. I
once was on a maths mailing list for about three years before I
realised that the most prolific and helpful person there was as
blind as a bat.

- Programming is a *text-based* activity. Code depends on WHAT you
write, not its colour, or the font you use, or whether there are
smiley faces in the background winking at you. So especially in
programming circles, many people find HTML code in emails to be a
distraction and an annoyance. Being able to express yourself in
plain text without colours and fonts is a good practice for any
programmer to get used to.

- Even if you think that people who dislike HTML emails are wrong, or
silly, or being precious, or completely nuts, nevertheless you should
indulge us. You are asking for free advice. It does not pay for you to
offend or annoy those you are asking for help.


(Apologies to anyone on the "tutor" mailing list who has already seen
this message earlier today.)



--
Steven

Chris Angelico

unread,
Jan 7, 2013, 10:54:49 AM1/7/13
to pytho...@python.org
On Tue, Jan 8, 2013 at 2:28 AM, Steven D'Aprano
<steve+comp....@pearwood.info> wrote:
> I
> once was on a maths mailing list for about three years before I
> realised that the most prolific and helpful person there was as
> blind as a bat.

And that, I think, is what s/he would have most wanted: three years
(more, most likely) without any sort of special treatment. It's all
very well to talk about anti-discrimination laws, but on the internet,
nobody knows you're a bat, if I can mangle that expression without
offending people. We have some excellent people on a couple of MUDs
I'm on who are, similarly, blind and using screen-readers. Again, you
don't even know that that's the case until/unless a question comes out
about some piece of ASCII art (which there's a very VERY little of in
Threshold), or some client-specific question hints at the fact that
s/he is using one of the reader-friendly clients (which are fairly
ugly to the sighted).

As to the use of color for emphasis, though - I don't think the OP
used it like that. I've no idea what the significance of white-on-blue
words was, there; it completely eludes me. Maybe he was sending a
secret message in the color codes? In any case, Steven's eight reasons
are absolutely right; when HTML code isn't adding information, it
should be stripped, and when it is adding information, you risk a
large proportion of people not seeing it. So there's never a good time
to use HTML.

ChrisA

Philipp Hagemeister

unread,
Jan 8, 2013, 7:42:07 AM1/8/13
to iMath, Python List
socket.socket.send is a low-level method and basically just the
C/syscall method send(3) / send(2). It can send less bytes than you
requested.

socket.socket.sendall is a high-level Python-only method that sends the
entire buffer you pass or throws an exception. It does that by calling
send until everything has been sent or an error occurs.

If you're using TCP with blocking sockets and don't want to be bothered
by internals (this is the case for most simple network applications),
use sendall.

Otherwise, use send and make sure to read and process its return value.

- Philipp
signature.asc
0 new messages