Files
proxy-scraper-checker/proxy_scraper_checker/proxy.py
T

83 lines
2.7 KiB
Python
Raw Normal View History

2023-01-02 00:09:55 +03:00
from __future__ import annotations
2024-01-21 17:45:01 +00:00
from io import StringIO
2023-01-02 00:09:55 +03:00
from time import perf_counter
2024-07-09 03:08:30 +03:00
from typing import TYPE_CHECKING
2023-01-02 00:09:55 +03:00
2024-01-21 17:45:01 +00:00
import attrs
import orjson
2024-01-21 17:45:01 +00:00
from aiohttp import ClientSession
2024-07-09 03:08:30 +03:00
from aiohttp_socks import ProxyConnector
2023-01-02 00:09:55 +03:00
from proxy_scraper_checker.http import HEADERS, SSL_CONTEXT, get_cookie_jar
2024-11-22 14:32:29 +03:00
from proxy_scraper_checker.parsers import parse_ipv4
from proxy_scraper_checker.settings import CheckWebsiteType
2024-07-09 03:08:30 +03:00
if TYPE_CHECKING:
from aiohttp_socks import ProxyType
2024-11-22 14:32:29 +03:00
from proxy_scraper_checker.settings import Settings
2023-01-15 13:05:16 +03:00
2023-02-19 21:44:15 +03:00
2024-01-21 17:45:01 +00:00
@attrs.define(
repr=False,
unsafe_hash=True,
weakref_slot=False,
kw_only=True,
getstate_setstate=False,
match_args=False,
)
class Proxy:
2024-01-21 17:45:01 +00:00
protocol: ProxyType
2023-02-19 21:44:15 +03:00
host: str
port: int
2024-06-03 14:08:42 +05:00
username: str | None
password: str | None
2024-09-18 18:23:01 +03:00
timeout: float | None = attrs.field(default=None, init=False, eq=False)
exit_ip: str | None = attrs.field(default=None, init=False, eq=False)
def is_checked(self) -> bool:
return self.timeout is not None
2023-01-15 13:05:16 +03:00
2024-01-21 17:45:01 +00:00
async def check(self, *, settings: Settings) -> None:
async with settings.semaphore:
2023-01-02 00:09:55 +03:00
start = perf_counter()
2023-06-27 12:04:44 +03:00
connector = ProxyConnector(
2024-01-21 17:45:01 +00:00
proxy_type=self.protocol,
host=self.host,
port=self.port,
username=self.username,
password=self.password,
ssl=SSL_CONTEXT,
2023-06-27 12:04:44 +03:00
)
2024-10-17 14:18:27 +03:00
async with (
ClientSession(
connector=connector,
headers=HEADERS,
cookie_jar=get_cookie_jar(),
raise_for_status=True,
timeout=settings.timeout,
) as session,
session.get(
settings.check_website,
headers=settings.check_website_type.headers,
) as response,
):
2024-01-21 21:46:01 +03:00
content = await response.read()
2023-01-02 00:09:55 +03:00
self.timeout = perf_counter() - start
2024-01-21 17:45:01 +00:00
if settings.check_website_type == CheckWebsiteType.HTTPBIN_IP:
self.exit_ip = parse_ipv4(orjson.loads(content)["origin"])
2024-01-21 17:45:01 +00:00
elif settings.check_website_type == CheckWebsiteType.PLAIN_IP:
2025-03-06 10:04:05 +03:00
self.exit_ip = parse_ipv4(content.decode(response.get_encoding()))
2024-02-04 12:24:12 +03:00
else:
self.exit_ip = None
2023-01-02 00:09:55 +03:00
2024-01-21 17:45:01 +00:00
def as_str(self, *, include_protocol: bool) -> str:
with StringIO() as buf:
if include_protocol:
buf.write(f"{self.protocol.name.lower()}://")
if self.username is not None and self.password is not None:
buf.write(f"{self.username}:{self.password}@")
buf.write(f"{self.host}:{self.port}")
return buf.getvalue()