Skip to content

Integrating Python Requests & curl_cffi with XProxy

Ask AI

Ready taskAdd this to my code

Coding agents

Claude CodeOpens the desktop appCodexOpens the desktop appCursorOpens the app from the web

Chat assistants

Open in ChatGPTNew tabOpen in ClaudeNew tab

Copy

View as MarkdownPlain text for AI tools
See the prompt
Read https://xproxy.io/document/integrating-python-requests-with-xproxy.md, the XProxy guide "Integrating Python Requests & curl_cffi with XProxy". If you cannot open links, download them with curl. Then add it to my project: read my code, use the language and libraries it already uses, keep proxy logins and API keys in environment variables, and show me how to test it.
Other XProxy guides, each with a Markdown copy at its URL + .md, are listed in https://xproxy.io/llms.txt.

The app opens with the prompt filled in. Nothing runs until you press Enter.

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:[email protected]: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.

Send a request through XProxy

requests_xproxy.py
import requests

PROXY = "http://USERNAME:[email protected]: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:[email protected]:4001. The h makes the proxy look up website names. Use the port shown in Proxy Management.

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.

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_xproxy.py
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())

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)
1 The Public IP your script should print. Demo data.

Something not working?

You seeWhat to do
407 Proxy Authentication Required, or an authentication errorThe 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 outThe 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). Also check the port in Proxy Management.
The printed IP is your own IPThe 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 falseCheck 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 rotatingSometimes 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.

Still stuck?A technician can look at your device with a one-time PIN, or chat with us on Telegram.