[Python-ideas] socket.sendall() "counter" parameter
Antoine Pitrou
solipsis at pitrou.net
Fri Apr 18 14:04:46 CEST 2014
On Fri, 18 Apr 2014 13:34:45 +0200
"Giampaolo Rodola'" <g.rodola at gmail.com>
wrote:
> With current socket.sendall() implementation if an error occurs it's
> impossible to tell how much data was sent. As such I'm wondering whether it
> would make sense to add a "counter" parameter which gets incremented
> internally:
>
> sent = 0
> try:
> sock.sendall(data, counter=sent)
> except socket.error as err:
> priint("only %s bytes were sent" % sent)
>
> This would both allow to not lose information on error and avoid keeping
> track of the total data being sent, which usually requires and extra len()
> call. E.g. when sending a file:
>
> file = open('somefile', 'rb')
> total = 0
> while True:
> chunk = file.read(8192)
> if not chunk:
> break
> sock.sendall(chunk, counter=total)
Why not simply use send() in such cases?
(or, if you want something file-like, call makefile() and then write())
Regards
Antoine.
More information about the Python-ideas
mailing list