fromozu commited on
Commit
e9d6d40
·
verified ·
1 Parent(s): 216f552

Upload hf_backend/test_monitor_network.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. hf_backend/test_monitor_network.py +85 -0
hf_backend/test_monitor_network.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ 监控网络请求,看是否能捕获到 EPUB 下载请求
4
+ """
5
+
6
+ from playwright.sync_api import sync_playwright
7
+ import time
8
+
9
+ MD5 = "d94c20d1364af9b484949659398c4062"
10
+ SLOW_URL = f"https://annas-archive.gl/slow_download/{MD5}/0/3"
11
+
12
+ def monitor_network():
13
+ """监控网络请求"""
14
+ print(f"目标: {SLOW_URL}\n")
15
+
16
+ epub_requests = []
17
+ all_requests = []
18
+
19
+ with sync_playwright() as p:
20
+ browser = p.chromium.launch(headless=False)
21
+ context = browser.new_context(
22
+ user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
23
+ )
24
+ page = context.new_page()
25
+
26
+ # 监控请求
27
+ def on_request(request):
28
+ url = request.url
29
+ if '.epub' in url.lower():
30
+ print(f" [EPUB请求] {url}")
31
+ epub_requests.append(url)
32
+ elif 'download' in url.lower() or 'file' in url.lower():
33
+ all_requests.append(url)
34
+
35
+ def on_response(response):
36
+ url = response.url
37
+ content_type = response.headers.get('content-type', '')
38
+ if '.epub' in url.lower():
39
+ print(f" [EPUB响应] {url} - {content_type}")
40
+ epub_requests.append(url)
41
+
42
+ page.on("request", on_request)
43
+ page.on("response", on_response)
44
+
45
+ print("访问 slow_download...")
46
+ page.goto(SLOW_URL, timeout=120000, wait_until="domcontentloaded")
47
+ print(f"初始标题: {page.title()}")
48
+
49
+ # 等待最多60秒
50
+ for i in range(60):
51
+ time.sleep(1)
52
+ try:
53
+ title = page.title()
54
+ url = page.url
55
+
56
+ if url.lower().endswith('.epub'):
57
+ print(f"\n在第{i+1}秒发现EPUB URL: {url}")
58
+ browser.close()
59
+ return url
60
+
61
+ if title != "DDoS-Guard":
62
+ print(f"\n在第{i+1}秒: 标题={title}, URL={url}")
63
+
64
+ if (i + 1) % 15 == 0:
65
+ print(f" {i+1}秒... 标题={title}")
66
+ except Exception as e:
67
+ print(f" {i+1}秒... (导航中)")
68
+ continue
69
+
70
+ print(f"\n最终URL: {page.url}")
71
+ print(f"\n捕获到的EPUB相关请求: {epub_requests}")
72
+ print(f"捕获到的下载相关请求: {all_requests[:5]}")
73
+
74
+ browser.close()
75
+ return epub_requests[0] if epub_requests else None
76
+
77
+ if __name__ == "__main__":
78
+ print("=" * 60)
79
+ print("监控网络请求")
80
+ print("=" * 60)
81
+ result = monitor_network()
82
+ if result:
83
+ print(f"\n成功: {result}")
84
+ else:
85
+ print("\n未捕获到EPUB请求")