BlogGuide9 min read

Using a proxy with Python requests

Pass a proxy to requests through the proxies dictionary, put the credentials in the URL, and confirm the exit with an IP check. SOCKS5 needs one extra library and a scheme change.

A code editor showing a short HTTP request in teal syntax
One dictionary carries the proxy; one call proves the exit.

The requests library takes a proxy through one small dictionary. Put the host, port, username, and password in the URL, pass the dictionary to your call, and check the exit IP to prove it worked. HTTP and SOCKS5 differ by one scheme and one dependency.

The same pattern works for any HTTP client, so it is worth getting right once.

The proxies dictionary

Requests reads a proxies mapping with http and https keys. Point both at the same HTTP proxy endpoint so every request uses it, not just the plain-HTTP ones.

Use the endpoint from your dashboard. On this service the HTTP CONNECT endpoint is proxygateway.si:41081. The field names are in the documentation.

Put authentication in the URL

Encode the username and password into the URL, as in http://user:pass@host:port. If the password contains characters like @ or a colon, percent-encode it first, or requests will misread the address.

A single stray space in the password is the most common reason every call fails with a 407.

SOCKS5 needs one extra step

To use SOCKS5, install the PySocks package and switch the scheme to socks5h. The h form resolves DNS through the proxy, which keeps the lookup inside the tunnel and avoids leaking the real location.

On this service the SOCKS5 endpoint is proxygateway.si:41082. Which endpoint belongs in which client is covered in HTTP or SOCKS5.

Confirm the exit IP

After the call, fetch an IP-check endpoint through the same proxy and print the returned address. If it matches the dashboard, the traffic left through the modem. If it shows your own IP, the proxies mapping did not apply.

Do this once per run. It catches a silent fallback before it corrupts a whole job.

Requests that trip people up

These five account for most failed calls. Check them before you debug the target.

  1. Wrong scheme: the HTTP endpoint with a socks5 prefix, or the reverse.
  2. Unencoded password characters in the URL.
  3. A trailing space in the pasted password.
  4. Missing PySocks when using socks5h.
  5. Forgetting to set both the http and https keys.

Rotating the IP from a script

When a job needs a fresh address, call the rotation URL, wait for the change to finish, then continue. The host, port, and credentials stay the same; only the modem's IP changes. How to rotate an IP on demand covers the timing.

Why do I get a 407 proxy authentication required?

Usually a wrong or unencoded password, or a stray space. Percent-encode special characters in the URL and recopy the password from the dashboard.

Do I need a separate library for SOCKS5?

Yes. Install PySocks and use the socks5h scheme. Plain HTTP works with requests alone.

How do I know the proxy is being used?

Fetch an IP-check URL through the same session and compare it with your dashboard. A different address means the proxy is applied.

Can I rotate the IP from inside the script?

Yes. Call the rotation URL, wait until the change completes, then send the next request. The credentials stay the same.