[issue43332] Make http.client._tunnel send one byte string over the network

Gregory P. Smith report at bugs.python.org
Fri Mar 5 17:13:24 EST 2021


Gregory P. Smith <greg at krypto.org> added the comment:

FYI another common socket idiom for this, specifically added for use in old HTTP 1.x servers building up responses, is setting and clearing the TCP_CORK (Linux) or TCP_NOPUSH (FreeBSD, and maybe macos? [buggy?]) socket option before and after the set of send()s you want to be optimally buffered into packets.

A more modern approach (often used with TCP_CORK) is not to build up a single buffer at all.  Instead use writev().  os.writev() exists in Python these days.  It limits userspace data shuffling in the normal case of everything getting written.

Unfortunately... this old http client code and API isn't really setup for that.  Everything is required to go through the HTTPConnection.send method.  And the send method is not defined to take a list of bytes objects as writev() would require.  

So for this code, the patch we want is just the joined bytes or BytesIO. It is the simple better change that works everywhere without getting into platform specific idioms.

For modern best practices I'd look to the async frameworks instead of this older library.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43332>
_______________________________________


More information about the Python-bugs-list mailing list