@@ -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
@@ -1457,16 +1458,16 @@ The first two examples support IPv4 only. ::
14571458
14581459 HOST = '' # Symbolic name meaning all available interfaces
14591460 PORT = 50007 # Arbitrary non-privileged port
1460- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1461- s.bind((HOST, PORT))
1462- s.listen(1)
1463- conn, addr = s.accept()
1464- print('Connected by', addr)
1465- while True:
1466- data = conn.recv(1024)
1467- if not data: break
1468- conn.sendall( data)
1469- conn.close( )
1461+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
1462+ s.bind((HOST, PORT))
1463+ s.listen(1)
1464+ conn, addr = s.accept()
1465+ with conn:
1466+ print('Connected by', addr)
1467+ while True:
1468+ data = conn.recv(1024)
1469+ if not data: break
1470+ conn.sendall(data )
14701471
14711472::
14721473
@@ -1475,11 +1476,10 @@ The first two examples support IPv4 only. ::
14751476
14761477 HOST = 'daring.cwi.nl' # The remote host
14771478 PORT = 50007 # The same port as used by the server
1478- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1479- s.connect((HOST, PORT))
1480- s.sendall(b'Hello, world')
1481- data = s.recv(1024)
1482- s.close()
1479+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
1480+ s.connect((HOST, PORT))
1481+ s.sendall(b'Hello, world')
1482+ data = s.recv(1024)
14831483 print('Received', repr(data))
14841484
14851485The 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. ::
15161516 print('could not open socket')
15171517 sys.exit(1)
15181518 conn, addr = s.accept()
1519- print('Connected by', addr)
1520- while True:
1521- data = conn.recv(1024)
1522- if not data: break
1523- conn.send( data)
1524- conn.close( )
1519+ with conn:
1520+ print('Connected by', addr)
1521+ while True:
1522+ data = conn.recv(1024)
1523+ if not data: break
1524+ conn.send(data )
15251525
15261526::
15271527
@@ -1549,9 +1549,9 @@ sends traffic to the first one connected successfully. ::
15491549 if s is None:
15501550 print('could not open socket')
15511551 sys.exit(1)
1552- s.sendall(b'Hello, world')
1553- data = s.recv(1024 )
1554- s.close( )
1552+ with s:
1553+ s.sendall(b'Hello, world' )
1554+ data = s.recv(1024 )
15551555 print('Received', repr(data))
15561556
15571557
0 commit comments