# HG changeset patch # Parent 328383905eafcb7c4a39f065a922aaddacb66658 # Parent 328383905eafcb7c4a39f065a922aaddacb66658 Issue #24911: All socket objects are context managers; update examples diff -r 328383905eaf Doc/library/socket.rst --- a/Doc/library/socket.rst Mon Aug 31 03:15:52 2015 +0000 +++ b/Doc/library/socket.rst Thu Feb 18 00:35:53 2016 +0000 @@ -386,9 +386,6 @@ .. versionchanged:: 3.2 *source_address* was added. - .. versionchanged:: 3.2 - support for the :keyword:`with` statement was added. - .. function:: fromfd(fd, family, type, proto=0) @@ -770,6 +767,10 @@ :meth:`~socket.makefile`, these correspond to Unix system calls applicable to sockets. +.. versionchanged:: 3.2 + Support for the :term:`context manager` protocol was added. Exiting the + context manager is equivalent to calling :meth:`~socket.close`. + .. method:: socket.accept() @@ -1316,16 +1317,16 @@ HOST = '' # Symbolic name meaning all available interfaces PORT = 50007 # Arbitrary non-privileged port - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.bind((HOST, PORT)) - s.listen(1) - conn, addr = s.accept() - print('Connected by', addr) - while True: - data = conn.recv(1024) - if not data: break - conn.sendall(data) - conn.close() + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind((HOST, PORT)) + s.listen(1) + conn, addr = s.accept() + with conn: + print('Connected by', addr) + while True: + data = conn.recv(1024) + if not data: break + conn.sendall(data) :: @@ -1334,11 +1335,10 @@ HOST = 'daring.cwi.nl' # The remote host PORT = 50007 # The same port as used by the server - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect((HOST, PORT)) - s.sendall(b'Hello, world') - data = s.recv(1024) - s.close() + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.connect((HOST, PORT)) + s.sendall(b'Hello, world') + data = s.recv(1024) print('Received', repr(data)) The next two examples are identical to the above two, but support both IPv4 and @@ -1375,12 +1375,12 @@ print('could not open socket') sys.exit(1) conn, addr = s.accept() - print('Connected by', addr) - while True: - data = conn.recv(1024) - if not data: break - conn.send(data) - conn.close() + with conn: + print('Connected by', addr) + while True: + data = conn.recv(1024) + if not data: break + conn.send(data) :: @@ -1408,9 +1408,9 @@ if s is None: print('could not open socket') sys.exit(1) - s.sendall(b'Hello, world') - data = s.recv(1024) - s.close() + with s: + s.sendall(b'Hello, world') + data = s.recv(1024) print('Received', repr(data)) diff -r 328383905eaf Doc/library/socketserver.rst --- a/Doc/library/socketserver.rst Mon Aug 31 03:15:52 2015 +0000 +++ b/Doc/library/socketserver.rst Thu Feb 18 00:35:53 2016 +0000 @@ -417,17 +417,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)) @@ -526,14 +522,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