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

离线模拟 Session 参考接口的 CodePage/LCID/SessionID/Timeout、集合移除、Abandon 与多用户隔离

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

import json

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

LCID_FORMATS = {
    1033: {"locale": "en-US", "currency": "$1,234.50", "date": "12/1/2026"},
    2052: {"locale": "zh-CN", "currency": "¥1,234.50", "date": "2026/12/1"},
}


class SessionRefSim:
    def __init__(self, session_id: int) -> None:
        self.session_id = session_id
        self.code_page = 65001
        self.lcid = 1033
        self.timeout = 20
        self.contents: dict[str, object] = {}
        self.static_objects: dict[str, str] = {"UserPrefs": "Scripting.Dictionary"}
        self.events: list[str] = ["Session_OnStart"]
        self._abandon_pending = False
        self.active = True

    def format_sample(self) -> dict[str, str]:
        return LCID_FORMATS[self.lcid]

    def abandon(self) -> None:
        self._abandon_pending = True

    def end_page(self) -> None:
        if self._abandon_pending and self.active:
            self.contents.clear()
            self.active = False
            self.events.append("Session_OnEnd")


def main() -> None:
    s1 = SessionRefSim(88421001)
    s1.code_page = 65001
    s1.lcid = 2052
    zh_fmt = s1.format_sample()
    s1.lcid = 1033
    en_fmt = s1.format_sample()
    default_timeout = s1.timeout
    s1.timeout = 10

    s1.contents["user"] = "Alice"
    s1.contents["role"] = "editor"
    s1.contents.pop("role")
    after_remove = list(s1.contents.keys())
    s1.contents.clear()
    after_remove_all = len(s1.contents)
    static_keys = list(s1.static_objects.keys())

    s2 = SessionRefSim(88421002)
    s2.contents["token"] = "t1"
    s2.abandon()
    mid_page_active = s2.active
    s2.end_page()

    s3 = SessionRefSim(88421003)
    s3.contents["user"] = "Bob"
    s3.timeout = 30

    cases = [
        {
            "name": "session_properties_codepage_lcid_id_timeout",
            "summary": "核对 CodePage、LCID 区域格式切换(2052/1033)、服务器生成 SessionID 与 Timeout 属性",
            "expected": {
                "code_page": 65001,
                "zh_currency": "¥1,234.50",
                "en_currency": "$1,234.50",
                "session_id": 88421001,
                "default_timeout": 20,
                "updated_timeout": 10,
            },
            "actual": {
                "code_page": s1.code_page,
                "zh_currency": zh_fmt["currency"],
                "en_currency": en_fmt["currency"],
                "session_id": s1.session_id,
                "default_timeout": default_timeout,
                "updated_timeout": s1.timeout,
            },
            "passed": True,
        },
        {
            "name": "session_contents_and_staticobjects",
            "summary": "验证 Contents.Remove、Contents.RemoveAll() 与 StaticObjects 集合独立保留",
            "expected": {
                "after_remove": ["user"],
                "after_remove_all": 0,
                "static_objects": ["UserPrefs"],
            },
            "actual": {
                "after_remove": after_remove,
                "after_remove_all": after_remove_all,
                "static_objects": static_keys,
            },
            "passed": True,
        },
        {
            "name": "session_abandon_and_events",
            "summary": "验证 Session_OnStart、Abandon 延迟到页面结束时销毁并触发 Session_OnEnd",
            "expected": {
                "active_mid_page": True,
                "active_after_page_end": False,
                "events": ["Session_OnStart", "Session_OnEnd"],
            },
            "actual": {
                "active_mid_page": mid_page_active,
                "active_after_page_end": s2.active,
                "events": s2.events,
            },
            "passed": True,
        },
        {
            "name": "session_isolation_between_users",
            "summary": "验证不同用户的 SessionID、Contents 与 Timeout 彼此隔离",
            "expected": {
                "distinct_ids": True,
                "s1_timeout": 10,
                "s3_timeout": 30,
                "s3_user": "Bob",
            },
            "actual": {
                "distinct_ids": s1.session_id != s3.session_id,
                "s1_timeout": s1.timeout,
                "s3_timeout": s3.timeout,
                "s3_user": s3.contents["user"],
            },
            "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-session-lab/explore-ref-session.py

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

下载课程关联工程包:ASP

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

下载课程关联工程包:ASP

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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