-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_test_residential_proxy.py
More file actions
73 lines (57 loc) · 2.42 KB
/
Copy path05_test_residential_proxy.py
File metadata and controls
73 lines (57 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
Health-check a generated residential proxy.
Routes a request through the proxy to https://ipapi.co/json/ and reports:
- exit IP and reverse DNS
- country, region, city, ASN/org
- round-trip latency
If the reported country does not match your targeting, something is wrong
(targeting code mistake, country out of stock, sub-user out of bandwidth).
"""
import time
import requests
from proxyshard import (
ProxyshardClient, build_residential_username, build_proxy_url, RELAY_HOSTS,
)
def probe(proxy_url: str, timeout: int = 20) -> dict:
start = time.perf_counter()
r = requests.get(
"https://ipapi.co/json/",
proxies={"http": proxy_url, "https": proxy_url},
timeout=timeout,
)
latency_ms = (time.perf_counter() - start) * 1000
r.raise_for_status()
data = r.json()
data["_latency_ms"] = round(latency_ms)
return data
def main() -> None:
client = ProxyshardClient()
PROXY_TYPE = "premium"
COUNTRY = "de"
subs = client.residential_subusers(proxy_type=PROXY_TYPE, limit=1)
if not subs:
raise SystemExit(f"No {PROXY_TYPE} sub-users — create one first.")
username = build_residential_username(PROXY_TYPE, country=COUNTRY, sticky=True)
proxy_url = build_proxy_url(username, subs[0]["proxy_password"], host=RELAY_HOSTS["eu"])
print(f"Testing {proxy_url.split('@')[1]} (country={COUNTRY})\n")
try:
info = probe(proxy_url)
except requests.RequestException as e:
print(f"FAILED: {e}\n")
print("Possible causes:")
print(" 1. Stale sticky session: the exit device left the pool. Generate a new sid.")
print(" 2. Sub-user bandwidth exhausted: check 02_list_residential_subusers.py.")
print(" 3. Country out of stock: try a neighbouring country or country='any'.")
print(" 4. Still stuck? Reach us on live chat at https://proxyshard.com and we'll help.")
return
expected_ok = info.get("country_code", "").lower() == COUNTRY.lower()
print(f" IP: {info.get('ip')}")
print(f" Country: {info.get('country_name')} ({info.get('country_code')}) "
f"{'OK' if expected_ok else 'MISMATCH'}")
print(f" Region: {info.get('region')}")
print(f" City: {info.get('city')}")
print(f" Org: {info.get('org')}")
print(f" ASN: {info.get('asn')}")
print(f" Latency: {info['_latency_ms']} ms")
if __name__ == "__main__":
main()