|
import ssl |
|
from starlette.applications import Starlette |
|
from starlette.routing import Route |
|
from starlette.responses import PlainTextResponse |
|
import httpx |
|
import aiohttp |
|
|
|
|
|
URL = "https://localhost:8000" |
|
|
|
ssl_context = ssl.create_default_context(cafile="client.pem") |
|
session = aiohttp.ClientSession() |
|
client = httpx.AsyncClient(verify="client.pem") |
|
|
|
|
|
async def index(request): |
|
return PlainTextResponse("world") |
|
|
|
|
|
async def aiohttp_single(request): |
|
async with aiohttp.ClientSession() as session: |
|
async with session.get(URL, ssl=ssl_context) as r: |
|
return _response(await r.text()) |
|
|
|
|
|
async def aiohttp_session(request): |
|
async with session.get(URL, ssl=ssl_context) as r: |
|
return _response(await r.text()) |
|
|
|
|
|
async def httpx_single(request): |
|
async with httpx.AsyncClient() as client: |
|
r = await client.get(URL) |
|
return _response(r.text) |
|
|
|
|
|
async def httpx_client(request): |
|
r = await client.get(URL) |
|
return _response(r.text) |
|
|
|
|
|
def _response(name): |
|
return PlainTextResponse("Hello, " + name) |
|
|
|
|
|
routes = [ |
|
Route("/", endpoint=index), |
|
Route("/aiohttp/single", endpoint=aiohttp_single), |
|
Route("/aiohttp/session", endpoint=aiohttp_session), |
|
Route("/httpx/single", endpoint=httpx_single), |
|
Route("/httpx/client", endpoint=httpx_client), |
|
] |
|
|
|
|
|
app = Starlette(routes=routes) |
|
|
|
|
|
if __name__ == "__main__": |
|
import uvicorn |
|
|
|
uvicorn.run( |
|
app, |
|
host="localhost", |
|
port=8000, |
|
ssl_keyfile="server.key", |
|
ssl_certfile="server.pem", |
|
) |