"
1 #!/usr/bin/env python
2 # -- coding:utf-8 --
3 # asyncio 没有提供http协议的接口
4 import asyncio
5 import time
6 from urllib.parse import urlparse
7
8
9 async def get_url(url):
10 url = urlparse(url)
11 host = url.netloc
12 path = url.path
13 if path == '':
14 path = '/'
15
16 # 建立连接
17 # asyncio.open_connection是一个协程
18 reader, writer = await asyncio.open_connection(host, 80)
19 writer.write(""GET {} HTTP/1.1\r\nHost:{}\r\nConnection:close\r\n\r\n"".format(path, host).encode('utf-8'))
20 all_lines = 【】
21 async for raw_line in reader:
22 line = raw_line.decode('utf-8')
23 all_lines.append(line)
24 html = '\n'.join(all_lines)
25 return html
26
27
28 async def main():
29 tasks = 【】
30 for url in range(1, 21):
31 url = ''.format(url)
32 tasks.append(asyncio.ensure_future(get_url(url)))
33 for task in asyncio.as_completed(tasks):
34 # asyncio.as_completed返回的是一个协程
35 result = await task
36 print(result)
37
38
39 if name == 'main':
40 start_time = time.time()
41 loop = asyncio.get_event_loop()
42 //代码效果参考:https://v.youku.com/v_show/id_XNjQwNjg1NzAwMA==.html
loop.run_until_complete(main())43 print('last time:{}'.format(time.time() - start_time))
?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528//代码效果参考:https://v.youku.com/v_show/id_XNjQwNjg1ODUwNA==.html
6287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Mon, 19 Aug 2019 11:38:06 GMT Content-Type: application/json Content-Length: 1497 Connection: close Vary: Accept, Cookie Allow: GET, HEAD, OPTIONS X-Frame-Options: SAMEORIGIN {""id"":1,""category"":{""id"":129,""sub_cat"":【】,""name"":""根茎类"",""code"":""gjl"",""desc"":"""",""category_type"":2,""is_tab"