@@ -445,9 +445,6 @@ The following functions all create :ref:`socket objects <socket-objects>`.
445445 .. versionchanged :: 3.2
446446 *source_address * was added.
447447
448- .. versionchanged :: 3.2
449- support for the :keyword: `with ` statement was added.
450-
451448
452449.. function :: fromfd(fd, family, type, proto=0)
453450
@@ -831,6 +828,10 @@ Socket objects have the following methods. Except for
831828:meth: `~socket.makefile `, these correspond to Unix system calls applicable
832829to sockets.
833830
831+ .. versionchanged :: 3.2
832+ Support for the :term: `context manager ` protocol was added. Exiting the
833+ context manager is equivalent to calling :meth: `~socket.close `.
834+
834835
835836.. method :: socket.accept()
836837
@@ -1461,16 +1462,16 @@ The first two examples support IPv4 only. ::
14611462
14621463 HOST = '' # Symbolic name meaning all available interfaces
14631464 PORT = 50007 # Arbitrary non-privileged port
1464- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1465- s.bind((HOST, PORT))
1466- s.listen(1)
1467- conn, addr = s.accept()
1468- print('Connected by', addr)
1469- while True:
1470- data = conn.recv(1024)
1471- if not data: break
1472- conn.sendall( data)
1473- conn.close( )
1465+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
1466+ s.bind((HOST, PORT))
1467+ s.listen(1)
1468+ conn, addr = s.accept()
1469+ with conn:
1470+ print('Connected by', addr)
1471+ while True:
1472+ data = conn.recv(1024)
1473+ if not data: break
1474+ conn.sendall(data )
14741475
14751476::
14761477
@@ -1479,11 +1480,10 @@ The first two examples support IPv4 only. ::
14791480
14801481 HOST = 'daring.cwi.nl' # The remote host
14811482 PORT = 50007 # The same port as used by the server
1482- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1483- s.connect((HOST, PORT))
1484- s.sendall(b'Hello, world')
1485- data = s.recv(1024)
1486- s.close()
1483+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
1484+ s.connect((HOST, PORT))
1485+ s.sendall(b'Hello, world')
1486+ data = s.recv(1024)
14871487 print('Received', repr(data))
14881488
14891489The 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. ::
15201520 print('could not open socket')
15211521 sys.exit(1)
15221522 conn, addr = s.accept()
1523- print('Connected by', addr)
1524- while True:
1525- data = conn.recv(1024)
1526- if not data: break
1527- conn.send( data)
1528- conn.close( )
1523+ with conn:
1524+ print('Connected by', addr)
1525+ while True:
1526+ data = conn.recv(1024)
1527+ if not data: break
1528+ conn.send(data )
15291529
15301530::
15311531
@@ -1553,9 +1553,9 @@ sends traffic to the first one connected successfully. ::
15531553 if s is None:
15541554 print('could not open socket')
15551555 sys.exit(1)
1556- s.sendall(b'Hello, world')
1557- data = s.recv(1024 )
1558- s.close( )
1556+ with s:
1557+ s.sendall(b'Hello, world' )
1558+ data = s.recv(1024 )
15591559 print('Received', repr(data))
15601560
15611561
0 commit comments