【小沐学Python】Python实现Web服务器(aiohttp)

2023-12-24 04:52

本文主要是介绍【小沐学Python】Python实现Web服务器(aiohttp),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 1、简介
  • 2、下载和安装
  • 3、代码测试
    • 3.1 客户端
    • 3.2 服务端
  • 4、更多测试
    • 4.1 asyncio
    • 4.2 aiohttp+HTTP服务器
    • 4.3 aiohttp+爬虫实例
    • 4.4 aiohttp+requests比较
  • 结语

1、简介

https://github.com/aio-libs/aiohttp
https://docs.aiohttp.org/en/stable/index.html

Asynchronous HTTP client/server framework for asyncio and Python
异步 http 客户端/服务器框架

在这里插入图片描述

在这里插入图片描述
主要特点:

  • 支持 HTTP 协议的客户端和服务器端。
  • 支持开箱即用的客户端和服务器 Web 套接字,并避免 回调地狱。
  • 为 Web 服务器提供中间件和可插拔路由。

2、下载和安装

在这里插入图片描述
安装库:

pip3 install aiohttp
# pip install aiodns
# pip install aiohttp[speedups]

在这里插入图片描述

3、代码测试

3.1 客户端

  • 客户端:要从网络上获取某些内容。
import aiohttp
import asyncioasync def main():async with aiohttp.ClientSession() as session:async with session.get('https://www.baidu.com/') as response:print("Status:", response.status)print("Content-type:", response.headers['content-type'])html = await response.text()print("Body:", html[:15], "...")asyncio.run(main())

运行之后:
在这里插入图片描述
报错了。
修改代码如下:

import aiohttp
import asyncioasync def main():async with aiohttp.ClientSession() as session:async with session.get('https://www.baidu.com/') as response:print("Status:", response.status)print("Content-type:", response.headers['content-type'])html = await response.text()print("Body:", html[:15], "...")# asyncio.run(main())
loop = asyncio.get_event_loop()
loop.run_until_complete(main())

再次运行之后,没有报错。
在这里插入图片描述

3.2 服务端

  • 服务器:使用简单服务器的示例。
# examples/server_simple.py
from aiohttp import webasync def handle(request):name = request.match_info.get('name', "Anonymous")text = "Hello, " + namereturn web.Response(text=text)async def wshandle(request):ws = web.WebSocketResponse()await ws.prepare(request)async for msg in ws:if msg.type == web.WSMsgType.text:await ws.send_str("Hello, {}".format(msg.data))elif msg.type == web.WSMsgType.binary:await ws.send_bytes(msg.data)elif msg.type == web.WSMsgType.close:breakreturn wsapp = web.Application()
app.add_routes([web.get('/', handle),web.get('/echo', wshandle),web.get('/{name}', handle)])if __name__ == '__main__':web.run_app(app)

运行之后:
在这里插入图片描述
浏览器访问网址:

http://127.0.0.1:8080/

在这里插入图片描述
在这里插入图片描述

4、更多测试

4.1 asyncio

asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持。

asyncio的编程模型就是一个消息循环。我们从asyncio模块中直接获取一个EventLoop的引用,然后把需要执行的协程扔到EventLoop中执行,就实现了异步IO。

import asyncio@asyncio.coroutine
def hello():print("Hello world!111")print("Hello world!22")# 异步调用asyncio.sleep(1):r = yield from asyncio.sleep(1)print("Hello again!333")print("Hello again!444")# 获取EventLoop:
loop = asyncio.get_event_loop()
# 执行coroutine
loop.run_until_complete(hello())
loop.close()

在这里插入图片描述

import threading
import asyncio@asyncio.coroutine
def hello():print('Hello world! (%s)' % threading.currentThread())yield from asyncio.sleep(1)print('Hello again! (%s)' % threading.currentThread())loop = asyncio.get_event_loop()
tasks = [hello(), hello()]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()

在这里插入图片描述

