Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions Lib/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,19 +1012,35 @@ def connect(self):
if self._tunnel_host:
self._tunnel()

def close(self):
"""Close the connection to the HTTP server."""
def close(self, shutdown=False):
Copy link
Contributor

@Mr-Sunglasses Mr-Sunglasses Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add it in the Doc/library/socket.rst also, about the shutdown.

Ref.

.. method:: socket.close()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, shouldn't take too long

Copy link
Contributor Author

@sharktide sharktide Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mr-Sunglasses Finally ready for review/ Added/updated docs for the close method of HTTPConnection class as well as added back a modifed docstring in http/client.py!

self.__state = _CS_IDLE
try:
sock = self.sock
if sock:
self.sock = None
sock.close() # close it manually... there may be other refs
if shutdown:
try:
sock.shutdown(socket.SHUT_RDWR)
except OSError as e:
print(f"Socket shutdown error: {e}", file=sys.stderr)
except Exception as e:
print(f"Unexpected error during socket shutdown: {e}", file=sys.stderr)
try:
sock.close()
except OSError as e:
print(f"Socket close error: {e}", file=sys.stderr)
except Exception as e:
print(f"Unexpected error during socket close: {e}", file=sys.stderr)
finally:
response = self.__response
if response:
self.__response = None
response.close()
try:
response.close()
except OSError as e:
print(f"Response close error: {e}", file=sys.stderr)
except Exception as e:
print(f"Unexpected error during response close: {e}", file=sys.stderr)

def send(self, data):
"""Send 'data' to the server.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Issue: :gh:`130902`


Modified close(self) method of HTTPConnection class to include an optional socket shutdown similar to gh-130850. Includes error handling. As mentioned, it adds an optional shutdown param to the close method, keeping existing programs working, but adding an extra feature to new programs that offers improvements.
Loading