Skip to content

Instantly share code, notes, and snippets.

@denisxab
Created December 16, 2021 21:44
Show Gist options
  • Save denisxab/9764c5b5591176544d347c113e76c18e to your computer and use it in GitHub Desktop.
Save denisxab/9764c5b5591176544d347c113e76c18e to your computer and use it in GitHub Desktop.
Проверка доступности прокси
from typing import NamedTuple, Optional
from requests import get
class TypeProxy(NamedTuple):
type_socks: str # socks5/socks4
ip: str
port: int
@staticmethod
def to_requests_proxies(self) -> dict:
"""
`requests.get(proxies=Proxy.to_requests_proxies(obj_Proxy))`
"""
return {
"https": f"{self.type_socks}://{self.ip}:{self.port}",
"http" : f"{self.type_socks}://{self.ip}:{self.port}"
}
def __repr__(self):
return f"{self.to_requests_proxies(self)}"
class CheckProxy:
def __init__(self, proxyArr: list[TypeProxy], url: str, timeout: int = 8):
self.proxyArr: list[TypeProxy] = proxyArr
self.valued_proxy_list: Optional[list[tuple[TypeProxy, int]]] = self.__check_proxy(url, timeout)
@staticmethod
def is_valued_proxy(obj_Proxy: TypeProxy, url: str, timeout: int = 8) -> Optional[int]:
try:
response = get(url,
proxies=TypeProxy.to_requests_proxies(obj_Proxy),
timeout=timeout)
return response.status_code
except Exception as detail:
print(f"{detail}\n{TypeProxy.to_requests_proxies(obj_Proxy)}")
return None
def __check_proxy(self, url: str, timeout: int) -> Optional[list[tuple[TypeProxy, int]]]:
res: list[tuple[TypeProxy, int]] = []
for _x in self.proxyArr:
tmp = self.is_valued_proxy(_x, url, timeout)
if tmp:
res.append((_x, tmp))
if res:
return res
return None
if __name__ == '__main__':
res = CheckProxy([
TypeProxy(type_socks="socks5", ip="223.166.135.90", port=1080),
], url='https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/socks5.txt', timeout=8)
print(res.valued_proxy_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment