From a0edcb0c4ce274b22ac0a8dcc7ea54225bcfdd4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E5=A4=A7=E5=B8=85Aiden?= <38374506+dashuaixu@users.noreply.github.com> Date: Wed, 30 Aug 2023 21:49:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=A7=81=E4=BA=BA=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E6=B1=A0=E5=9C=BA=E6=99=AF=E4=B8=AD=EF=BC=8C=E5=B8=A6?= =?UTF-8?q?=E8=BA=AB=E4=BB=BD=E8=AE=A4=E8=AF=81=E7=9A=84=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81=20(#198)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 增加私人代理池场景中,带身份认证的代理的支持 * 修复忘记修改部分 --------- Co-authored-by: dashuai xu --- proxypool/utils/proxy.py | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/proxypool/utils/proxy.py b/proxypool/utils/proxy.py index 294033fc..79cc27fb 100644 --- a/proxypool/utils/proxy.py +++ b/proxypool/utils/proxy.py @@ -5,7 +5,10 @@ def is_valid_proxy(data): """ check this string is within proxy format """ - if data.__contains__(':'): + if is_auth_proxy(data): + host, port = extract_auth_proxy(data) + return is_ip_valid(host) and is_port_valid(port) + elif data.__contains__(':'): ip = data.split(':')[0] port = data.split(':')[1] return is_ip_valid(ip) and is_port_valid(port) @@ -17,6 +20,8 @@ def is_ip_valid(ip): """ check this string is within ip format """ + if is_auth_proxy(ip): + ip = ip.split('@')[1] a = ip.split('.') if len(a) != 4: return False @@ -48,9 +53,36 @@ def convert_proxy_or_proxies(data): # skip invalid item item = item.strip() if not is_valid_proxy(item): continue - host, port = item.split(':') + if is_auth_proxy(item): + host, port = extract_auth_proxy(item) + else: + host, port = item.split(':') result.append(Proxy(host=host, port=int(port))) return result if isinstance(data, str) and is_valid_proxy(data): - host, port = data.split(':') + if is_auth_proxy(data): + host, port = extract_auth_proxy(data) + else: + host, port = data.split(':') return Proxy(host=host, port=int(port)) + + +def is_auth_proxy(data: str) -> bool: + return '@' in data + + +def extract_auth_proxy(data: str) -> (str, str): + """ + extract host and port from a proxy with authentication + """ + auth = data.split('@')[0] + ip_port = data.split('@')[1] + ip = ip_port.split(':')[0] + port = ip_port.split(':')[1] + host = auth + '@' + ip + return host, port + + +if __name__ == '__main__': + proxy = 'test1234:test5678.@117.68.216.212:32425' + print(extract_auth_proxy(proxy))