From 758f7e4487bebabfc3055c6b69619ca847d7a2e8 Mon Sep 17 00:00:00 2001 From: rtobar Date: Thu, 7 Oct 2021 01:49:44 +0800 Subject: [PATCH] bpo-45328: Avoid failure in OSs without TCP_NODELAY support (GH-28646) Operating systems without support for TCP_NODELAY will raise an OSError when trying to set the socket option, but the show can still go on. (cherry picked from commit 0571b934f5f9198c3461a7b631d7073ac0a5676f) Co-authored-by: rtobar --- Lib/http/client.py | 8 +++++++- .../next/Library/2021-09-30-08-22-44.bpo-45328.8Z-Q0B.rst | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2021-09-30-08-22-44.bpo-45328.8Z-Q0B.rst diff --git a/Lib/http/client.py b/Lib/http/client.py index 0fd9021b4a785d..a98432e5685063 100644 --- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -70,6 +70,7 @@ import email.parser import email.message +import errno import http import io import re @@ -944,7 +945,12 @@ def connect(self): """Connect to the host and port specified in __init__.""" self.sock = self._create_connection( (self.host,self.port), self.timeout, self.source_address) - self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) + # Might fail in OSs that don't implement TCP_NODELAY + try: + self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) + except OSError as e: + if e.errno != errno.ENOPROTOOPT: + raise if self._tunnel_host: self._tunnel() diff --git a/Misc/NEWS.d/next/Library/2021-09-30-08-22-44.bpo-45328.8Z-Q0B.rst b/Misc/NEWS.d/next/Library/2021-09-30-08-22-44.bpo-45328.8Z-Q0B.rst new file mode 100644 index 00000000000000..eeb49310e8f66a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-09-30-08-22-44.bpo-45328.8Z-Q0B.rst @@ -0,0 +1 @@ +Fixed :class:`http.client.HTTPConnection` to work properly in OSs that don't support the ``TCP_NODELAY`` socket option.