Skip to content
hiproxy
Get started

Python requests proxy setup with HiProxy

Copy-ready examples for Python requests, Node.js and cURL. All of them use the HiProxy gateway, so the part you change is the username.

Updated 2026-11-28 · 2 min read

Short answer

In Python requests, pass a proxies dictionary with the same proxy URL for http and https, built from your username, password, gate.hiproxy.shop and port 6100. For SOCKS5, install requests[socks] and use socks5h with port 6200. Leave out -sid- for a new IP per request, or add -sid- and -ttl- to keep one IP for up to 60 minutes.

Python requests proxy: the basic example

Setting a Python requests proxy takes one dictionary. Put the same proxy URL under both the http and https keys. The https key covers https:// addresses, and those requests still go through the HTTP port as a tunnel.

The address it prints is the IP your request used. Replace hpexample1 and your_password with the proxy username and password from your dashboard. Always set a timeout so one slow request cannot hang your script.

import requests

proxy = "http://hpexample1-zone-res-cc-us:your_password@gate.hiproxy.shop:6100"
proxies = {"http": proxy, "https": proxy}

r = requests.get("https://api.ipify.org", proxies=proxies, timeout=30)
print(r.text)

Using SOCKS5 in Python

SOCKS support in requests needs one extra package: pip install requests[socks]. After that, change the scheme to socks5h and the port to 6200. Everything else stays the same.

socks5h means the proxy resolves host names. With plain socks5 your machine does the DNS lookup first, and that lookup leaves from your own network.

# pip install requests[socks]
import requests

proxy = "socks5h://hpexample1-zone-res-cc-us:your_password@gate.hiproxy.shop:6200"
proxies = {"http": proxy, "https": proxy}

r = requests.get("https://api.ipify.org", proxies=proxies, timeout=30)
print(r.text)

Rotating and sticky sessions

By default each request gets a new IP. That fits scraping, where requests do not depend on each other. To keep one IP across several requests, such as a login and the pages after it, add -sid- with any ID and -ttl- with the minutes, up to 60. Every request with the same ID uses the same IP.

OptionExampleWhat it does
-zone--zone-resProduct: res, unl, isp or dc
-cc--cc-usExit country, two lowercase letters
-city--city-new_yorkCity, with underscores for spaces
-sid--sid-a1b2c3d4Same IP for every request with this ID
-ttl--ttl-30Minutes the session keeps its IP, 1 to 60
import uuid
import requests

sid = uuid.uuid4().hex[:8]
user = f"hpexample1-zone-res-cc-us-sid-{sid}-ttl-30"
proxy = f"http://{user}:your_password@gate.hiproxy.shop:6100"

s = requests.Session()
s.proxies = {"http": proxy, "https": proxy}
print(s.get("https://api.ipify.org", timeout=30).text)
print(s.get("https://api.ipify.org", timeout=30).text)  # same IP

Node.js and cURL examples

In Node.js, the undici package gives fetch a proxy through ProxyAgent. For SOCKS5, the socks-proxy-agent package does the same job with a socks5h:// URL on port 6200.

With cURL, pass the proxy with -x and the login with -U: curl -x http://gate.hiproxy.shop:6100 -U "USERNAME:PASSWORD" https://api.ipify.org. For SOCKS5, use socks5h://gate.hiproxy.shop:6200 instead. Run it twice without -sid- and you should see two different IPs.

import { ProxyAgent, fetch } from "undici";

const agent = new ProxyAgent(
  "http://hpexample1-zone-res-cc-us:your_password@gate.hiproxy.shop:6100"
);
const res = await fetch("https://api.ipify.org", { dispatcher: agent });
console.log(await res.text());

Fixing 407 errors and timeouts

Log the status code and the exit IP for failed requests. It makes it much easier to tell a proxy login problem from a block by the target site.

ErrorLikely causeFix
407 Proxy Authentication RequiredWrong proxy username or password, or a typo in the optionsCopy both again from the dashboard. If you issued a new proxy password, update the script.
Connection timed outThe port does not match the scheme, or a firewall blocks outgoing connectionsUse 6100 with http:// and 6200 with socks5h://. Allow outgoing traffic to gate.hiproxy.shop.
Missing dependencies for SOCKS supportrequests[socks] is not installedRun pip install requests[socks].
IP in the wrong country-cc- is not a lowercase two-letter codeWrite the code in lowercase, such as -cc-us.
Read timed out on some requestsA slow target site or routeCatch the exception and retry. Without -sid- the retry gets a new IP.

Running scripts on a server with Allowed IPs

If your script runs on a server with a fixed IPv4 address, add that address to Allowed IPs in the dashboard. Up to 10 addresses fit. Requests from those servers connect without the proxy password.

Either way, keep the proxy password out of code you share or commit. Read it from an environment variable instead, and issue a new one in the dashboard if it ever leaks.

Questions about this guide

How do I set a proxy in Python requests?

Pass proxies={'http': proxy, 'https': proxy} to the request, where proxy is your full proxy URL. Use port 6100 for HTTP, or socks5h with port 6200 for SOCKS5.

Why do I get a 407 error?

407 means the proxy did not accept the login. Check the username, its options and the proxy password. The proxy password is not the one you use to sign in to the site.

How do I rotate IPs in Python?

You do not need extra code. Without -sid- in the username, each request gets a new IP. Add -sid- only when you want to keep one.

Does the same code work with every HiProxy product?

Yes. Change -zone- to res, unl, isp or dc. The host, ports and password stay the same.

Do I need a separate SOCKS5 plan?

No. Every plan works on both ports. Only the scheme and the port in the URL change.

Top up $50 and get 185 GB of residential traffic

Pick a username and a password and you're in. Your balance never expires.