# Integrating Python Requests &amp; curl_cffi with XProxy

> For AI tools: this is the Markdown copy of https://xproxy.io/document/integrating-python-requests-with-xproxy. Every XProxy guide has one at its URL + `.md`; the index of all guides is https://xproxy.io/llms.txt.

Send requests and curl_cffi through XProxy and change the IP from Python.

Send Python `requests` and `curl_cffi` scrapers through an XProxy mobile proxy, and change the IP from your code.

In short

**One proxy URL**

Your code needs one line: `http://user:pass@host:port`. Copy it from your XProxy panel.

**New IP from code**

Call one URL on your panel. The modem reconnects in a few seconds and gets a new IP.

**Easy to check**

Print the exit IP. It must match the **Public IP** of that proxy in your panel.

**You need:** Python 3 with `pip install requests curl_cffi`, and an XProxy panel with a working modem.

## Get your proxy URL

In your XProxy panel open **Proxy Management** and click **Export Proxy**. Pick the **Plain HTTP** preset with `user:pass@host:port`, turn on **Scheme prefix** and click **Copy All**. Each line is one proxy, for example `http://USERNAME:PASSWORD@192.168.1.10:4001`. The port is the one shown for that modem in **Proxy Management**.

The address must work from the computer that runs your code: see [how to copy a proxy in the right format](https://xproxy.io/document/how-use-proxy-format-http-socks5).

## Send a request through XProxy

**requests**

```
import requests

PROXY = "http://USERNAME:PASSWORD@192.168.1.10:4001"  # from Export Proxy
proxies = {"http": PROXY, "https": PROXY}

r = requests.get("https://api.ipify.org?format=json", proxies=proxies, timeout=20)
print(r.json())  # {'ip': '...'} the modem's public IP
```

Want SOCKS5? Run `pip install "requests[socks]"` and use `socks5h://USERNAME:PASSWORD@192.168.1.10:4001`. The `h` makes the proxy look up website names. Use the port shown in **Proxy Management**.

**curl_cffi**

```
from curl_cffi import requests

PROXY = "http://USERNAME:PASSWORD@192.168.1.10:4001"  # from Export Proxy

r = requests.get(
    "https://api.ipify.org?format=json",
    proxies={"http": PROXY, "https": PROXY},
    impersonate="chrome",  # send a real Chrome TLS/HTTP2 fingerprint
    timeout=20,
)
print(r.status_code, r.json())
```

`curl_cffi` sends the TLS and HTTP/2 fingerprint of a real browser. The proxy part works like in `requests`.

## Change the IP from code

Position **1** is the first modem, the one on port **4001** when the starting port is 4000. The code calls the rotation API, then asks the panel for the modem's status until it says `MODEM_READY`. All calls are in [How to change / rotate IP](https://xproxy.io/document/how-change-rotate-ip).

**Panel asks for a login?** If **Force API Auth** is on, these calls need your dashboard login: add `auth=("USER", "PASS")` to each `requests.get`.

**Rotate once**

```
import time

import requests

PANEL = "http://192.168.1.10"      # your XProxy panel address
POSITION = 1                       # the Pos number in Proxy Management
PROXY_ADDR = "192.168.1.10:4001"   # host:port of the same modem

def rotate_ip(timeout=60):
    """Ask the modem for a new IP, then wait until it is back online."""
    r = requests.get(f"{PANEL}/api/v1/rotate_ip/position/{POSITION}", timeout=10)
    r.raise_for_status()
    if not r.json().get("status"):
        raise RuntimeError(f"Rotation refused: {r.text}")
    time.sleep(3)  # the modem reconnects in a few seconds
    deadline = time.time() + timeout
    while time.time() < deadline:
        s = requests.get(f"{PANEL}/status", params={"proxy": PROXY_ADDR}, timeout=10).json()
        if s.get("msg") == "MODEM_READY":
            return s.get("public_ip")
        time.sleep(1)
    raise TimeoutError("The modem did not come back in time")

print("New IP:", rotate_ip())
```

**Retry when blocked**

```
# Add this under the code of "Rotate once" (it uses rotate_ip).
PROXY = "http://USERNAME:PASSWORD@192.168.1.10:4001"
proxies = {"http": PROXY, "https": PROXY}
URL = "https://api.ipify.org?format=json"

for attempt in range(3):
    r = requests.get(URL, proxies=proxies, timeout=20)
    if r.status_code not in (403, 429):
        break
    print(f"HTTP {r.status_code}, changing the IP...")
    rotate_ip()

print(r.status_code, r.text)
```

Changes the IP and tries again, up to 3 times, when a site answers 403 or 429.

## Check it works

1. Run the first script. It prints an IP address.
2. In **Proxy Management**, look at the **Public IP** of that proxy (marked 1). It is the same IP.
3. Run the rotate script, then the first script again. The IP is new.

![Proxy Management table with the Public IP of the first proxy marked (demo data)](https://media1.xproxy.io/docs/integrations/int-publicip-v1.webp)

The Public IP your script should print. Demo data.

## Something not working?

| You see | What to do |
| --- | --- |
| 407 Proxy Authentication Required, or an authentication error | The username or password is wrong or missing. Copy the proxy again after saving **Proxy Settings → Login**. If the password has special characters such as `@` or `:`, URL-encode them in the proxy URL. |
| Connection refused or timed out | The address is not reachable from your computer. A **LAN only** address works only on your own network: use **Public address** or **VPS relay** ([public access](https://xproxy.io/document/how-public-access-xproxy)). Also check the port in **Proxy Management**. |
| The printed IP is your own IP | The request did not go through the proxy. Check that the proxy is passed to the request, the browser or the crawler, and that it is not set only for `http` or only for `https`. |
| The rotation call fails or says `status` is false | Check the position number and the panel address. If **Force API Auth** is on, add your dashboard login to the call. |
| The IP is the same after rotating | Sometimes the carrier gives the modem the same IP. Rotate again. The status call can also answer `COLLISION_IP` when **IP unchanged detect** is on in Rotation Settings. |
| "Missing dependencies for SOCKS support" | Run `pip install "requests[socks]"`, or use an `http://` proxy URL. |

## What next?

Change the IP on a timer, or copy your proxies in another format.

[Rotate IP: all ways →](https://xproxy.io/document/how-change-rotate-ip) [Proxy formats →](https://xproxy.io/document/how-use-proxy-format-http-socks5)

---

Source: https://xproxy.io/document/integrating-python-requests-with-xproxy
