Message260393
I reviewed the patch.
> It would also be cool if you can add a short code snippet somewhere:
The socket module has examples.
Why not modifying these examples to promote the context manager protocol?
Example:
--------
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s
# use with to ensure that the socket is closed, especially on error
with 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)
conn.close()
--------
The second "with conn:" is maybe overkill. What do you think?
For a client connection, usually I prefer to explicitly close the socket (even if I use "with conn:") to get exception on my ".close()" line, instead of getting an exception from the context manager, which is harder to understand. |
|
Date |
User |
Action |
Args |
2016-02-17 11:25:01 | vstinner | set | recipients:
+ vstinner, docs@python, berker.peksag, martin.panter, desbma, yselivanov |
2016-02-17 11:25:01 | vstinner | set | messageid: <1455708301.04.0.952164054205.issue24911@psf.upfronthosting.co.za> |
2016-02-17 11:25:01 | vstinner | link | issue24911 messages |
2016-02-17 11:25:00 | vstinner | create | |
|