You point a scraper at a rotating proxy, the kind where every request is supposed to leave from a different IP. You log the IP the site sees, and it's the same one ten times in a row. Then you try the proxy with a curl loop in the terminal and the IP changes every time.

The proxy is usually fine. If your code uses requests.Session(), or you put several URLs in one curl command, all of those requests go down one connection. For https:// sites, a rotating gateway has no way to pick a new exit inside that connection.

The fix

If you want a new IP per request, give each request its own connection. Any of these does it:

import requests proxy = "http://user:pass@host:port" proxies = {"http": proxy, "https": proxy} # 1. No Session: requests.get() opens and closes its own connection each time r = requests.get(url, proxies=proxies, timeout=10) # 2. Keep your Session (cookies, headers) but ask for the connection to be closed with requests.Session() as s: r = s.get(url, proxies=proxies, timeout=10, headers={"Connection": "close"}) # 3. A fresh Session per request with requests.Session() as s: r = s.get(url, proxies=proxies, timeout=10) 

In curl, run one command per request instead of listing several URLs in one command.

It also works the other way round. If you need one IP for a whole login flow, a kept-alive connection gives you that, but only until something closes it. A sticky session from your provider is the dependable way to hold an IP.

Why one connection means one IP

When you request an https:// URL through an HTTP proxy, the client doesn't send the proxy your request. It sends this:

CONNECT httpbin.org:443 HTTP/1.1 

The proxy opens a connection to that host, from whichever exit IP it picks, and after that it just passes encrypted bytes back and forth. Your GET, your headers and your cookies all travel inside that TLS tunnel, and the proxy can't read any of it. It can't even tell where one request ends and the next starts.

A Session keeps connections open and reuses them, which is most of why it's faster. So request 2 goes into the tunnel that request 1 opened, through the same exit. The gateway only gets a chance to rotate when it sees a new CONNECT.

The test

I knew all that in theory and wanted to count it. Everything ran on my Mac (macOS 26.7.1) with curl 8.7.1, Python 3.9.6, requests 2.32.5 and urllib3 2.6.3. No paid proxy. I wrote a small proxy that behaves like a rotating gateway: each new client connection gets the next "exit IP" from a list of documentation addresses (203.0.113.x), and it prints every CONNECT and every plain HTTP request with the exit it used. The requests themselves go to the real httpbin.org, so the keep-alive behaviour on the other end is a real server's.

rotating_proxy.py
# rotating_proxy.py: a small HTTP proxy that acts like a rotating gateway. # Every new client connection gets the next "exit IP" from a list, and every # CONNECT tunnel and plain-HTTP request is printed with the exit it went out on. import http.client, itertools, socket, threading # Documentation-only addresses (RFC 5737), standing in for a pool of exit IPs EXITS = itertools.cycle(f"203.0.113.{n}" for n in range(10, 250)) conn_ids = itertools.count(1) lock = threading.Lock() def pipe(src, dst): try: while (data := src.recv(65536)): dst.sendall(data) except OSError: pass finally: for s in (src, dst): try: s.shutdown(socket.SHUT_RDWR) except OSError: pass def read_head(f): lines = [] while (line := f.readline()) not in (b"\r\n", b"\n", b""): lines.append(line.decode("latin-1").rstrip("\r\n")) return lines def handle(client): with lock: cid, exit_ip = next(conn_ids), next(EXITS) print(f"conn {cid:>2}: new connection, exit {exit_ip}", flush=True) f = client.makefile("rb") n_req = 0 while True: head = read_head(f) if not head: break method, target, _ = head[0].split(" ", 2) if method == "CONNECT": print(f"conn {cid:>2}: CONNECT {target} via {exit_ip}", flush=True) host, port = target.rsplit(":", 1) upstream = socket.create_connection((host, int(port)), timeout=10) upstream.settimeout(None) # no idle timeout of our own once the tunnel is up client.sendall(b"HTTP/1.1 200 Connection established\r\n\r\n") threading.Thread(target=pipe, args=(client, upstream), daemon=True).start() pipe(upstream, client) return # Plain http:// request with an absolute URL: forward it, keep the client connection open n_req += 1 rest = target.split("://", 1)[1] host, _, path = rest.partition("/") print(f"conn {cid:>2}: request {n_req}: {method} http://{rest} via {exit_ip}", flush=True) up = http.client.HTTPConnection(host, timeout=10) up.request(method, "/" + path, headers={"Host": host, "Connection": "close"}) resp = up.getresponse() body = resp.read() up.close() out = [f"HTTP/1.1 {resp.status} {resp.reason}"] out += [f"{k}: {v}" for k, v in resp.getheaders() if k.lower() not in ("connection", "transfer-encoding", "content-length", "keep-alive")] out += [f"Content-Length: {len(body)}", "", ""] client.sendall("\r\n".join(out).encode("latin-1") + body) client.close() server = socket.socket() server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server.bind(("127.0.0.1", 8899)) server.listen() while True: conn, _ = server.accept() threading.Thread(target=handle, args=(conn,), daemon=True).start() 

