Python Requests Proxy Setup for Rotating IPs

To use a proxy with Python requests, you pass a `proxies` dictionary with the gateway host, port, and your proxy user credentials. You can control the network, location, and session behavior by adding options directly to the proxy username.

Key points
  • Use port 7070 for HTTP/HTTPS and port 7071 for SOCKS5 with the gateway host.
  • Add targeting and session options like `cc.us` or `sid.abc123` to the username.
  • Implement retries and connection pooling for reliable, high-concurrency scraping.

Python Requests Proxy Basics

The Python requests library supports proxies through a proxies argument to its request methods or a Session object. The proxy settings define the protocol, host, port, and authentication details. For our gateway, you will use gw.rotatingproxy.cc as the host, with port 7070 for HTTP/HTTPS traffic and port 7071 for SOCKS5 traffic. Authentication is handled via HTTP Basic Auth, embedded in the proxy URL as the username and password.

Where to Put Proxy Settings in Requests

You define proxies in a dictionary mapping protocol schemes (http, https) to proxy URLs. This dictionary can be passed to individual requests.get() calls or attached to a requests.Session() for reuse. Using a Session is recommended for efficiency, as it maintains a connection pool and reuses underlying TCP connections, which is crucial for planning concurrency for crawlers. The session approach also makes it easier to manage headers, cookies, and retry logic across multiple requests.

HTTP and HTTPS Setup on Port 7070

For HTTP and HTTPS traffic, configure the proxy to use port 7070. The proxy URL format includes your proxy user login, password, and any targeting options appended to the username with underscores.

import requests

proxy_url = "http://USERNAME_net.res_cc.us:PASSWORD@gw.rotatingproxy.cc:7070"
proxies = {
    "http": proxy_url,
    "https": proxy_url,
}

response = requests.get("https://httpbin.org/ip", proxies=proxies)
print(response.json())

In this example, USERNAME is your proxy user's login, PASSWORD is its password, net.res selects the residential network, and cc.us targets the United States. The same URL is used for both http and https keys because the gateway handles HTTPS via the CONNECT method on the same port. This setup is ideal for tasks like proxies for price monitoring or proxies for rank tracking.

SOCKS5 Setup on Port 7071

If your tool or target site works better with SOCKS5, use port 7071. The requests library itself does not natively support SOCKS5; you need to install the requests[socks] extra or the PySocks package. After installation, the setup is similar, but you change the scheme in the proxy URL to socks5. Here is an example with a mobile proxy targeting Germany:

pip install requests[socks]
import requests

proxy_url = "socks5://USERNAME_net.mob_cc.de:PASSWORD@gw.rotatingproxy.cc:7071"
proxies = {
    "http": proxy_url,
    "https": proxy_url,
}

response = requests.get("https://api.ipify.org?format=json", proxies=proxies)
print(response.json())

SOCKS5 can be beneficial in some network environments as it operates at a lower level, but for most web scraping, the HTTP proxy on port 7070 is sufficient. For a comparison, see HTTP port vs SOCKS5 port on our gateway.

Passing Targeting and Session Options in the Username

All proxy configuration beyond the network choice is done through the username field. You append options as key.value pairs, separated by underscores. The order does not matter. For example, to target the city of Chicago with a sticky session that lasts 30 minutes, you would construct a username like USERNAME_net.res_cc.us_city.chicago_sid.a7k2m9_ttl.30. The sid (session ID) is any string of 6 to 16 lowercase letters or digits you choose; reusing the same sid within the ttl window will keep the same exit IP. This is essential for proxies for social media management where you need to maintain a logged-in state.

If you omit the sid, you get a new IP on every new connection (or when the TCP connection closes in a pooled session). For detailed guidance, read sticky vs rotating sessions.

Verifying the Exit IP and Location

After setting up the proxy, verify that your traffic is exiting from the expected location and that the IP is rotating (or sticking) as intended. Simple public endpoints like https://api.ipify.org?format=json or https://ipinfo.io/json return your exit IP and often geolocation data. Run a quick test script:

import requests, time

proxy_url = "http://USERNAME_net.res_cc.ca:PASSWORD@gw.rotatingproxy.cc:7070"
proxies = {"http": proxy_url, "https": proxy_url}

for i in range(3):
    resp = requests.get("https://ipinfo.io/json", proxies=proxies)
    print(f"Request {i+1}:", resp.json()['ip'], resp.json()['country'])
    time.sleep(2)

