python socket.sendall returns 成功是什么意思?

what does it mean when python socket.sendall returns successfully?

在我的代码中我写了这样的东西:

try:
    s.sendall(data)
except Exception as e:
    print e

现在,我可以假设如果 sendall 没有抛出任何异常,那么套接字的另一端(其内核)确实收到了 'data'?如果不是,那意味着我需要发送一个对我来说似乎不合理的应用程序确认。

如果我可以假设对方的内核确实收到了 'data' 那么这意味着 'sendall' returns 仅当它看到我放入的所有字节的 tcp ack 'data' 但是我看不到这方面的任何文档,相反,通过搜索网络,我感觉我不能假设收到了 ack。

是的你可以:)

根据 socket.sendall 文档:

socket.sendall(string[, flags]) Send data to the socket. The socket must be connected to a remote socket. The optional flags argument has the same meaning as for recv() above. Unlike send(), this method continues to send data from string until either all data has been sent or an error occurs. None is returned on success. On error, an exception is raised, and there is no way to determine how much data, if any, was successfully sent.

具体来说:

socket.sendall() 将继续发送所有数据,直到完成或发生 错误

更新: 回答您对正在发生的事情的评论在钩子下

查看 socketmodule.c 源代码,它似乎反复尝试 "send all data" 直到没有更多数据要发送。你可以在 L3611 } while (len > 0); 上看到这个。希望这能回答您的问题。

can I assume that if there wasn't any exception thrown by sendall that the other side of the socket (its kernel) did receive 'data'?

不,你不能。它告诉您系统已成功发送数据。它不会等待对端确认数据(即在 OS 内核接收到的数据),甚至不会等到数据被对端应用程序处理。此行为并非特定于 python。

通常情况下,对等系统内核是否收到数据并将其放入应用程序套接字缓冲区并不重要。真正重要的是它是否在应用程序内部接收和处理数据,这可能涉及复杂的事情,例如将数据插入数据库并等待成功提交,甚至将数据转发到另一个系统。由于由应用程序决定何时真正处理数据,因此您必须使您的应用程序特定 ACK 以表示处理成功。