ten_requests.py
# ten_requests.py: send 10 requests through the proxy, one of four ways import sys, time, warnings warnings.filterwarnings("ignore") import requests PROXY = "http://127.0.0.1:8899" PROXIES = {"http": PROXY, "https": PROXY} mode = sys.argv[1] url = sys.argv[2] if len(sys.argv) > 2 else "https://httpbin.org/uuid" start = time.perf_counter() if mode == "get": # requests.get() each time for _ in range(10): requests.get(url, proxies=PROXIES, timeout=10) elif mode == "session": # one Session for all 10 with requests.Session() as s: for _ in range(10): s.get(url, proxies=PROXIES, timeout=10) elif mode == "session-close": # one Session, Connection: close on every request with requests.Session() as s: for _ in range(10): s.get(url, proxies=PROXIES, timeout=10, headers={"Connection": "close"}) elif mode == "new-session": # a fresh Session per request for _ in range(10): with requests.Session() as s: s.get(url, proxies=PROXIES, timeout=10) print(f"{mode}: 10 requests in {time.perf_counter() - start:.2f}s") 

For each run I started the proxy fresh (python3 rotating_proxy.py), ran python3 ten_requests.py <mode>, and counted the lines in the proxy's log.

What the proxy saw

Ten requests to https://httpbin.org/uuid each time. The time is the median of three runs.

How the 10 requests were sent Connections CONNECTs Exit IPs used Time
requests.get() ×10 10 10 10 9.44 s
one Session, 10 × s.get() 1 1 1 3.12 s
one Session + Connection: close 10 10 10 9.37 s
new Session per request 10 10 10 9.56 s
curl, one command, 10 URLs 1 1 1
curl in a shell loop, 10 commands 10 10 10

Here is the whole proxy log for the Session run:

conn 1: new connection, exit 203.0.113.10 conn 1: CONNECT httpbin.org:443 via 203.0.113.10 

The other nine requests went through that tunnel, so the proxy never saw them. The requests.get() run starts like this and carries on up to conn 10:

conn 1: new connection, exit 203.0.113.10 conn 1: CONNECT httpbin.org:443 via 203.0.113.10 conn 2: new connection, exit 203.0.113.11 conn 2: CONNECT httpbin.org:443 via 203.0.113.11 

Most of the extra six seconds is a new tunnel and a new TLS handshake for every request, which is what a new IP each time costs you. Your numbers will depend on how far you are from the proxy and from the site, and they wobble: one earlier run of the Connection: close version took 12.93 s.

For curl, -w '%{num_connects}' prints how many new connections each transfer had to open:

curl -s -o /dev/null -w '%{num_connects} ' -x http://127.0.0.1:8899 'https://httpbin.org/uuid?n=[1-10]' 
1 0 0 0 0 0 0 0 0 0 

The first transfer opened the connection and the other nine reused it. With -v, curl prints Re-using existing connection with proxy 127.0.0.1.

Plain http:// is different

I ran the Session test once more against http://httpbin.org/uuid. Again one connection, but this time the log had ten request lines:

conn 1: new connection, exit 203.0.113.10 conn 1: request 1: GET http://httpbin.org/uuid via 203.0.113.10 conn 1: request 2: GET http://httpbin.org/uuid via 203.0.113.10 

Without TLS there's no tunnel, so the proxy reads every request. My test proxy picks the exit per connection and used one IP for all ten, but a gateway that rotates per request could give each of them a different exit. So the same scraper can rotate fine on an http:// site and get stuck on one IP on an https:// one.

What this doesn't tell you

  • My proxy is a stand-in. Real rotating gateways decide when to rotate in their own ways, and some also rotate on a timer. What none of them can do is change the exit in the middle of a CONNECT tunnel they can't read.
  • With one Session and a 50-second pause between two requests, the tunnel was still reused. If a Session's IP changes only now and then, look for whatever closed the connection: the site, the proxy, or an error.
  • A new IP doesn't make your requests unrelated. A Session also carries cookies, and a site can link requests through those no matter which IP they come from.

I work on BuyProxy, a proxy service. Nothing in this test used it; every request went through the small proxy above.