asyncio模块

asyncio模块

# import asyncio
#
# @asyncio.coroutine
# def task(task_id,senconds):
#     print('%s run' %task_id)
#     yield from asyncio.sleep(senconds)
#     print('%s done' %task_id)
#
# if __name__ == '__main__':
#     tasks = [
#         task('任务1',3),
#         task('任务2',2),
#         task('任务3',1),
#     ]
#
#     loop = asyncio.get_event_loop()
#     loop.run_until_complete(asyncio.wait(tasks))
#     loop.close()
#





import requests
import asyncio
import uuid

User_Agent='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'


def parse_page(res):
    with open('%s.html' %uuid.uuid1(),'wb') as f:
        f.write(res)

@asyncio.coroutine
def get_page(host,port=80,url='/',ssl=False,callback=parse_page):

    #1、建立连接
    if ssl:
        port=443
    print('下载:https:%s:%s:%s' %(host,port,url))
    recv,send=yield from asyncio.open_connection(host=host,port=port,ssl=ssl)

    #2、封装请求头
    request_headers="""GET %s HTTP/1.0
Host: %s
User-Agent: %s

""" %(url,host,User_Agent)
    request_headers=request_headers.encode('utf-8')

    #3、发送请求头
    send.write(request_headers)
    yield from send.drain()

    #4、接收响应头
    while True:
        line=yield from recv.readline()
        if line == b'
':
            break
        print('%s 响应头: %s' %(host,line))

    #5、接收响应体
    text=yield from recv.read()
    # print(text)
    #6、执行回调函数完成解析
    callback(text)

    #7、关闭
    send.close()

if __name__ == '__main__':
    tasks=[
        get_page(host='www.baidu.com',url='/s?wd=美女',ssl=True),
        get_page(host='www.cnblogs.com',url='/linhaifeng/articles/7806303.html',ssl=True)
    ]


    loop=asyncio.get_event_loop()
    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()
原文地址:https://www.cnblogs.com/zhongbokun/p/8330640.html