summaryrefslogtreecommitdiffstats
path: root/Doc/library/socket.rst
diff options
context:
space:
mode:
authorMartin Panter <vadmium+py@gmail.com>2016-04-24 04:55:00 (GMT)
committerMartin Panter <vadmium+py@gmail.com>2016-04-24 04:55:00 (GMT)
commita497774b719a3bb61ea907eb2dfe0b435e61e24a (patch)
tree3f98792781310bc11e9b238902a610092d4022a4 /Doc/library/socket.rst
parent1e8ee9b3808cd6c1a7a29c75115d1060a8ee877b (diff)
parente37fc18b3c0d5fe13d75f37d9ae9c4387a46ee3d (diff)
downloadcpython-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/socket.rst')
-rw-r--r--Doc/library/socket.rst54
1 files changed, 27 insertions, 27 deletions
diff --git a/Doc/library/socket.rst b/Doc/library/socket.rst
index cd6e310..ceb72b7 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()
@@ -1461,16 +1462,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)
::
@@ -1479,11 +1480,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
@@ -1520,12 +1520,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)
::
@@ -1553,9 +1553,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))