1. urllib 发起 GET
from urllib.request import urlopen
with urlopen("https://httpbin.org/get", timeout=10) as resp:
body = resp.read().decode("utf-8")
print(body[:200])
第三方库 requests 更常用:pip install requests。
2. 极简 FastAPI 服务
pip install fastapi uvicorn
from fastapi import FastAPI
app = FastAPI()
@app.get("/ping")
def ping() -> dict[str, str]:
return {"status": "ok"}
uvicorn main:app --reload
3. 仅标准库:http.server
python3 -m http.server 8000
适合静态文件调试,不适合生产 API。
📋 本章要点
客户端用 urllib/requests;服务端小项目可用 FastAPI + uvicorn。
评论加载中...