Python · urllib3
urllib3 through a ProxyGuys proxy
urllib3 is the connection layer underneath requests and many SDKs. Using it directly gives you control over pools, retries and timeouts, but it does less on your behalf: the proxy login has to be handed over explicitly, as a header for the HTTP port or as arguments for the SOCKS5 port. Both are shown below, followed by a script that changes the IP and waits for the new one. The urllib3 manual is at urllib3.readthedocs.io.
Setup
pip install urllib3
# SOCKS5 support comes from PySocks
pip install "urllib3[socks]"The proxy has to be started: a new one stays Pending until you press Start proxy on My Proxies. On that page, the Connection column lists an "HTTP proxy" and a "SOCKS5 proxy" address, the same host with two different ports. Credentials has the username (Copy username) and the masked password (reveal it with Show / hide, copy it with Copy password). Export them so the code can read them:
export PROXY_HOST="HOST"
export PROXY_HTTP_PORT="HTTP_PORT"
export PROXY_SOCKS5_PORT="SOCKS5_PORT"
export PROXY_USER="USERNAME"
export PROXY_PASS="PASSWORD"
# PowerShell: $env:PROXY_HOST = "HOST", and likewise for the restHTTP port: ProxyManager plus a login header
For an https:// page, ProxyManager opens a CONNECT tunnel through the proxy and sends the login header with it; for a plain http:// page it forwards the request with the header attached. The timeout and retry settings apply to every request the pool makes.
import json
import os
import urllib3
host = os.environ["PROXY_HOST"]
http_port = os.environ["PROXY_HTTP_PORT"]
# urllib3 takes the proxy login as a header. make_headers base64-encodes
# "user:password" exactly as given, so special characters need no escaping.
auth_header = urllib3.make_headers(
proxy_basic_auth=f"{os.environ['PROXY_USER']}:{os.environ['PROXY_PASS']}"
)
pool = urllib3.ProxyManager(
f"http://{host}:{http_port}",
proxy_headers=auth_header,
timeout=urllib3.Timeout(connect=10.0, read=30.0),
retries=urllib3.Retry(total=2, backoff_factor=1.0),
)
resp = pool.request("GET", "https://api.ipify.org?format=json")
print(resp.status, json.loads(resp.data)["ip"])SOCKS5 port: SOCKSProxyManager
SOCKS lives in urllib3.contrib.socks and needs the PySocks extra installed above. The socks5h:// scheme makes the proxy resolve hostnames, so DNS lookups go through it too. Passing the login as username and password arguments, rather than inside the URL, means it never needs percent-encoding.
import json
import os
from urllib3.contrib.socks import SOCKSProxyManager
pool = SOCKSProxyManager(
f"socks5h://{os.environ['PROXY_HOST']}:{os.environ['PROXY_SOCKS5_PORT']}",
username=os.environ["PROXY_USER"],
password=os.environ["PROXY_PASS"],
timeout=30.0,
)
resp = pool.request("GET", "https://httpbin.org/ip")
print(resp.status, json.loads(resp.data)["origin"])Either script should print an IP that isn't yours. Lookup sites may place it in a different city or state from the one you picked; their databases vary, and mobile IPs are often listed somewhere else.
Change the IP with the change-IP link
Every proxy has a private link that changes its IP when opened, with no API key needed. Get it from the proxy's ⋮ menu → Copy change-IP link and store it in PROXY_CHANGE_IP_URL. Because anyone holding the link can use it, treat it like a password; ⋮ → New change-IP link replaces it and stops the old one working.
The link answers in plain text ("OK — rotating your IP now. Allow up to ~90 seconds for the new IP."), and the change itself usually takes 30 to 90 seconds. So the script records the current IP, opens the link, then checks every 10 seconds until a different address appears. Clearing the pool before each check makes sure the answer doesn't come over a connection opened before the change.
import json
import os
import time
import urllib3
proxy = urllib3.ProxyManager(
f"http://{os.environ['PROXY_HOST']}:{os.environ['PROXY_HTTP_PORT']}",
proxy_headers=urllib3.make_headers(
proxy_basic_auth=f"{os.environ['PROXY_USER']}:{os.environ['PROXY_PASS']}"
),
timeout=urllib3.Timeout(connect=10.0, read=30.0),
)
# Opens the change-IP link itself. Its reply can take as long as the change, so read slowly.
direct = urllib3.PoolManager(timeout=urllib3.Timeout(connect=10.0, read=150.0))
def exit_ip():
proxy.clear() # close pooled connections so every check starts a new one
reply = proxy.request("GET", "https://api.ipify.org?format=json")
return json.loads(reply.data)["ip"]
before = exit_ip()
answer = direct.request("GET", os.environ["PROXY_CHANGE_IP_URL"])
print(answer.status, answer.data.decode())
for _ in range(12): # look every 10 seconds, for up to two minutes
time.sleep(10)
try:
current = exit_ip()
except (urllib3.exceptions.HTTPError, ValueError, KeyError):
continue
if current != before:
print(f"IP changed: {before} -> {current}")
break
else:
print("No new IP yet; check the proxy on My Proxies")The link works once every 60 seconds, so space out repeated calls. If you would rather get an answer only when the new IP is ready, use the API's rotate call with a Bearer key, as shown in the requests guide.
Fixing common failures
- ProxySchemeUnknown. A
socks5h://URL went to ProxyManager, which only acceptshttp://andhttps://proxy URLs. Use SOCKSProxyManager for the SOCKS5 port. - A warning or ImportError that mentions PySocks. Install the extra:
pip install "urllib3[socks]". - A ProxyError with 407 in it. The login header is missing or wrong. Check that
proxy_headersis set, and copy the password again with Copy password. - MaxRetryError caused by a timeout or a reset connection. The scheme and port are probably crossed:
http://belongs with the HTTP port andsocks5h://with the SOCKS5 port. The ⋮ → Details line contains the SOCKS5 port, not the HTTP one. - Connecting by IP whitelist. Copy endpoint in the IP whitelist window gives a host and port that need no login. With the whitelist protocol on HTTP, pass ProxyManager
http://plus that address and leave outproxy_headers; on SOCKS5, pass SOCKSProxyManagersocks5h://plus the address and nousernameorpassword. See the IP whitelist guide.