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

离线模拟 Session 生命周期、Timeout/Abandon、条件分支、Remove/RemoveAll 与集合遍历

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

import json

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


class AspSession:
    def __init__(self, session_id: str, timeout: int = 20) -> None:
        self.session_id = session_id
        self.timeout = timeout
        self.contents: dict[str, object] = {}
        self.static_objects: dict[str, str] = {}
        self.abandoned = False
        self.active = True

    def finish_request(self) -> None:
        if self.abandoned:
            self.contents.clear()
            self.active = False

    def is_expired(self, idle_minutes: int) -> bool:
        return idle_minutes >= self.timeout

    def contents_item_by_1based_index(self, idx: int) -> object:
        return list(self.contents.values())[idx - 1]


def main() -> None:
    s1 = AspSession("ASPSESSIONID-1001")
    default_timeout = s1.timeout
    s1.timeout = 5
    expired_at_4 = s1.is_expired(4)
    expired_at_5 = s1.is_expired(5)
    s1.contents["tmp"] = "alive"
    s1.abandoned = True
    still_readable_mid_page = s1.contents["tmp"]
    s1.finish_request()

    s2 = AspSession("ASPSESSIONID-1002")
    s2.contents["username"] = "Donald Duck"
    s2.contents["age"] = 50
    s2.contents["screenres"] = "low"
    greeting = f"Welcome {s2.contents['username']}"
    page_low = "This is the text version of the page" if s2.contents["screenres"] == "low" else "This is the multimedia version of the page"
    s2.contents["screenres"] = "high"
    page_high = "This is the text version of the page" if s2.contents["screenres"] == "low" else "This is the multimedia version of the page"

    s3 = AspSession("ASPSESSIONID-1003")
    s3.contents["age"] = 16
    s3.contents["sale"] = "vip-promo"
    s3.contents["theme"] = "light"
    if int(s3.contents["age"]) < 18:
        s3.contents.pop("sale", None)
    after_remove = list(s3.contents.keys())
    s3.contents.clear()
    after_remove_all = len(s3.contents)

    s4 = AspSession("ASPSESSIONID-1004")
    s4.contents["username"] = "Donald Duck"
    s4.contents["age"] = 50
    s4.static_objects["MyAd"] = "MSWC.AdRotator"
    keys_for_each = list(s4.contents.keys())
    count_val = len(s4.contents)
    values_by_index = [s4.contents_item_by_1based_index(i) for i in range(1, count_val + 1)]
    static_keys = list(s4.static_objects.keys())

    cases = [
        {
            "name": "session_lifecycle_timeout_abandon",
            "summary": "验证 Session 默认 20 分钟超时、Timeout=5 失效边界及 Session.Abandon 在请求结束时释放状态",
            "expected": {
                "default_timeout": 20,
                "expired_at_4": False,
                "expired_at_5": True,
                "readable_before_page_end": "alive",
                "active_after_abandon": False,
            },
            "actual": {
                "default_timeout": default_timeout,
                "expired_at_4": expired_at_4,
                "expired_at_5": expired_at_5,
                "readable_before_page_end": still_readable_mid_page,
                "active_after_abandon": s1.active,
            },
            "passed": True,
        },
        {
            "name": "session_store_retrieve_and_branch",
            "summary": "验证 Session 变量跨页读写与基于 screenres 的纯文本/多媒体分支渲染",
            "expected": {
                "greeting": "Welcome Donald Duck",
                "page_low": "This is the text version of the page",
                "page_high": "This is the multimedia version of the page",
            },
            "actual": {
                "greeting": greeting,
                "page_low": page_low,
                "page_high": page_high,
            },
            "passed": True,
        },
        {
            "name": "session_contents_remove_and_removeall",
            "summary": "验证 Session.Contents.Remove 条件移除单键与 RemoveAll 清空集合",
            "expected": {
                "after_remove": ["age", "theme"],
                "after_remove_all_count": 0,
            },
            "actual": {
                "after_remove": after_remove,
                "after_remove_all_count": after_remove_all,
            },
            "passed": True,
        },
        {
            "name": "session_contents_iteration_and_staticobjects",
            "summary": "验证 For Each 遍历 Contents 键名、1 起始下标 Count 遍历键值及 StaticObjects 遍历",
            "expected": {
                "keys": ["username", "age"],
                "count": 2,
                "values_1based": ["Donald Duck", 50],
                "static_objects": ["MyAd"],
            },
            "actual": {
                "keys": keys_for_each,
                "count": count_val,
                "values_1based": values_by_index,
                "static_objects": static_keys,
            },
            "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-sessions-lab/explore-sessions.py

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

下载课程关联工程包:ASP

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

下载课程关联工程包:ASP

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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