Start a socket server, with a callback for each client connected. The return
value is the same as create_server().

The client_connected_cb parameter is called with two parameters:
client_reader, client_writer. client_reader is a
StreamReader object, while client_writer is a
StreamWriter object. The client_connected_cb parameter can
either be a plain callback function or a coroutine function; if it is a coroutine function, it will be automatically
converted into a Task.

The rest of the arguments are all the usual arguments to
create_server() except protocol_factory; most
common are positional host and port, with various optional keyword
arguments following.

Additional optional keyword arguments are loop (to set the event loop
instance to use) and limit (to set the buffer limit passed to the
StreamReader).

On success, the data and separator will be removed from the
internal buffer (consumed). Returned data will include the
separator at the end.

Configured stream limit is used to check result. Limit sets the
maximal length of data that can be returned, not counting the
separator.

If an EOF occurs and the complete separator is still not found,
an IncompleteReadError exception will be
raised, and the internal buffer will be reset. The
IncompleteReadError.partial attribute may contain the
separator partially.

If the data cannot be read because of over limit, a
LimitOverrunError exception will be raised, and the data
will be left in the internal buffer, so it can be read again.

Let the write buffer of the underlying transport a chance to be flushed.

The intended use is to write:

w.write(data)yield fromw.drain()

When the size of the transport buffer reaches the high-water limit (the
protocol is paused), block until the size of the buffer is drained down
to the low-water limit and the protocol is resumed. When there is nothing
to wait for, the yield-from continues immediately.

Yielding from drain() gives the opportunity for the loop to
schedule the write operation and flush the buffer. It should especially
be used when a possibly large amount of data is written to the transport,
and the coroutine does not yield-from between calls to write().

stream_reader is a StreamReader instance, client_connected_cb
is an optional function called with (stream_reader, stream_writer) when a
connection is made, loop is the event loop instance to use.

(This is a helper class instead of making StreamReader itself a
Protocol subclass, because the StreamReader has other
potential uses, and to prevent the user of the StreamReader from
accidentally calling inappropriate methods of the protocol.)

importasyncio@asyncio.coroutinedefhandle_echo(reader,writer):data=yield fromreader.read(100)message=data.decode()addr=writer.get_extra_info('peername')print("Received %r from %r"%(message,addr))print("Send: %r"%message)writer.write(data)yield fromwriter.drain()print("Close the client socket")writer.close()loop=asyncio.get_event_loop()coro=asyncio.start_server(handle_echo,'127.0.0.1',8888,loop=loop)server=loop.run_until_complete(coro)# Serve requests until Ctrl+C is pressedprint('Serving on {}'.format(server.sockets[0].getsockname()))try:loop.run_forever()exceptKeyboardInterrupt:pass# Close the serverserver.close()loop.run_until_complete(server.wait_closed())loop.close()

importasynciotry:fromsocketimportsocketpairexceptImportError:fromasyncio.windows_utilsimportsocketpair@asyncio.coroutinedefwait_for_data(loop):# Create a pair of connected socketsrsock,wsock=socketpair()# Register the open socket to wait for datareader,writer=yield fromasyncio.open_connection(sock=rsock,loop=loop)# Simulate the reception of data from the networkloop.call_soon(wsock.send,'abc'.encode())# Wait for datadata=yield fromreader.read(100)# Got data, we are done: close the socketprint("Received:",data.decode())writer.close()# Close the second socketwsock.close()loop=asyncio.get_event_loop()loop.run_until_complete(wait_for_data(loop))loop.close()