akshare-proxy-patch 0.2.2__tar.gz → 0.2.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of akshare-proxy-patch might be problematic. Click here for more details.
- {akshare_proxy_patch-0.2.2 → akshare_proxy_patch-0.2.3}/PKG-INFO +1 -1
- {akshare_proxy_patch-0.2.2 → akshare_proxy_patch-0.2.3}/akshare_proxy_patch/__init__.py +40 -24
- {akshare_proxy_patch-0.2.2 → akshare_proxy_patch-0.2.3}/akshare_proxy_patch.egg-info/PKG-INFO +1 -1
- {akshare_proxy_patch-0.2.2 → akshare_proxy_patch-0.2.3}/pyproject.toml +1 -1
- {akshare_proxy_patch-0.2.2 → akshare_proxy_patch-0.2.3}/setup.py +1 -1
- {akshare_proxy_patch-0.2.2 → akshare_proxy_patch-0.2.3}/LICENSE +0 -0
- {akshare_proxy_patch-0.2.2 → akshare_proxy_patch-0.2.3}/README.md +0 -0
- {akshare_proxy_patch-0.2.2 → akshare_proxy_patch-0.2.3}/akshare_proxy_patch.egg-info/SOURCES.txt +0 -0
- {akshare_proxy_patch-0.2.2 → akshare_proxy_patch-0.2.3}/akshare_proxy_patch.egg-info/dependency_links.txt +0 -0
- {akshare_proxy_patch-0.2.2 → akshare_proxy_patch-0.2.3}/akshare_proxy_patch.egg-info/requires.txt +0 -0
- {akshare_proxy_patch-0.2.2 → akshare_proxy_patch-0.2.3}/akshare_proxy_patch.egg-info/top_level.txt +0 -0
- {akshare_proxy_patch-0.2.2 → akshare_proxy_patch-0.2.3}/setup.cfg +0 -0
|
@@ -2,11 +2,10 @@ import time
|
|
|
2
2
|
import threading
|
|
3
3
|
import requests
|
|
4
4
|
from requests.adapters import HTTPAdapter
|
|
5
|
-
from copy import deepcopy
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
__version__ = "0.2.3"
|
|
7
|
+
# 备份 Session 的原始 request 方法,这是所有 requests.get/post 的最终入口
|
|
8
|
+
_original_request = requests.Session.request
|
|
10
9
|
_auth_session = requests.Session()
|
|
11
10
|
|
|
12
11
|
|
|
@@ -23,14 +22,12 @@ _cache = AuthCache()
|
|
|
23
22
|
|
|
24
23
|
def get_auth_config_with_cache(auth_url, auth_token):
|
|
25
24
|
now = time.time()
|
|
26
|
-
|
|
27
|
-
# 检查缓存是否有效 (双重检查锁前置)
|
|
25
|
+
# 1. 检查缓存是否有效
|
|
28
26
|
if _cache.data and now < _cache.expire_at:
|
|
29
27
|
return _cache.data
|
|
30
28
|
|
|
31
|
-
# 缓存失效,加锁更新
|
|
29
|
+
# 2. 缓存失效,加锁更新
|
|
32
30
|
with _cache.lock:
|
|
33
|
-
# 再次检查,防止多个线程同时穿透锁
|
|
34
31
|
if _cache.data and now < _cache.expire_at:
|
|
35
32
|
return _cache.data
|
|
36
33
|
|
|
@@ -53,48 +50,67 @@ def get_auth_config_with_cache(auth_url, auth_token):
|
|
|
53
50
|
|
|
54
51
|
|
|
55
52
|
def install_patch(auth_ip, auth_token):
|
|
56
|
-
def
|
|
57
|
-
#
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
d in url
|
|
53
|
+
def patched_request(self, method, url, **kwargs):
|
|
54
|
+
# 排除非目标域名
|
|
55
|
+
is_target = any(
|
|
56
|
+
d in (url or "")
|
|
61
57
|
for d in [
|
|
62
58
|
"fund.eastmoney.com",
|
|
63
59
|
"push2.eastmoney.com",
|
|
64
60
|
"push2his.eastmoney.com",
|
|
65
61
|
]
|
|
66
|
-
)
|
|
67
|
-
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
if not is_target:
|
|
65
|
+
return _original_request(self, method, url, **kwargs)
|
|
68
66
|
|
|
69
67
|
auth_url = f"http://{auth_ip}:47001/api/akshare-auth"
|
|
70
68
|
|
|
71
|
-
|
|
69
|
+
# 重试逻辑
|
|
70
|
+
for _ in range(10):
|
|
72
71
|
auth_res = get_auth_config_with_cache(auth_url, auth_token)
|
|
73
72
|
if not auth_res:
|
|
74
73
|
time.sleep(0.5)
|
|
75
74
|
continue
|
|
76
75
|
|
|
77
|
-
|
|
78
|
-
|
|
76
|
+
# 处理 Headers:确保不破坏业务代码传入的 headers
|
|
77
|
+
headers = kwargs.get("headers") or {}
|
|
78
|
+
headers["User-Agent"] = auth_res["ua"]
|
|
79
|
+
headers["Cookie"] = (
|
|
79
80
|
f"nid18={auth_res['nid18']}; nid18_create_time={auth_res['nid18_create_time']}"
|
|
80
81
|
)
|
|
81
|
-
|
|
82
|
-
# 动态注入代理
|
|
82
|
+
kwargs["headers"] = headers
|
|
83
83
|
kwargs["proxies"] = {
|
|
84
84
|
"http": auth_res["proxy"],
|
|
85
85
|
"https": auth_res["proxy"],
|
|
86
86
|
}
|
|
87
|
-
|
|
87
|
+
|
|
88
|
+
if "timeout" not in kwargs:
|
|
89
|
+
kwargs["timeout"] = 8
|
|
88
90
|
|
|
89
91
|
try:
|
|
90
|
-
|
|
92
|
+
# 调用原始 request 方法
|
|
93
|
+
resp = _original_request(self, method, url, **kwargs)
|
|
91
94
|
if resp.ok:
|
|
92
95
|
return resp
|
|
96
|
+
|
|
97
|
+
# 如果遇到 403 等可能是代理失效
|
|
98
|
+
if resp.status_code == 403:
|
|
99
|
+
with _cache.lock:
|
|
100
|
+
_cache.expire_at = 0
|
|
93
101
|
except Exception:
|
|
102
|
+
# 网络异常直接使缓存失效,强制下一个请求换 IP
|
|
94
103
|
with _cache.lock:
|
|
95
104
|
_cache.expire_at = 0
|
|
96
105
|
time.sleep(0.1)
|
|
97
106
|
|
|
98
|
-
|
|
107
|
+
# 最终尝试
|
|
108
|
+
return _original_request(self, method, url, **kwargs)
|
|
109
|
+
|
|
110
|
+
# 关键:全局替换 Session 的 request 入口
|
|
111
|
+
requests.Session.request = patched_request
|
|
112
|
+
|
|
99
113
|
|
|
100
|
-
|
|
114
|
+
# 使用示例
|
|
115
|
+
# install_patch("你的IP", "你的TOKEN")
|
|
116
|
+
# requests.get("http://fund.eastmoney.com/...")
|
|
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
|
|
|
5
5
|
|
|
6
6
|
setup(
|
|
7
7
|
name="akshare-proxy-patch", # 包名,用户 pip install 时的名称
|
|
8
|
-
version="0.2.
|
|
8
|
+
version="0.2.3", # 版本号
|
|
9
9
|
author="cheapproxy.net",
|
|
10
10
|
author_email="emezxyj1@gmail.com",
|
|
11
11
|
description="AkShare 报错全局Hook",
|
|
File without changes
|
|
File without changes
|
{akshare_proxy_patch-0.2.2 → akshare_proxy_patch-0.2.3}/akshare_proxy_patch.egg-info/SOURCES.txt
RENAMED
|
File without changes
|
|
File without changes
|
{akshare_proxy_patch-0.2.2 → akshare_proxy_patch-0.2.3}/akshare_proxy_patch.egg-info/requires.txt
RENAMED
|
File without changes
|
{akshare_proxy_patch-0.2.2 → akshare_proxy_patch-0.2.3}/akshare_proxy_patch.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|