Last active
May 29, 2019 11:00
-
-
Save gvanrossum/18bdf248a679155f1381 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
END = b'Bye-bye!\n' | |
@asyncio.coroutine | |
def echo_client(): | |
reader, writer = yield from asyncio.open_connection('localhost', 8000) | |
writer.write(b'Hello, world\n') | |
writer.write(b'What a fine day it is.\n') | |
writer.write(END) | |
while True: | |
line = yield from reader.readline() | |
print('received:', line) | |
if line == END or not line: | |
break | |
writer.close() | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(echo_client()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
@asyncio.coroutine | |
def echo_server(): | |
yield from asyncio.start_server(handle_connection, 'localhost', 8000) | |
@asyncio.coroutine | |
def handle_connection(reader, writer): | |
while True: | |
data = yield from reader.read(8192) | |
if not data: | |
break | |
writer.write(data) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(echo_server()) | |
loop.run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(This is in response to https://gist.github.com/denik/8c4fcf4593ced686c718 from https://twitter.com/gevent/status/462617361821220864)