Skip to main content

A modern library for managing, leasing, and monitoring proxy pools.

Project description

Pharox Core

Python Versions PyPI Version CI Status License Code style: ruff Docs

The foundational Python toolkit for building robust proxy management systems.

pharox provides pure, domain-agnostic business logic for managing the entire lifecycle of network proxies. The library is designed as a reusable dependency for any Python application that needs to acquire, lease, and monitor proxies without inheriting opinionated service architecture.


Key Features

  • Proxy Leasing System: A powerful system to "lease" proxies to consumers, with support for exclusive, shared (concurrent), and unlimited usage.
  • Pluggable Storage: A clean interface (IStorage) that decouples the core logic from the database, allowing you to "plug in" any storage backend (e.g., in-memory, PostgreSQL, MongoDB).
  • Health Checking Toolkit: A protocol-aware HealthChecker with configurable options for HTTP, HTTPS, and SOCKS proxies.
  • Modern & Type-Safe: Built with Python 3.10+, Pydantic v2, and a 100% type-annotated codebase.
  • Toolkit, Not a Framework: Provides focused utilities that can be embedded inside your own scripts, workers, or services without imposing a runtime.

Installation

You can install pharox directly from PyPI:

pip install pharox

Need the SQL adapter tooling? Install the optional extras:

pip install 'pharox[postgres]'
# or, if you're hacking on the repo:
poetry install --extras postgres

Quickstart Example

Here is a simple example of how to use pharox with the default InMemoryStorage to acquire and release a proxy.

from pharox import (
    InMemoryStorage,
    Proxy,
    ProxyManager,
    ProxyPool,
    ProxyStatus,
)

# 1. Setup the storage and manager
storage = InMemoryStorage()
manager = ProxyManager(storage=storage)

# 2. Seed the storage with necessary data for the example
# In a real application, you would load this from your database.

# The manager uses a "default" consumer if none is specified and will
# auto-register it in the storage when first needed.

# Create a pool and a proxy
pool = ProxyPool(name="latam-residential")
storage.add_pool(pool)

proxy = Proxy(
    host="1.1.1.1",
    port=8080,
    protocol="http",
    pool_id=pool.id,
    status=ProxyStatus.ACTIVE,
)
storage.add_proxy(proxy)

# 3. Acquire a proxy from the pool (without specifying a consumer)
print(f"Attempting to acquire a proxy from pool '{pool.name}'...")
lease = manager.acquire_proxy(pool_name=pool.name, duration_seconds=60)

if lease:
    leased_proxy = storage.get_proxy_by_id(lease.proxy_id)
    print(f"Success! Leased proxy: {leased_proxy.host}:{leased_proxy.port}")
    print(f"Lease acquired by consumer ID: {lease.consumer_id}")

    # ... do some work with the proxy ...

    # 4. Release the lease when done
    print("\nReleasing the lease...")
    manager.release_proxy(lease)
    print("Lease released.")

    proxy_after_release = storage.get_proxy_by_id(lease.proxy_id)
    print(f"Proxy lease count after release: {proxy_after_release.current_leases}")
else:
    print("Failed to acquire a proxy. None available.")

Filtering and Geospatial Matching

ProxyFilters let you target proxies by provider metadata or geographic proximity. The storage layer handles the matching logic, including radius-based searches using latitude and longitude.

from pharox import ProxyFilters

filters = ProxyFilters(
    country="AR",
    source="fast-provider",
    latitude=-34.6,
    longitude=-58.38,
    radius_km=50,
)

lease = manager.acquire_proxy(
    pool_name="latam-residential",
    consumer_name="team-madrid",
    filters=filters,
)

if lease:
    print("Got a proxy close to Buenos Aires!")

Health Checks Across Protocols

Use HealthChecker to verify connectivity through HTTP, HTTPS, or SOCKS proxies. Health checks are configurable via HealthCheckOptions, letting you define target URLs, expected status codes, retry counts, and latency thresholds.

from pharox import HealthCheckOptions, HealthChecker, ProxyStatus

checker = HealthChecker()
options = HealthCheckOptions(
    target_url="https://example.com/status/204",
    expected_status_codes=[204],
    attempts=2,
    slow_threshold_ms=1500,
)

proxy = storage.get_proxy_by_id(lease.proxy_id)
health = await checker.check_proxy(proxy, options=options)

if health.status in {ProxyStatus.ACTIVE, ProxyStatus.SLOW}:
    print("Proxy ready for workloads")
else:
    print(f"Proxy inactive: {health.error_message}")

Embedding the Toolkit

pharox is intentionally agnostic about where it runs. Typical usage includes:

  • Automation scripts and workers leasing proxies with ProxyManager and performing health probes before dispatching workloads.
  • Custom services that provide APIs or dashboards on top of the toolkit by implementing the IStorage contract for their own databases.
  • Standalone applications that rely on the in-memory storage adapter for quick tasks or testing harnesses.

The in-memory adapter included with the package is great for development. For production, implement IStorage to connect the toolkit to your own persistence layer.

Examples

  • pharox.storage.postgres.PostgresStorage provides the reference PostgreSQL adapter. The accompanying examples/postgres/ directory bundles Docker Compose, schema migrations, and customization notes you can copy into your services.

Contributing

Contributions are welcome! Please see the CONTRIBUTING.md file for details on how to set up your development environment, run tests, and submit a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

pharox-0.4.0.tar.gz (21.2 kB view details)

Uploaded Source

Built Distribution

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

pharox-0.4.0-py3-none-any.whl (24.7 kB view details)

Uploaded Python 3

File details

Details for the file pharox-0.4.0.tar.gz.

File metadata

  • Download URL: pharox-0.4.0.tar.gz
  • Upload date:
  • Size: 21.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.12.13 Linux/6.14.0-1017-azure

File hashes

Hashes for pharox-0.4.0.tar.gz
Algorithm Hash digest
SHA256 cf16db4ab4e711116860382fa324e3dfcee41e7becd019b3b0b01a288ab49b3c
MD5 097ab2db062b7e9b092b1265c7f50151
BLAKE2b-256 78e396fa2be72a66f4811f42d7d0f6a575f1c025e5069f2da76f7ca0c77a4a3c

See more details on using hashes here.

File details

Details for the file pharox-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: pharox-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 24.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.12.13 Linux/6.14.0-1017-azure

File hashes

Hashes for pharox-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9d97b8ec6749ac2381b289d623fc16a2a368ddce5b925c07371fde502fd3a9f3
MD5 8f34d65c5fb0c29d0630fd0b375e0662
BLAKE2b-256 4052b222e8355a85b22c14f195a1855c125a4cc32a0c02a36375320f9d8083ee

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