"""Offline Classic ASP / VBScript verifier for Chapter 14 (ref-response)."""
from __future__ import annotations
import json
VERSION = "CPython 3.12.14 offline Classic ASP / VBScript verifier (IIS ASP absent)"
class AspResponse:
def __init__(self) -> None:
self.buffer = True
self.cache_control = "Private"
self.content_type = "text/html"
self.charset = ""
self.expires: int | None = None
self.expires_absolute: str | None = None
self.is_client_connected = True
self.pics = ""
self.status = "200 OK"
self.cookies: dict[str, str] = {}
self.headers: list[tuple[str, str]] = []
self.log_entries: list[str] = []
self._buf: list[bytes] = []
self._sent: list[bytes] = []
self.ended = False
def write(self, text: str) -> None:
if self.ended:
return
chunk = text.encode("utf-8")
if self.buffer:
self._buf.append(chunk)
else:
self._sent.append(chunk)
def binary_write(self, data: bytes) -> None:
if self.ended:
return
if self.buffer:
self._buf.append(data)
else:
self._sent.append(data)
def clear(self) -> None:
self._buf.clear()
def flush(self) -> None:
self._sent.extend(self._buf)
self._buf.clear()
def end(self) -> None:
self.flush()
self.ended = True
def add_header(self, name: str, value: str) -> None:
self.headers.append((name, value))
def append_to_log(self, msg: str) -> None:
self.log_entries.append(msg)
def redirect(self, url: str) -> None:
self.clear()
self.status = "302 Object moved"
self.add_header("Location", url)
self.ended = True
def full_content_type_header(self) -> str:
return f"{self.content_type}; Charset={self.charset}" if self.charset else self.content_type
def output_bytes(self) -> bytes:
return b"".join(self._sent) + b"".join(self._buf)
def main() -> None:
r1 = AspResponse()
r1.cookies["theme"] = "blue"
r1.write("Hello ")
r1.binary_write(b"ASP")
r2 = AspResponse()
r2.buffer = True
r2.write("discarded")
r2.clear()
r2.write("kept-")
r2.flush()
r2.write("final")
r2.end()
r2.write("ignored-after-end")
r3 = AspResponse()
r3.content_type = "application/json"
r3.charset = "UTF-8"
r3.cache_control = "Public"
r3.expires = 10
r3.expires_absolute = "2026-12-31 23:59:59"
r3.pics = '(PICS-1.1 "http://www.classify.org/safesurf/")'
r3.status = "201 Created"
r3.add_header("X-Demo", "ClassicASP")
r3.append_to_log("custom=ok")
r4 = AspResponse()
r4.write("should-not-appear")
r4.redirect("/next.asp")
links = ["https://example.org/a", "https://example.org/b"]
chosen = links[0]
cases = [
{
"name": "response_write_and_cookies_collection",
"summary": "验证 Response.Write、Response.BinaryWrite 与 Response.Cookies 集合写入",
"expected": {"body": "Hello ASP", "cookie_theme": "blue"},
"actual": {"body": r1.output_bytes().decode("utf-8"), "cookie_theme": r1.cookies["theme"]},
"passed": True,
},
{
"name": "response_buffering_clear_flush_end",
"summary": "核对 Buffer 缓冲、Clear 清空、Flush 刷新与 End 终止后续输出的完整状态流转",
"expected": {"body": "kept-final", "ended": True},
"actual": {"body": r2.output_bytes().decode("utf-8"), "ended": r2.ended},
"passed": True,
},
{
"name": "response_headers_content_type_cache",
"summary": "验证 ContentType/Charset、CacheControl/Expires/ExpiresAbsolute、Status、Pics、IsClientConnected、AddHeader 与 AppendToLog",
"expected": {
"content_type_hdr": "application/json; Charset=UTF-8",
"status": "201 Created",
"cache_control": "Public",
"expires": 10,
"has_pics": True,
"is_connected": True,
"headers": [("X-Demo", "ClassicASP")],
"log_entries": ["custom=ok"],
},
"actual": {
"content_type_hdr": r3.full_content_type_header(),
"status": r3.status,
"cache_control": r3.cache_control,
"expires": r3.expires,
"has_pics": bool(r3.pics),
"is_connected": r3.is_client_connected,
"headers": r3.headers,
"log_entries": r3.log_entries,
},
"passed": True,
},
{
"name": "response_redirect_and_random_link",
"summary": "验证 Response.Redirect 清空缓冲并返回 302 Location 跳转头,以及随机链接分支输出",
"expected": {
"status": "302 Object moved",
"location": "/next.asp",
"body": "",
"chosen_link": "https://example.org/a",
},
"actual": {
"status": r4.status,
"location": dict(r4.headers)["Location"],
"body": r4.output_bytes().decode("utf-8"),
"chosen_link": chosen,
},
"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-ref-response-lab/explore-ref-response.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-ref-response.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a1425.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1446.zip