<div dir="ltr"><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">On Fri, Jul 3, 2015 at 9:14 AM, Marko Rauhamaa <marko at <a href="http://pacujo.net">pacujo.net</a>> wrote:<br>><br>>>> 1) is there a way to close just one direction of the connection?<br>>><br>>> No. SOCK_STREAM sockets are always bidirectional.<br>><br>> socket.shutdown(socket.SHUT_WR) does the trick.<br>><br>> I think the asyncio.StreamWriter.write_eof() is the high-level<br>> equivalent.<br><br>I stand corrected. And you're also correct about write_eof: it causes<br>shutdown(SHUT_WR) to be called on the underlying socket once the<br>buffer has been written.<br><br><a href="https://hg.python.org/cpython/file/34460219c0e0/Lib/asyncio/selector_events.py#l737">https://hg.python.org/cpython/file/34460219c0e0/Lib/asyncio/selector_events.py#l737</a><br><br>That said, just replacing the writer.close() with writer.write_eof()<br>in the OP's code doesn't seem to work; the server response comes back<br>empty.<br><br>This works:<br><br>>>> sock1, sock2 = socket.socketpair()<br>>>> sock1.send(b'REQUEST')<br>7<br>>>> sock1.shutdown(socket.SHUT_WR)<br>>>> sock2.recv(100)<br>b'REQUEST'<br>>>> sock2.send(b'RESPONSE')<br>8<br>>>> sock1.recv(100)<br>b'RESPONSE'<br><br>And this works:<br><br>import asyncio<br>import socket<br><br>def server(sock):<br>    request = yield from asyncio.get_event_loop().sock_recv(sock, 100)<br>    print("got request {!r}".format(request))<br>    yield from asyncio.get_event_loop().sock_sendall(sock, b'RESPONSE')<br><br>def client(sock):<br>    yield from asyncio.get_event_loop().sock_sendall(sock, b'REQUEST')<br>    sock.shutdown(socket.SHUT_WR)<br>    response = yield from asyncio.get_event_loop().sock_recv(sock, 100)<br>    print("got response {!r}".format(response))<br>    asyncio.get_event_loop().stop()<br><br>def connect():<br>    clientsock, serversock = socket.socketpair()<br>    clientsock.setblocking(False)<br>    serversock.setblocking(False)<br>    asyncio.async(client(clientsock))<br>    asyncio.async(server(serversock))<br><br>connect()<br>asyncio.get_event_loop().run_forever()<br><br>I'm wondering whether there might be a bug in the higher-level<br>transport code that interferes with reading after calling write_eof.</blockquote><div><br></div><div>There is a bug. See <a href="http://bugs.python.org/issue24539">http://bugs.python.org/issue24539</a> .<br><br></div></div>