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\r\nHost: %s\r\nUser-Agent: %s\r\n\r\n""" %(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'\r\n': 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()
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步