Skip to main content

Proxy-backed HTTP session utilities with retry and rotation support.

Project description

rotate-session

rotate-session is a small Python package for HTTP requests with retry support and optional proxy rotation.

Installation

pip install rotate-session

Features

  • Retry failed requests with configurable attempts and wait time
  • Rotate across a list of proxies automatically on retry
  • Fetch proxy lists from Webshare
  • Use plain requests-style proxies when provider integration is not needed

Quick start

from rotate_session import RotateSession

session = RotateSession()
response = session.get("https://httpbin.org/get", timeout=20)
print(response.status_code)

Configure retries

from rotate_session import RetryConfig, RotateSession

retry = RetryConfig(max_retries=5, wait_secs=2)
session = RotateSession(retry_config=retry)

Use your own proxy list

from rotate_session import ProxyConfig, RotateSession

proxy_config = ProxyConfig(
    proxies=[
        {"http": "http://user:pass@1.2.3.4:8080", "https": "http://user:pass@1.2.3.4:8080"},
        {"http": "http://user:pass@5.6.7.8:8080", "https": "http://user:pass@5.6.7.8:8080"},
    ]
)

session = RotateSession(proxy_config=proxy_config)
response = session.get("https://httpbin.org/ip", timeout=20)
print(response.text)

Use Webshare as proxy provider

from rotate_session import ProxyConfig, ProxyProvider, RotateSession

proxy_config = ProxyConfig(
    provider=ProxyProvider.WEBSHARE,
    api_key="YOUR_WEBSHARE_API_KEY",
)

session = RotateSession(
    proxy_config=proxy_config,
    proxies_fetch_params={"country_codes": ["US"], "mode": "direct"},
)

response = session.get("https://httpbin.org/ip", timeout=20)
print(response.text)

Run multiple scrape targets

import re

from rotate_session import RotateSessionMulti, ScrapeTarget


class QuotePageScraper(RotateSessionMulti):
    def extract_once(self, from_scrape, progress_text=""):
        quotes = re.findall(
            r'<span class="text" itemprop="text">(.*?)</span>',
            from_scrape.text,
        )
        return quotes[:3]


scraper = QuotePageScraper(
    targets=[
        ScrapeTarget(
            url="https://quotes.toscrape.com/page/1/",
            kwargs={"timeout": 20},
        ),
        ScrapeTarget(
            url="https://quotes.toscrape.com/page/2/",
            kwargs={"timeout": 20},
        ),
    ],
    global_params={"source": "quotes.toscrape.com"},
    num_threads=2,
)

results = scraper.run()
print(results)

ScrapeTarget is the default target model. The base scrape_once(session, target, progress_text="") uses its url, optional params, and optional request kwargs. It can also validate compatible dictionaries with the same keys.

For custom target shapes, use dictionaries and override both hooks:

import html
import re

from rotate_session import RotateSessionMulti


class QuoteTagScraper(RotateSessionMulti):
    def scrape_once(self, session, target, progress_text=""):
        response = session.get(
            f"{self.global_params['base_url']}/tag/{target['tag']}/",
            timeout=20,
        )
        return {"tag": target["tag"], "html": response.text}

    def extract_once(self, from_scrape, progress_text=""):
        quotes = re.findall(
            r'<span class="text" itemprop="text">(.*?)</span>',
            from_scrape["html"],
        )
        return {
            "tag": from_scrape["tag"],
            "quotes": [html.unescape(quote) for quote in quotes[:2]],
        }


scraper = QuoteTagScraper(
    targets=[
        {"tag": "love"},
        {"tag": "humor"},
    ],
    global_params={"base_url": "https://quotes.toscrape.com"},
    num_threads=2,
)

results = scraper.run()
print(results)

Notes

  • Logging, documentation and README were generated with AI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rotate_session-0.1.5.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rotate_session-0.1.5-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file rotate_session-0.1.5.tar.gz.

File metadata

  • Download URL: rotate_session-0.1.5.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"PikaOS","version":"4","id":"nest","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for rotate_session-0.1.5.tar.gz
Algorithm Hash digest
SHA256 de79a28d250a9e021e4a6a87af2356385c6568ef6dc8ca4e18aa29e05df9511a
MD5 206f9d563a2cfb95cccdc0270401293f
BLAKE2b-256 d571fb75e074c0f2eefd3803e600d9a63796dba2c4b696541cd33b171601c7df

See more details on using hashes here.

File details

Details for the file rotate_session-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: rotate_session-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"PikaOS","version":"4","id":"nest","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for rotate_session-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 2b3396730c72c3358769c1dad4a7ec47472df4fd7325d63e6d47030d7205d4e4
MD5 de3109942accd032184c4ddb669f86b5
BLAKE2b-256 3d51e6037a313583d06e13428143ac4028080d1f7cc229d55fe7e2432db191a0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page