diff options
author | Martin Panter <vadmium+py@gmail.com> | 2016-04-24 04:55:00 (GMT) |
---|---|---|
committer | Martin Panter <vadmium+py@gmail.com> | 2016-04-24 04:55:00 (GMT) |
commit | a497774b719a3bb61ea907eb2dfe0b435e61e24a (patch) | |
tree | 3f98792781310bc11e9b238902a610092d4022a4 /Doc/library/socketserver.rst | |
parent | 1e8ee9b3808cd6c1a7a29c75115d1060a8ee877b (diff) | |
parent | e37fc18b3c0d5fe13d75f37d9ae9c4387a46ee3d (diff) | |
download | cpython-a497774b719a3bb61ea907eb2dfe0b435e61e24a.zip cpython-a497774b719a3bb61ea907eb2dfe0b435e61e24a.tar.gz cpython-a497774b719a3bb61ea907eb2dfe0b435e61e24a.tar.bz2 |
Issue #24911: Merge socket context manager doc from 3.5
Diffstat (limited to 'Doc/library/socketserver.rst')
-rw-r--r-- | Doc/library/socketserver.rst | 13 |
1 files changed, 3 insertions, 10 deletions
diff --git a/Doc/library/socketserver.rst b/Doc/library/socketserver.rst index e148d30..1b8d7ff 100644 --- a/Doc/library/socketserver.rst +++ b/Doc/library/socketserver.rst @@ -474,17 +474,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)) @@ -583,14 +579,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 |