Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e37fc18

Browse files
committed
Issue #24911: All socket objects are context managers; update examples
1 parent 887bc96 commit e37fc18

2 files changed

Lines changed: 30 additions & 37 deletions

File tree

Doc/library/socket.rst

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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
832829
to 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

14851485
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. ::
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

Doc/library/socketserver.rst

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -465,17 +465,13 @@ This is the client side::
465465
data = " ".join(sys.argv[1:])
466466

467467
# Create a socket (SOCK_STREAM means a TCP socket)
468-
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
469-
470-
try:
468+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
471469
# Connect to server and send data
472470
sock.connect((HOST, PORT))
473471
sock.sendall(bytes(data + "\n", "utf-8"))
474472

475473
# Receive data from the server and shut down
476474
received = str(sock.recv(1024), "utf-8")
477-
finally:
478-
sock.close()
479475

480476
print("Sent: {}".format(data))
481477
print("Received: {}".format(received))
@@ -574,14 +570,11 @@ An example for the :class:`ThreadingMixIn` class::
574570
pass
575571

576572
def client(ip, port, message):
577-
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
578-
sock.connect((ip, port))
579-
try:
573+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
574+
sock.connect((ip, port))
580575
sock.sendall(bytes(message, 'ascii'))
581576
response = str(sock.recv(1024), 'ascii')
582577
print("Received: {}".format(response))
583-
finally:
584-
sock.close()
585578

586579
if __name__ == "__main__":
587580
# Port 0 means to select an arbitrary unused port

0 commit comments

Comments
 (0)