#asyncio 没有提供http协议的接口 ,可以用aiohttp
import asyncio
import socket
from urllib.parse import urlparseasync def get_url(url):#通过socket请求htmlurl = urlparse(url)host = url.netlocpath = url.pathif path == "":path = "/"#建立socket连接reader, 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 for raw_line in reader:data = raw_line.decode("utf8")all_lines.append(data)html = "\n".join(all_lines)return htmlasync def main():tasks = []for url in range(20):url = "http://shop.projectsedu.com/goods/{}/".format(url)tasks.append(asyncio.ensure_future(get_url(url)))for task in asyncio.as_completed(tasks): #这里注意,完成一个就会打印一个,我也没看懂result = await taskprint(result)if __name__ == "__main__":import timestart_time = time.time()loop = asyncio.get_event_loop()loop.run_until_complete(main()) #这里也没有看懂,直接传个协程进来也可以。。。print('last time:{}'.format(time.time()-start_time))
详细解决方案
[python]asyncio 模拟 http 请求,并获取响应
热度:75 发布时间:2023-12-05 12:53:35.0
相关解决方案
- struts HTTP Status 500解决办法
- httpclient老是提示java.lang.NoSuchMethodError: org.apache.http.protocol.BasicHttpCon
- 启动tomcat后,http://localhost:8080无法打开的有关问题
- 相干tomcat HTTP Status 500的疑问,麻烦大家进来看看
- Web Service 错误:服务器未能识别 HTTP 头 SOAPAction 的值: http://tempuri.org/
- HTTP Status 404 - /jspdemo/Register.jsp,该怎么解决
- 'http://reg.163.com/login.jsp?type=1&url=http://entry.mail.163.com/coremail/fcg/,该如何处理
- org.apache.http.client.HttpClient与org.apache.commons.httpclient.HttpClient的差别
- HTTP 404有关问题
- myeclipse<%@ taglib prefix="c" uri="http://java.sun.com/jst1/core_rt"%>解决思路
- ,http 200状态码
- tomcat新手有关问题 HTTP Status 404 - /manager/
- java http 分段上载
- soap调用webservice,报错:Root element of a SOAP message must be: 'http://schemas.xmls,该怎么处理
- HTTP Status 404如何解决
- org.apache.tomcat.util.http.Parameters processParameters,该怎么解决
- 新手,HTTP Status 404 有关问题
- java http 断点上传,该如何处理
- JSP疑问<%@ taglib prefix="ht" uri="http://www.ht.com.cn/jsp/taglibs"%>该怎么处理
- weservice调用Server returned HTTP response code: 500 for URL解决方法
- HTTP Status 404 - /webproject2/servlet/login解决办法
- 报错:HTTP Status 405 - HTTP method GET is not supported by this URL解决思路
- http://newbbs.csdn.net/ 现时和老版的bbs用的还不是同一个库
- http://newbbs.csdn.net/ 现下和老版的bbs用的还不是同一个库
- Java.io.IOException: error 3 during http over WSP 这个是什么东东
- http://sourceforge.net这个网站是什么啊解决方案
- http 传送数据 每个包有大小限制码?解决办法
- Server returned HTTP response code: 403 for URL如何解决
- http://www.java-home.com重新下线,诚邀您的加入
- httpClient 错误 The server failed to respond with a valid HTTP response