diff --git a/samples/async/async_wget2.py b/samples/async/async_wget2.py index aac074dc..6dc17f7d 100644 --- a/samples/async/async_wget2.py +++ b/samples/async/async_wget2.py @@ -4,12 +4,22 @@ import threading import asyncio -async def hello(): - print('Hello world! (%s)' % threading.currentThread()) - await asyncio.sleep(1) - print('Hello again! (%s)' % threading.currentThread()) +async def wget(host): + print('wget %s...' % host) + connect = asyncio.open_connection(host, 80) + reader, writer = await connect + header = 'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % host + writer.write(header.encode('utf-8')) + await writer.drain() + while True: + line = await reader.readline() + if line == b'\r\n': + break + print('%s header > %s' % (host, line.decode('utf-8').rstrip())) + # Ignore the body, close the socket + writer.close() loop = asyncio.get_event_loop() -tasks = [hello(), hello()] +tasks = [wget(host) for host in ['www.sina.com.cn', 'www.sohu.com', 'www.163.com']] loop.run_until_complete(asyncio.wait(tasks)) -loop.close() +loop.close() \ No newline at end of file