一文搞懂:【12.5】asyncio模拟http请求

简介: 一文搞懂:【12.5】asyncio模拟http请求

"

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"
image.png
相关文章
|
1天前
|
缓存 负载均衡 NoSQL
Redis系列学习文章分享---第十四篇(Redis多级缓存--封装Http请求+向tomcat发送http请求+根据商品id对tomcat集群负载均衡)
Redis系列学习文章分享---第十四篇(Redis多级缓存--封装Http请求+向tomcat发送http请求+根据商品id对tomcat集群负载均衡)
7 1
|
4天前
|
XML 开发框架 前端开发
http请求响应的contentType
http请求响应的contentType
21 5
|
6天前
|
域名解析 存储 缓存
HTTP请求流程概览:浏览器构建请求行含方法、URL和版本;检查缓存;解析IP与端口
【6月更文挑战第23天】 HTTP请求流程概览:浏览器构建请求行含方法、URL和版本;检查缓存;解析IP与端口;TCP连接(HTTP/1.1可能需排队);三次握手;发送请求头与体;服务器处理并返回响应;TCP连接可能关闭或保持;浏览器接收并显示响应,更新缓存。HTTP版本间有差异。
21 5
|
1天前
|
Java API Spring
Spring Boot中使用Feign进行HTTP请求
Spring Boot中使用Feign进行HTTP请求
|
1天前
|
运维 Serverless 数据处理
函数计算产品使用问题之如何通过HTTP请求进行调用
函数计算产品作为一种事件驱动的全托管计算服务,让用户能够专注于业务逻辑的编写,而无需关心底层服务器的管理与运维。你可以有效地利用函数计算产品来支撑各类应用场景,从简单的数据处理到复杂的业务逻辑,实现快速、高效、低成本的云上部署与运维。以下是一些关于使用函数计算产品的合集和要点,帮助你更好地理解和应用这一服务。
|
3天前
|
移动开发 JSON 程序员
程序员必知:【12.5】asyncio模拟http请求
程序员必知:【12.5】asyncio模拟http请求
|
3天前
|
安全
tp5使用ueditor请求后台配置项http错误,上传功能将不能正常使用!
tp5使用ueditor请求后台配置项http错误,上传功能将不能正常使用!
8 0
|
9天前
|
安全 搜索推荐
基础入门 HTTP数据包&Postman构造&请求方法&请求头修改&状态码判断
基础入门 HTTP数据包&Postman构造&请求方法&请求头修改&状态码判断
|
Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
最近在线上往hbase导数据,因为hbase写入能力比较强,没有太在意写的问题。让业务方进行历史数据的导入操作,中间发现一个问题,写入速度太快,并且业务数据集中到其中一个region,这个region无法split掉,处于不可用状态。
1312 0
|
Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
Every Programmer Should Know These Latency Numbers 1秒=1000毫秒(ms) 1秒=1,000,000 微秒(μs) 1秒=1,000,000,000 纳秒(ns) 1秒=1,000,000,000,000 皮秒(ps) L1 cache reference .
625 0