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

离线模拟 IIS 静态 HTML 与 Classic ASP 引擎请求分发管线

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-intro.py
explore-intro.py99 行
"""Offline Classic ASP / VBScript verifier for Chapter 02 (intro)."""
from __future__ import annotations

import json
import re
from pathlib import PurePosixPath

VERSION = "CPython 3.12.14 offline Classic ASP / VBScript verifier (IIS ASP absent)"


def handle_iis_request(filename: str, content: str, query: dict[str, str] | None = None) -> dict[str, object]:
    """Simulate IIS static HTML pass-through vs Classic ASP engine execution."""
    ext = PurePosixPath(filename).suffix.lower()
    params = query or {}
    if ext in {".html", ".htm"}:
        return {"engine": "static_file_handler", "body": content, "script_executed": False}
    if ext != ".asp":
        return {"engine": "unsupported", "body": "", "script_executed": False}

    def eval_block(match: re.Match[str]) -> str:
        expr = match.group(1).strip()
        if expr.startswith("="):
            rhs = expr[1:].strip()
            if rhs.startswith('Request.QueryString("') and rhs.endswith('")'):
                key = rhs[len('Request.QueryString("'):-2]
                return params.get(key, "")
            if rhs.startswith('"') and rhs.endswith('"'):
                return rhs[1:-1]
        m = re.fullmatch(r'response\.write\s*\(\s*"([^"]*)"\s*\)', expr, re.IGNORECASE)
        if m:
            return m.group(1)
        raise ValueError(f"Unsupported ASP snippet: {expr}")

    body = re.sub(r"<%(.*?)%>", eval_block, content, flags=re.DOTALL)
    return {"engine": "asp.dll", "body": body, "script_executed": True}


def main() -> None:
    raw_doc = "<p>Static: <% Response.Write(\"Dynamic\") %></p>"
    html_res = handle_iis_request("page.html", raw_doc)
    asp_res = handle_iis_request("page.asp", raw_doc)

    mixed_asp = (
        '<data><title>Note</title><msg><% Response.Write("Hello ASP") %></msg></data>'
    )
    mixed_res = handle_iis_request("feed.asp", mixed_asp)

    personal_asp = '<p>Welcome, <%= Request.QueryString("user") %>!</p>'
    personal_res = handle_iis_request("welcome.asp", personal_asp, {"user": "Ada"})

    cases = [
        {
            "name": "html_vs_asp_pipeline",
            "summary": "对比 .html 原样返回与 .asp 经 IIS/ASP 引擎逐行执行后返回纯 HTML",
            "expected": {
                "html_engine": "static_file_handler",
                "html_has_delim": True,
                "asp_engine": "asp.dll",
                "asp_body": "<p>Static: Dynamic</p>",
                "asp_has_delim": False,
            },
            "actual": {
                "html_engine": html_res["engine"],
                "html_has_delim": "<%" in str(html_res["body"]),
                "asp_engine": asp_res["engine"],
                "asp_body": asp_res["body"],
                "asp_has_delim": "<%" in str(asp_res["body"]),
            },
            "passed": True,
        },
        {
            "name": "asp_extension_and_content_mix",
            "summary": "验证 .asp 文件可混合文本、HTML/XML 标签与服务器脚本并仅暴露最终输出",
            "expected": "<data><title>Note</title><msg>Hello ASP</msg></data>",
            "actual": mixed_res["body"],
            "passed": mixed_res["body"] == "<data><title>Note</title><msg>Hello ASP</msg></data>",
        },
        {
            "name": "dynamic_personalization_and_form_reply",
            "summary": "模拟根据表单/查询参数动态定制网页,客户端无需脚本引擎即可显示",
            "expected": {
                "body": "<p>Welcome, Ada!</p>",
                "requires_browser_script": False,
            },
            "actual": {
                "body": personal_res["body"],
                "requires_browser_script": "<script" in str(personal_res["body"]).lower(),
            },
            "passed": True,
        },
    ]
    for c in cases:
        c["passed"] = c["actual"] == c["expected"]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/asp-intro-lab/explore-intro.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-intro.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:ASP

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

下载课程关联工程包:ASP

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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