Issue #24911: All socket objects are context managers; update examples
diff --git a/Doc/library/socketserver.rst b/Doc/library/socketserver.rst
index f0cfc80..98d2c46 100644
--- a/Doc/library/socketserver.rst
+++ b/Doc/library/socketserver.rst
@@ -465,17 +465,13 @@
data = " ".join(sys.argv[1:])
# Create a socket (SOCK_STREAM means a TCP socket)
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-
- try:
+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
# Connect to server and send data
sock.connect((HOST, PORT))
sock.sendall(bytes(data + "\n", "utf-8"))
# Receive data from the server and shut down
received = str(sock.recv(1024), "utf-8")
- finally:
- sock.close()
print("Sent: {}".format(data))
print("Received: {}".format(received))
@@ -574,14 +570,11 @@
pass
def client(ip, port, message):
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.connect((ip, port))
- try:
+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
+ sock.connect((ip, port))
sock.sendall(bytes(message, 'ascii'))
response = str(sock.recv(1024), 'ascii')
print("Received: {}".format(response))
- finally:
- sock.close()
if __name__ == "__main__":
# Port 0 means to select an arbitrary unused port