蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

用表单、curl 和浏览器核对真实 HTTP

PYTHONWindows 本机隔离环境,CPython 3.12、Flask 3.1.3、Werkzeug 3.1.9、Flask-SQLAlchemy 3.1.1wire.py
wire.py113 行
from pathlib import Path
from uuid import uuid4

from flask import Blueprint,abort,current_app,make_response,redirect,render_template,request,url_for
from werkzeug.utils import secure_filename


wire=Blueprint("wire",__name__)


@wire.get("/inspect")
def inspect_request():
    return dict(method=request.method,path=request.path,url=request.url,host=request.host,
                client=request.remote_addr,agent=request.headers.get("User-Agent"),
                theme=request.cookies.get("theme"))


@wire.get("/search")
def search():
    if request.args.get("require")=="1":
        request.args["keyword"]
    return dict(keyword=request.args.get("keyword",""),tags=request.args.getlist("tag"),
                page=request.args.get("page",1,type=int))


@wire.route("/form",methods=["GET","POST"])
def receive_form():
    if request.method=="GET":
        return render_template("wire-form.html")
    name=request.form.get("name","").strip()
    note=request.form.get("note","").strip()
    if not name or not note:
        return {"error":"name and note are required"},400
    return render_template("wire-result.html",name=name,note=note),201


@wire.post("/json")
def receive_json():
    data=request.json
    if not isinstance(data,dict) or not isinstance(data.get("name"),str):
        return {"error":"object with a name required"},400
    return {"name":data["name"]},201


@wire.route("/upload",methods=["GET","POST"])
def upload():
    if request.method=="GET":
        return render_template("upload.html")
    part=request.files.get("sample")
    if not part or not part.filename:
        return {"error":"select a file"},400
    name=secure_filename(part.filename)
    if not name:
        return {"error":"choose a usable filename"},400
    directory=Path(current_app.config["UPLOAD_FOLDER"]).resolve()
    directory.mkdir(parents=True,exist_ok=True)
    target=directory/(uuid4().hex+"-"+name)
    with target.open("xb") as stream:
        part.save(stream)
    return {"name":name,"bytes":target.stat().st_size},201


@wire.get("/dictionary")
def dictionary():
    return {"station":"harbor"}


@wire.get("/list")
def records():
    return ["tide","wind"]


@wire.get("/tuple/<mode>")
def tuple_variants(mode):
    if mode=="status":
        return "暂未找到",404
    if mode=="headers":
        return "有效",{"X-Station":"harbor"}
    if mode=="both":
        return {"stored":True},201,{"X-Station":"harbor"}
    abort(404)


@wire.get("/moved/<mode>")
def moved(mode):
    target=url_for("pages.home")
    if mode=="default":
        return redirect(target)
    if mode=="permanent":
        return redirect(target,code=301)
    abort(404)


@wire.post("/moved")
def after_post():
    return redirect(url_for("pages.home"),code=303)


@wire.get("/reject/<mode>")
def reject(mode):
    if mode=="private":
        abort(403,description="This is a local permission fixture")
    abort(404)


@wire.get("/cookies")
def set_cookies():
    response=make_response("本机偏好已设置")
    response.set_cookie("theme","dark",max_age=3600,samesite="Lax",httponly=True)
    response.set_cookie("locale","zh-CN",samesite="Lax")
    response.headers["X-Station"]="harbor"
    return response
本例需在 Windows 本机隔离环境,CPython 3.12、Flask 3.1.3、Werkzeug 3.1.9、Flask-SQLAlchemy 3.1.1 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows 本机隔离环境,CPython 3.12、Flask 3.1.3、Werkzeug 3.1.9、Flask-SQLAlchemy 3.1.1

入口文件:projects/flask-harbor-http/harbor/wire.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 wire.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows 本机隔离环境,CPython 3.12、Flask 3.1.3、Werkzeug 3.1.9、Flask-SQLAlchemy 3.1.1。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Flask

课程关联工程包(登记关联4章) · 含当前章节或实例 · mbskycom202611a489.zip

下载课程关联工程包:Flask

课程关联工程包(登记关联32章) · 含当前章节或实例 · mbskycom202611a482.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习