If you used a session ID, the IP should remain the same across loops. If you did not, you should see different IPs (likely from the targeted country).

Common Errors: 407, Timeouts, TLS

A 407 Proxy Authentication Required error means your login was not accepted. Check that the username and password are correct, that the proxy user is not paused, and that your account has a positive balance. Ensure the username includes the correct network (e.g., net.res) if your proxy user is restricted. More fixes are at proxy error 407 authentication required.

Timeouts can happen because a site is slow or your connection settings are too tight. Increase the timeout parameter in your requests call. Also, consider that residential and mobile proxies inherently have higher latency than datacenter proxies.

TLS/SSL errors sometimes happen when the target site uses strict TLS fingerprinting. Using the HTTP proxy port (7070) with a modern requests version typically handles TLS correctly. If you encounter issues, ensure you are not using an outdated Python or OpenSSL version. For complex cases, a headless browser like Playwright Python proxy might be necessary.

Tips for Running at Scale with Requests

When making thousands of requests, efficiency and politeness are key. Always use a requests.Session to benefit from connection pooling and reduced overhead. Set appropriate User-Agent headers and respect robots.txt (see how to read robots.txt). Implement a solid retry logic using the urllib3.util.Retry class and an adapter:

from requests.adapters import HTTPAdapter
from urllib3.util import Retry

retry_strategy = Retry(
    total=3,
    backoff_factor=1,
    status_forcelist=[429, 500, 502, 503, 504]
)
adapter = HTTPAdapter(max_retries=retry_strategy, pool_connections=100, pool_maxsize=100)

session = requests.Session()
session.mount("http://", adapter)
session.mount("https://", adapter)

# Attach your proxies to the session
session.proxies.update(proxies)

Monitor your traffic usage to manage costs; our billing is pay as you go per GB. For large-scale projects like proxies for AI training data collection, consider distributing requests across multiple proxy users to stay within concurrency limits and to isolate traffic segments.

Using Sessions for Connection Pooling and Rotation

The requests.Session object is your main tool for managing connections. By default, a session reuses TCP connections for the same host, which is fast but can mean multiple requests go out through the same proxy connection, and potentially the same exit IP if you are not using a sid. To force a new IP for each logical request while still using a session, you can either omit the session ID (causing rotation when connections are recycled) or manually close the adapter's connection pool periodically. For more advanced patterns, review managing sessions in scrapers.

Integrating with Other Python Tools

While requests is versatile, you might need other libraries for asynchronous tasks or full browser simulation. For async HTTP, consider aiohttp proxy or httpx proxy. For browser automation, Playwright Python proxy and Selenium proxy with authentication are fully supported. If you are building a dedicated crawler, Scrapy proxy integration is also available.

Questions

Can I use one proxy URL for both http and https traffic?

Yes, the same proxy URL with the gateway host and port 7070 works for both HTTP and HTTPS protocols. The gateway handles HTTPS via the CONNECT tunnel method automatically.

How do I get a new IP address for every single request in Python requests?

Omit the session ID (`sid`) from your proxy username. Each new TCP connection will then receive a new IP. When using a `requests.Session` with connection pooling, you may need to adjust pool settings or close connections to ensure rotation happens as often as you intend.

Why is my SOCKS5 proxy not working with Python requests?

You likely need to install SOCKS support. Run `pip install requests[socks]` or `pip install PySocks`. Then ensure your proxy URL scheme is `socks5://` and you are using port 7071.

How can I target a specific US state like California?

Append `st.ca` to your username, and also include `cc.us` for the United States. The full option would look like `USERNAME_net.res_cc.us_st.ca`. Note that state targeting is only available for the US and with residential or mobile networks.

My script works without proxies but times out with the proxy. What should I do?

First, increase the `timeout` parameter in your `requests` call (e.g., `timeout=30`). Second, try a different proxy network; datacenter and ISP proxies generally have lower latency than residential. Third, check your own network for firewall rules blocking outbound connections to ports 7070 or 7071.

How do I manage multiple proxy users or locations in a large scraper?

Create multiple proxy users in your dashboard, each with its own login and password. You can then rotate through a list of proxy URLs in your code. For location rotation, change the `cc.xx` or `city.xxx` option in the username for each proxy user or for different request batches.

Start with one top-up, use any network

Create an account in a minute, add $40 or more, and the same balance pays for residential, mobile, datacenter and ISP traffic.