Code examples
Replace the username and password with your own. Each example fetches a page that returns the exit IP address.
curl
# HTTP proxy
curl -x http://USERNAME-country-us-time-600-session-a1b2c3d4:PASSWORD@resi.piratproxies.com:8080 https://api.ipify.org
# SOCKS5 proxy (socks5h resolves names through the proxy)
curl -x socks5h://USERNAME-country-us-time-600-session-a1b2c3d4:PASSWORD@resi.piratproxies.com:1080 https://api.ipify.orgPython (requests)
import requests
proxy = "http://USERNAME-country-us-time-600-session-a1b2c3d4:PASSWORD@resi.piratproxies.com:8080"
r = requests.get("https://api.ipify.org", proxies={"http": proxy, "https": proxy}, timeout=30)
print(r.text)Node.js (undici)
import { ProxyAgent, fetch } from "undici";
const proxy = new ProxyAgent("http://USERNAME-country-us-time-600-session-a1b2c3d4:PASSWORD@resi.piratproxies.com:8080");
const res = await fetch("https://api.ipify.org", { dispatcher: proxy });
console.log(await res.text());Playwright
import { chromium } from "playwright";
const browser = await chromium.launch({
proxy: { server: "http://resi.piratproxies.com:8080", username: "USERNAME-country-us-time-600-session-a1b2c3d4", password: "PASSWORD" },
});
const page = await browser.newPage();
await page.goto("https://api.ipify.org");
console.log(await page.textContent("body"));
await browser.close();Scrapy
import scrapy
class IpSpider(scrapy.Spider):
name = "ip"
def start_requests(self):
yield scrapy.Request(
"https://api.ipify.org",
meta={"proxy": "http://USERNAME-country-us-time-600-session-a1b2c3d4:PASSWORD@resi.piratproxies.com:8080"},
)
def parse(self, response):
self.logger.info(response.text)A different IP for each worker
Give each worker its own session value. This Python snippet makes ten sticky proxies:
import secrets, string
def session_id():
return "".join(secrets.choice(string.ascii_lowercase + string.digits) for _ in range(8))
proxies = [
f"http://USERNAME-country-us-time-600-session-{session_id()}:PASSWORD@resi.piratproxies.com:8080"
for _ in range(10)
]What goes wrong most often
- The proxy address starts with
http://, even when the page you fetch is https. Writinghttps://in front of the proxy host makes most libraries fail to connect. - A password with characters such as
@,:or/must be percent-encoded when it is written inside an address. - Always set a timeout. A home or mobile connection can stall, and a request without a timeout waits for ever.
- Libraries reuse connections. To get a new IP address for each request on a rotating plan, open a new connection or session object each time.
- Blocking images, fonts and media in a browser tool cuts the traffic a page uses many times over.
The same examples for other languages and tools, with their own pitfalls, are in the web scraping guides.
Stuck? Use the chat in the corner or write to support@piratproxies.com.