diff options
author | Martin Panter <vadmium+py@gmail.com> | 2016-04-24 04:24:36 (GMT) |
---|---|---|
committer | Martin Panter <vadmium+py@gmail.com> | 2016-04-24 04:24:36 (GMT) |
commit | e37fc18b3c0d5fe13d75f37d9ae9c4387a46ee3d (patch) | |
tree | 5fb88e16ffe337006da9ca947011c53ea2e27290 /Doc/library | |
parent | 887bc96e2e06e261a8502cf6568a276be6f36260 (diff) | |
download | cpython-e37fc18b3c0d5fe13d75f37d9ae9c4387a46ee3d.zip cpython-e37fc18b3c0d5fe13d75f37d9ae9c4387a46ee3d.tar.gz cpython-e37fc18b3c0d5fe13d75f37d9ae9c4387a46ee3d.tar.bz2 |
Issue #24911: All socket objects are context managers; update examples
Diffstat (limited to 'Doc/library')
-rw-r--r-- | Doc/library/socket.rst | 54 | ||||
-rw-r--r-- | Doc/library/socketserver.rst | 13 |
2 files changed, 30 insertions, 37 deletions
diff --git a/Doc/library/socket.rst b/Doc/library/socket.rst index c09927c..8cf4dba 100644 --- a/Doc/library/socket.rst +++ b/Doc/library/socket.rst @@ -445,9 +445,6 @@ The following functions all create :ref:`socket objects <socket-objects>`. .. 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) @@ -831,6 +828,10 @@ Socket objects have the following methods. Except for :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() @@ -1457,16 +1458,16 @@ The first two examples support IPv4 only. :: 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) :: @@ -1475,11 +1476,10 @@ The first two examples support IPv4 only. :: 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 @@ -1516,12 +1516,12 @@ sends traffic to the first one connected successfully. :: 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) :: @@ -1549,9 +1549,9 @@ sends traffic to the first one connected successfully. :: 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 --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 @@ This is the client side:: 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 @@ An example for the :class:`ThreadingMixIn` class:: 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 |