import asyncio@asyncio.coroutine
def wget(host):print('wget %s...' % host)connect = asyncio.open_connection(host, 80)reader, writer = yield from connectheader = 'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % hostwriter.write(header.encode('utf-8'))yield from writer.drain()while True:line = yield from reader.readline()if line == b'\r\n':breakprint('%s header > %s' % (host, line.decode('utf-8').rstrip()))# Ignore the body, close the socketwriter.close()loop = asyncio.get_event_loop()
tasks = [wget(host) for host in ['www.sina.com.cn', 'www.sohu.com', 'www.163.com']]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()

在这里插入图片描述
asyncio提供了完善的异步IO支持;
异步操作需要在coroutine中通过yield from完成;

4.2 aiohttp+HTTP服务器

编写一个HTTP服务器:

import asynciofrom aiohttp import webasync def index(request):await asyncio.sleep(0.5)return web.Response(text='<h1>Index</h1>', content_type= 'text/html')async def hello(request):await asyncio.sleep(0.5)text = '<h1>hello, %s!</h1>' % request.match_info['name']return web.Response(text=text, content_type= 'text/html')async def init(loop):app = web.Application(loop=loop)app.router.add_route('GET', '/', index)app.router.add_route('GET', '/hello/{name}', hello)srv = await loop.create_server(app.make_handler(), '127.0.0.1', 8000)print('Server started at http://127.0.0.1:8000...')return srvloop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
loop.run_forever()

在这里插入图片描述
在这里插入图片描述

4.3 aiohttp+爬虫实例

pip install bs4 

编写一个爬虫实例:

import asyncio
import aiohttp
from bs4 import BeautifulSoup
import loggingclass AsnycSpider(object):def __init__(self, url_list, max_threads):self.urls = url_listself.results = {}self.max_threads = max_threadsdef __parse_results(self, url, html):try:soup = BeautifulSoup(html, 'html.parser')title = soup.find('title').get_text()except Exception as e:raise eif title:self.results[url] = titleasync def get_body(self, url):async with aiohttp.ClientSession() as session:async with session.get(url, timeout=30) as response:assert response.status == 200html = await response.read()return response.url, htmlasync def get_results(self, url):url, html = await self.get_body(url)self.__parse_results(url, html)return 'Completed'async def handle_tasks(self, task_id, work_queue):while not work_queue.empty():current_url = await work_queue.get()try:task_status = await self.get_results(current_url)except Exception as e:logging.exception('Error for {}'.format(current_url), exc_info=True)def eventloop(self):q = asyncio.Queue()[q.put_nowait(url) for url in self.urls]loop = asyncio.get_event_loop()tasks = [self.handle_tasks(task_id, q, ) for task_id in range(self.max_threads)]loop.run_until_complete(asyncio.wait(tasks))# loop.close()if __name__ == '__main__':async_example = AsnycSpider(['https://www.qq.com/','https://www.163.com/','https://news.baidu.com/','https://blog.csdn.net/'], 5)async_example.eventloop()print(async_example.results)

在这里插入图片描述

4.4 aiohttp+requests比较

在 Python 众多的 HTTP 客户端中,有这几个:requests、aiohttp和httpx。在不借助其他第三方库的情况下,requests只能发送同步请求;aiohttp只能发送异步请求;httpx既能发送同步请求,又能发送异步请求。

pip install requests
  • test_requests.py
import random
import time
import datetime
import requestsdef make_request(session):resp = session.get('http://httpbin.org/get')# result = resp.text# print(result)passdef main():session = requests.Session()start = time.time()for _ in range(100):make_request(session)end = time.time()print(f'发送100次请求,耗时:{end - start}')if __name__ == '__main__':main()

在这里插入图片描述

  • test_aiohttp.py
import aiohttp
import random
import datetime
import asyncio
import timeasync def request(client):async with client.get('http://httpbin.org/get') as resp:# print(resp.status)# print(await resp.text())passasync def main():async with aiohttp.ClientSession() as client:start = time.time()task_list = []for _ in range(100):req = request(client)task = asyncio.create_task(req)task_list.append(task)await asyncio.gather(*task_list)end = time.time()print(f'发送100次请求,耗时:{end - start}')asyncio.run(main())

在这里插入图片描述

结语

