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

Skip to content

Commit a497774

Browse files
committed
Issue #24911: Merge socket context manager doc from 3.5
2 parents 1e8ee9b + e37fc18 commit a497774

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

@@ -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

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

Doc/library/socketserver.rst

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

476476
# Create a socket (SOCK_STREAM means a TCP socket)
477-
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
478-
479-
try:
477+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
480478
# Connect to server and send data
481479
sock.connect((HOST, PORT))
482480
sock.sendall(bytes(data + "\n", "utf-8"))
483481

484482
# Receive data from the server and shut down
485483
received = str(sock.recv(1024), "utf-8")
486-
finally:
487-
sock.close()
488484

489485
print("Sent: {}".format(data))
490486
print("Received: {}".format(received))
@@ -583,14 +579,11 @@ An example for the :class:`ThreadingMixIn` class::
583579
pass
584580

585581
def client(ip, port, message):
586-
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
587-
sock.connect((ip, port))
588-
try:
582+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
583+
sock.connect((ip, port))
589584
sock.sendall(bytes(message, 'ascii'))
590585
response = str(sock.recv(1024), 'ascii')
591586
print("Received: {}".format(response))
592-
finally:
593-
sock.close()
594587

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

0 commit comments

Comments
 (0)