#Asyncio does not provide an interface to the HTTP protocol aiohttpImportAsyncioImportSocket fromUrllib.parseImportUrlparseasyncdefget_url (URL):#request HTML via socketURL =urlparse (URL) host=Url.netloc Path=Url.pathifPath = ="": Path="/" #establishing a socket connectionReader, writer = await asyncio.open_connection (host,80) Writer.write ("GET {} http/1.1\r\nhost:{}\r\nconnection:close\r\n\r\n". Format (path, host). Encode ("UTF8")) All_lines=[] Async forRaw_lineinchReader:data= Raw_line.decode ("UTF8") all_lines.append (data) HTML="\ n". Join (All_lines)returnHtmlasyncdefMain (): Tasks= [] forUrlinchRange (20): URL="http://shop.projectsedu.com/goods/{}/". Format (URL) tasks.append (asyncio.ensure_future (Get_url (URL))) forTaskinchasyncio.as_completed (Tasks): Result=await taskPrint(Result)if __name__=="__main__": ImportTime start_time=time.time () loop=Asyncio.get_event_loop () loop.run_until_complete (Main ())Print('Last time:{}'. Format (Time.time ()-start_time))
Asyncio Implementation Aiohttp