Skip to content

Instantly share code, notes, and snippets.

@gvanrossum
Last active May 29, 2019 11:00
Show Gist options
  • Save gvanrossum/18bdf248a679155f1381 to your computer and use it in GitHub Desktop.
Save gvanrossum/18bdf248a679155f1381 to your computer and use it in GitHub Desktop.
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())
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()
@gvanrossum
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment