MCPcopy Create free account
hub / github.com/ZyphrZero/z.ai2api_python / _build_dynamic_headers

Function _build_dynamic_headers

app/utils/guest_session_pool.py:73–138  ·  view source on GitHub ↗

生成匿名访客鉴权所需浏览器请求头。

(chat_id: str = "")

Source from the content-addressed store, hash-verified

71
72
73def _build_dynamic_headers(chat_id: str = "") -> Dict[str, str]:
74 """生成匿名访客鉴权所需浏览器请求头。"""
75 browser_choices = [
76 "chrome",
77 "chrome",
78 "chrome",
79 "edge",
80 "edge",
81 "firefox",
82 "safari",
83 ]
84 browser_type = random.choice(browser_choices)
85 user_agent = get_random_user_agent(browser_type)
86 fe_version = get_latest_fe_version()
87
88 chrome_version = "139"
89 edge_version = "139"
90
91 if "Chrome/" in user_agent:
92 try:
93 chrome_version = user_agent.split("Chrome/")[1].split(".")[0]
94 except Exception:
95 pass
96
97 if "Edg/" in user_agent:
98 try:
99 edge_version = user_agent.split("Edg/")[1].split(".")[0]
100 sec_ch_ua = (
101 f'"Microsoft Edge";v="{edge_version}", '
102 f'"Chromium";v="{chrome_version}", "Not_A Brand";v="24"'
103 )
104 except Exception:
105 sec_ch_ua = (
106 f'"Not_A Brand";v="8", "Chromium";v="{chrome_version}", '
107 f'"Google Chrome";v="{chrome_version}"'
108 )
109 elif "Firefox/" in user_agent:
110 sec_ch_ua = None
111 else:
112 sec_ch_ua = (
113 f'"Not_A Brand";v="8", "Chromium";v="{chrome_version}", '
114 f'"Google Chrome";v="{chrome_version}"'
115 )
116
117 headers = {
118 "Content-Type": "application/json",
119 "Accept": "application/json, text/event-stream",
120 "Connection": "keep-alive",
121 "Cache-Control": "no-cache",
122 "User-Agent": user_agent,
123 "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
124 "X-FE-Version": fe_version,
125 "Origin": "https://chat.z.ai",
126 }
127
128 if sec_ch_ua:
129 headers["sec-ch-ua"] = sec_ch_ua
130 headers["sec-ch-ua-mobile"] = "?0"

Callers 2

_create_sessionMethod · 0.85
_delete_all_chatsMethod · 0.85

Calls 2

get_random_user_agentFunction · 0.90
get_latest_fe_versionFunction · 0.90

Tested by

no test coverage detected