PythonライブラリノックDay105 外部ライブラリ #プログラミング #python #library

テクノロジー



Pythonのライブラリの紹介です。それぞれの説明と使用例は以下になります。

L 313. quart(Webフレームワーク)
quart は、 Flask互換 の非同期(async/await)対応Webフレームワークです。ASGI対応で、WebSocketやHTTP/2にも対応します。

▼主な機能
Flask互換のAPI(Flaskアプリの移植が容易)
非同期処理対応(async defでルーティング可能)
WebSocket/HTTP2/Streaming 対応
ASGI互換:UvicornなどのASGIサーバで動作

▼使い方の例(Hello World)
from quart import Quart

app = Quart(__name__)

@app.route(‘/’)
async def hello():
return ‘Hello, Quart!’

if __name__ == ‘__main__’:
app.run()

▼インストール
pip install quart

L 314. aiohttp(HTTPクライアント・サーバライブラリ)
aiohttp、 は 非同期のHTTPクライアントおよびサーバライブラリです。asyncioベースで、HTTP通信を効率的に処理できます。

▼主な機能
非同期HTTPクライアント機能
非同期Webサーバ構築機能
WebSocketサポート
ミドルウェアやテンプレートも使えるミニWebフレームワーク的な機能

▼クライアントの使い方
import aiohttp
import asyncio

async def fetch():
async with aiohttp.ClientSession() as session:
async with session.get(‘https://httpbin.org/get’) as resp:
print(await resp.text())

asyncio.run(fetch())

▼サーバの使い方
from aiohttp import web

async def handle(request):
return web.Response(text=”Hello, aiohttp!”)

app = web.Application()
app.add_routes([web.get(‘/’, handle)])

web.run_app(app)

▼インストール
pip install aiohttp

L 315. httpx(HTTPクライアント)
httpx は、 requests ライクなAPIを持ちつつ、非同期(async/await)にも対応したHTTPクライアントです。

▼主な機能
requestsと似た使い方(同期)
非同期リクエスト(httpx.AsyncClient)
HTTP/1.1, HTTP/2 両対応
Cookie, Session, 認証、タイムアウト、プロキシなど高度な機能

▼同期の使い方
import httpx

response = httpx.get(‘https://httpbin.org/get’)
print(response.status_code)
print(response.json())

▼非同期の使い方
import httpx
import asyncio

async def main():
async with httpx.AsyncClient() as client:
response = await client.get(‘https://httpbin.org/get’)
print(response.text)

asyncio.run(main())

▼インストール
pip install httpx

Comments

Copied title and URL