如果您觉得该方法或代码有一点点用处,可以给作者点个赞,或打赏杯咖啡;╮( ̄▽ ̄)╭
如果您感觉方法或代码不咋地//(ㄒoㄒ)//,就在评论处留言,作者继续改进;o_O???
如果您需要相关功能的代码定制化开发,可以留言私信作者;(✿◡‿◡)
感谢各位大佬童鞋们的支持!( ´ ▽´ )ノ ( ´ ▽´)っ!!!

这篇关于【小沐学Python】Python实现Web服务器(aiohttp)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/530596

相关文章

8种快速易用的Python Matplotlib数据可视化方法汇总(附源码)

《8种快速易用的PythonMatplotlib数据可视化方法汇总(附源码)》你是否曾经面对一堆复杂的数据,却不知道如何让它们变得直观易懂?别慌,Python的Matplotlib库是你数据可视化的... 目录引言1. 折线图(Line Plot)——趋势分析2. 柱状图(Bar Chart)——对比分析3

Spring Boot 整合 Redis 实现数据缓存案例详解

《SpringBoot整合Redis实现数据缓存案例详解》Springboot缓存,默认使用的是ConcurrentMap的方式来实现的,然而我们在项目中并不会这么使用,本文介绍SpringB... 目录1.添加 Maven 依赖2.配置Redis属性3.创建 redisCacheManager4.使用Sp

Python模拟串口通信的示例详解

《Python模拟串口通信的示例详解》pySerial是Python中用于操作串口的第三方模块,它支持Windows、Linux、OSX、BSD等多个平台,下面我们就来看看Python如何使用pySe... 目录1.win 下载虚www.chinasem.cn拟串口2、确定串口号3、配置串口4、串口通信示例5

Kali Linux安装实现教程(亲测有效)

《KaliLinux安装实现教程(亲测有效)》:本文主要介绍KaliLinux安装实现教程(亲测有效),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、下载二、安装总结一、下载1、点http://www.chinasem.cn击链接 Get Kali | Kal

C#使用MQTTnet实现服务端与客户端的通讯的示例

《C#使用MQTTnet实现服务端与客户端的通讯的示例》本文主要介绍了C#使用MQTTnet实现服务端与客户端的通讯的示例,包括协议特性、连接管理、QoS机制和安全策略,具有一定的参考价值,感兴趣的可... 目录一、MQTT 协议简介二、MQTT 协议核心特性三、MQTTNET 库的核心功能四、服务端(BR

SpringCloud整合MQ实现消息总线服务方式

《SpringCloud整合MQ实现消息总线服务方式》:本文主要介绍SpringCloud整合MQ实现消息总线服务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、背景介绍二、方案实践三、升级版总结一、背景介绍每当修改配置文件内容,如果需要客户端也同步更新,

Web技术与Nginx网站环境部署教程

《Web技术与Nginx网站环境部署教程》:本文主要介绍Web技术与Nginx网站环境部署教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Web基础1.域名系统DNS2.Hosts文件3.DNS4.域名注册二.网页与html1.网页概述2.HTML概述3.

Dubbo之SPI机制的实现原理和优势分析

《Dubbo之SPI机制的实现原理和优势分析》:本文主要介绍Dubbo之SPI机制的实现原理和优势,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Dubbo中SPI机制的实现原理和优势JDK 中的 SPI 机制解析Dubbo 中的 SPI 机制解析总结Dubbo中

Python Pandas高效处理Excel数据完整指南

《PythonPandas高效处理Excel数据完整指南》在数据驱动的时代,Excel仍是大量企业存储核心数据的工具,Python的Pandas库凭借其向量化计算、内存优化和丰富的数据处理接口,成为... 目录一、环境搭建与数据读取1.1 基础环境配置1.2 数据高效载入技巧二、数据清洗核心战术2.1 缺失

使用Java实现Navicat密码的加密与解密的代码解析

《使用Java实现Navicat密码的加密与解密的代码解析》:本文主要介绍使用Java实现Navicat密码的加密与解密,通过本文,我们了解了如何利用Java语言实现对Navicat保存的数据库密... 目录一、背景介绍二、环境准备三、代码解析四、核心代码展示五、总结在日常开发过程中,我们有时需要处理各种软