Rayobyte Guides

Using Rayobyte Proxies With Python: Requests, HTTPX and aiohttp

How to use Rayobyte proxies in Python with Requests, HTTPX and aiohttp: proxy URLs, rotating and sticky sessions, SOCKS5, retries with fresh sessions, concurrency and routing by domain.

By Team of Monkeys Editorial Updated 3 min read Affiliate disclosure

This guide contains affiliate links to Geonode, Webshare and Rayobyte. If you buy through them we may earn a commission, at no extra cost to you.

Rayobyte with Python illustration showing Python connecting through Rayobyte to Website, covering requests, httpx, aiohttp 3jdej38j4

The basics

Every Rayobyte product gives you a proxy you can express as a URL:

text
http://USERNAME:PASSWORD@HOST:PORT

Copy the host, port, username and password from the dashboard's generator. For rotating products, targeting and session options are usually part of the username; the generator builds that for you. Keep the full URL in an environment variable rather than in code. Our general Python Requests proxies guide covers proxy concepts in Python.

Requests

python
import os
import requests

proxy = os.environ["RAYOBYTE_PROXY"]  # http://user:pass@host:port
proxies = {"http": proxy, "https": proxy}

r = requests.get("https://httpbin.org/ip", proxies=proxies, timeout=30)
print(r.json())

Use a requests.Session() for connection reuse. On a rotating gateway, each new connection gets a new IP; a reused connection may keep the same one, which is usually fine.

HTTPX

python
import os
import httpx

with httpx.Client(proxy=os.environ["RAYOBYTE_PROXY"], timeout=30) as client:
    print(client.get("https://httpbin.org/ip").json())

Older HTTPX versions use proxies= instead of proxy=.

aiohttp for concurrency

python
import asyncio, os
import aiohttp

PROXY = os.environ["RAYOBYTE_PROXY"]
SEM = asyncio.Semaphore(50)  # stay under your plan's concurrency

async def fetch(session, url):
    async with SEM:
        async with session.get(url, proxy=PROXY, timeout=aiohttp.ClientTimeout(total=30)) as r:
            return r.status, await r.text()

async def main(urls):
    async with aiohttp.ClientSession() as session:
        return await asyncio.gather(*(fetch(session, u) for u in urls), return_exceptions=True)

Size the semaphore to your plan. Premium Residential lists unlimited concurrency; Scale Residential and monthly rotating ISP allow one request per dollar of plan. See concurrency and threads.

Residential tip: You can test most of this guide for free: Rayobyte offers a no-card trial on Premium Residential, rotating ISP and rotating datacenter proxies. Move to a paid plan once the pilot works. See our complete Rayobyte guide.

Sticky sessions with retries

Sticky sessions keep one IP across steps. Generate a session ID per task and put it in the username in the format the dashboard shows. If the residential IP drops, start a new session:

python
import uuid

def session_proxy(template):
    # template comes from the dashboard, with {session} where the session ID goes
    return template.format(session=uuid.uuid4().hex[:10])

def fetch_with_retry(url, template, tries=3):
    for _ in range(tries):
        proxy = session_proxy(template)
        try:
            r = requests.get(url, proxies={"http": proxy, "https": proxy}, timeout=30)
            if r.ok:
                return r
        except requests.RequestException:
            pass
    return None

See Rayobyte sticky sessions for session lengths and why sessions end early.

SOCKS5

Install requests[socks] and use socks5h:// so DNS resolves through the proxy:

python
proxy = "socks5h://user:pass@HOST:PORT"

See SOCKS5, UDP and protocol support.

Routing by domain

Use different Rayobyte products for different domains: rotating datacenter for easy sites, rotating ISP or residential for protected ones. A simple dictionary of domain to proxy URL is enough to start. See Rayobyte for web scraping.

Detecting blocks

Check content, not just status codes. Many blocked responses are 200 OK with a CAPTCHA page. Look for an element you expect, and treat its absence as a failure that triggers a retry on a new session or a higher tier.

Proxy tip: For a second opinion on hard targets, run the same test through Geonode residential proxies, which support sticky sessions up to 24 hours and ASN targeting. We compare the two in Rayobyte vs Geonode.

Common errors

  • 407: credentials or encoding. URL-encode special characters in the password with urllib.parse.quote.
  • ProxyError or timeouts: wrong port, narrow targeting or short timeout.
  • SSL errors: don't set the proxy URL to https:// unless the docs say to; most proxies take http:// even for HTTPS sites.

More in Rayobyte troubleshooting.

How it compares

The code is nearly identical for Geonode and Webshare; only the proxy URL format changes. See Rayobyte vs Geonode for Python, Rayobyte vs Webshare for Python and all three for Python.

Not sure this is the right proxy type for the job? The best proxies in 2026 guide compares residential, ISP, datacenter and mobile options, and our cheap proxies guide covers budget picks that still work.

Budget tip: For jobs where a fixed IP list is enough, Webshare is one of the cheapest options: shared datacenter proxies are sold by the IP, and static residential (ISP) proxies cost well under a dollar per IP per month at the time of writing. More in using Webshare with Python.

Key takeaways

  • Rayobyte proxies are plain proxy URLs that work with Requests, HTTPX and aiohttp.
  • Keep credentials in environment variables and build session usernames from the dashboard's template.
  • Use semaphores to stay under concurrency limits and retry with fresh sessions.
  • Detect blocks by content and route domains to the cheapest product that works.

FAQ

Does Rayobyte work with Python Requests?

Yes. Pass the proxy URL in the proxies dictionary for both http and https.

How do I rotate IPs in Python?

Use a rotating gateway, where each new connection gets a new IP, or change the session ID in the username.

Can I use SOCKS5 with Requests?

Yes, with requests[socks] installed and a socks5h:// proxy URL.

Affiliate disclosure: Team of Monkeys is an affiliate of Geonode, Webshare and Rayobyte. Links and banners marked as sponsored, and links to Geonode, Webshare and Rayobyte in the text, use our referral links, and we may earn a commission if you buy through them, at no extra cost to you. Our explanations and comparisons are written to be useful whichever provider you choose.

Editorial note: Proxy providers change their pools, features and prices often, so treat any figures here as typical ranges at the time of writing and confirm current terms with the provider before you buy.