调试代码如下:
# -*- coding:utf-8 -*-
import asyncio
@asyncio.coroutine
def wget(host):
print('wget %s...' % host)
content = asyncio.open_connection(host, 80)
reader, writer = yield from connect
header = 'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % host
writer.write(header.encode('utf-8'))
yield from writer.drain()
while True:
line = yield from reader.readline()
if line == b'\r\n':
break
print('%s header > %s' % (host, line.decode('utf-8'.rstrip())))
writer.close()
loop = asyncio.get_event_loop()
tasks = [wget(host) for host in ['www.sina.com', 'www.shu.com', 'www.163.com']]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
提示报错:
Traceback (most recent call last):
File "asyncio.py", line 2, in <module>
import asyncio
File "/application/study/python/python3/asyncio.py", line 4, in <module>
@asyncio.coroutine
AttributeError: module 'asyncio' has no attribute 'coroutine'
自己明明安装了asyncio库,还会报这个错,后来发现自己的文件名跟库名重复了,加载了当前的文件,所以就没有使用官方的库。那就改一下文件名就可以解决了