<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Apr 18, 2014 at 2:04 PM, Antoine Pitrou <span dir="ltr"><<a href="mailto:solipsis@pitrou.net" target="_blank">solipsis@pitrou.net</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">On Fri, 18 Apr 2014 13:34:45 +0200<br>
"Giampaolo Rodola'" <<a href="mailto:g.rodola@gmail.com">g.rodola@gmail.com</a>><br>
wrote:<br>
<div><div class="h5">> With current socket.sendall() implementation if an error occurs it's<br>
> impossible to tell how much data was sent. As such I'm wondering whether it<br>
> would make sense to add a "counter" parameter which gets incremented<br>
> internally:<br>
><br>
> sent = 0<br>
> try:<br>
>      sock.sendall(data, counter=sent)<br>
> except socket.error as err:<br>
>      priint("only %s bytes were sent" % sent)<br>
><br>
> This would both allow to not lose information on error and avoid keeping<br>
> track of the total data being sent, which usually requires and extra len()<br>
> call. E.g. when sending a file:<br>
><br>
> file = open('somefile', 'rb')<br>
> total = 0<br>
> while True:<br>
>     chunk = file.read(8192)<br>
>     if not chunk:<br>
>         break<br>
>     sock.sendall(chunk, counter=total)<br>
<br>
</div></div>Why not simply use send() in such cases?<br>
(or, if you want something file-like, call makefile() and then write())<br>
<br>
Regards<br>
<span class=""><font color="#888888"><br>
Antoine.</font></span></blockquote><div><br></div><div>send() requires to keep track of how much data has actually been sent, hence you need to add some additional logic to resend the missing (tail) part (e.g.:   <a href="http://hg.python.org/cpython/file/7433f7bce880/Lib/asyncio/unix_events.py#l389">http://hg.python.org/cpython/file/7433f7bce880/Lib/asyncio/unix_events.py#l389</a>).</div>

<div>That is is fine, but also a bit annoying when you're lazy and grumpy like me.  ;-)</div><div>Actually what would really be useful for sendall() in order to be as "nicer" as possible in terms of usability would be to:</div>

<div><br></div><div>1 - return the number of bytes sent instead of None; that would spare you from using "total += len(data)" on every iteration.</div><div><br></div><div>2 - in case of error set a "sent" attribute to the returned (socket.error) exception.</div>

<div><br></div><div>I'm not sure how #2 is feasible in practice though nor if it's acceptable to have only certain socket.error exceptions providing extra attributes.</div><div><br></div></div>-- <br><div dir="ltr">

<div>Giampaolo - <a href="http://grodola.blogspot.com" target="_blank">http://grodola.blogspot.com</a></div><div><br></div></div>
</div></div>