Skip to content

Repository files navigation

httpx-proxyhat

Route httpx requests through ProxyHat residential proxies — drop-in sync and async clients with a sticky residential IP pinned for the client's lifetime, plus geo-targeting and rotation.

CICompatible with httpx latestPyPILicense: MIT

Tip

Recommended proxies — ProxyHat residential IPs. Every feature in this package is tested end-to-end against ProxyHat and works great. First-class integration; also works with any proxy, or none.

Why

Scraping or hitting APIs straight from a datacenter IP gets you flagged, rate-limited and blocked. httpx has first-class proxy support (proxy= on Client / AsyncClient), and this package wires ProxyHat's residential IPs (50M+ across 148+ countries) into it — one pinned residential IP per client by default, so cookies and TLS sessions stay coherent while you work. ProxyHatClient / ProxyHatAsyncClient are drop-in subclasses of httpx.Client / httpx.AsyncClient: same API, every request routed. No fork, no boilerplate.

Install

pip install httpx-proxyhat

For a SOCKS5 gateway, install the httpx SOCKS extra too:

pip install "httpx-proxyhat[socks]"

Quick start

fromhttpx_proxyhatimportProxyHatClient# An API key auto-selects an active residential sub-user:withProxyHatClient(api_key="ph_your_api_key", country="us") asclient:
r=client.get("https://api.ipify.org?format=json")
print(r.json()) # a US residential IP, pinned for the whole client

Async is identical:

fromhttpx_proxyhatimportProxyHatAsyncClientasyncwithProxyHatAsyncClient(country="de") asclient:
r=awaitclient.get("https://api.ipify.org?format=json")

Get an API key at proxyhat.com.

Already wiring httpx yourself? Grab the raw proxy URL:

importhttpxfromhttpx_proxyhatimportproxyhat_proxy_urlproxy=proxyhat_proxy_url(country="gb", sticky="1h")
# -> "http://<user>-country-gb-sid-<id>-ttl-1h:<pass>@gate.proxyhat.com:8080"withhttpx.Client(proxy=proxy) asclient:
client.get("https://example.com")

Credentials

Pass them explicitly or via environment variables — options win over env:

OptionEnv varNotes
api_keyPROXYHAT_API_KEYAuto-selects an active sub-user with remaining traffic
sub_userPROXYHAT_SUBUSERPick a specific sub-user by uuid or name (with an API key)
usernamePROXYHAT_USERNAMEExplicit gateway proxy_username (skips the API)
passwordPROXYHAT_PASSWORDExplicit gateway proxy_password

Targeting

Every option is accepted by ProxyHatClient, ProxyHatAsyncClient and proxyhat_proxy_url:

ProxyHatClient(
protocol="http", # or "socks5" (needs the [socks] extra)country="us", # ISO code or "any" (default)region="california",
city="new_york",
filter="high", # AI IP-quality tiersticky="30m", # client lifetime (default); sticky=False rotatestimeout=30.0, # any extra kwarg is forwarded to httpx.Clientheaders={"user-agent": "my-scraper/1.0"},
follow_redirects=True,
)

Sticky IP per client (default)

A client typically makes many requests against the same site — paging, following links, keeping a login. If the exit IP changed mid-session the site would see a user teleporting between cities and block it. So this package is sticky by default: one residential IP is pinned for the whole client (sticky="30m", renewed as you work), so cookies, TLS sessions and fingerprint stay coherent. The resolved URL is on client.proxyhat_url.

Want a fresh IP on every connection instead (e.g. many independent one-shot fetches)? Turn stickiness off:

fromhttpx_proxyhatimportProxyHatClientwithProxyHatClient(country="us", sticky=False) asclient: # rotating residential IPforurlinurls:
client.get(url)

Set a custom lifetime with sticky="2h".

How it works

proxyhat_proxy_url(...) resolves your gateway credentials (via the official proxyhat SDK — an API key auto-picks an active sub-user, or pass username/password), then builds the proxy URL: the host is the ProxyHat gateway (gate.proxyhat.com:8080, or :1080 for SOCKS5) and the username carries ProxyHat's targeting grammar (<user>-country-us-sid-<id>-ttl-30m). ProxyHatClient / ProxyHatAsyncClient build that URL and pass it to httpx's proxy= parameter, so every request the client makes is tunnelled through a residential IP. A sticky username (with a session id) pins one IP for the client's lifetime; a rotating one (no session id) makes the gateway hand out a fresh IP per new connection.

License

MIT © ProxyHat

About

Route httpx (sync + async) through ProxyHat residential proxies — sticky sessions, geo-targeting, rotation.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages