Skip to content

davidteather/proxyproviders

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ProxyProviders

The Unified Python Proxy API for managing proxies with support for BrightData, WebShare, and more.

codecov Sponsor Me GitHub release (latest by date) GitHub Downloads Support Server LinkedIn

Table of Contents

Documentation

You can find the full documentation here

Note: If you want to learn how to web scrape websites check my free and open-source course for learning everything web scraping

How to Support The Project

  • Star the repo 😎
  • Consider sponsoring me on GitHub
  • Send me an email or a LinkedIn message about what you're doing with it :D
  • Submit PRs for issues or any new providers/features :)

Getting Started

To get started using this package follow the instructions below.

Installation

This package is installable via pip.

python -m pip install proxyproviders

Quick Start Guide

Here's a quick bit of code to get you started! There's also a few examples in the folder.

Note: If you want to learn how to web scrape websites check my free and open-source course for learning everything web scraping

Choose a Proxy Provider

If you already haven't, choose which proxy provider to use. You can find a list in the documentation. After choosing, look at the documentation around the specific provider you've choosen. Where needed, we've laid out steps to get api keys in the documentation. These steps will vary slightly by provider. For this example I'll be using the Webshare Provider because it's both easy to setup and they give you 10 free data center proxies to test out.

You can create an account on webshare here (affiliate link), then head into the API tab on the side and generate a new token. Keep this API token safe and don't post it publically.

Basic Example

After you can list out your proxies with

from proxyproviders import Webshare

proxy_provider = Webshare(api_key="your-api-key")

proxies = proxy_provider.list_proxies()

print(proxies)

Each provider has their own custom options, the Webshare class lets you specify url params according to their api spec, here's an example which will only return proxies that are based in the US.

proxy_provider = Webshare(api_key="your-api-key", params={"country_code_in": "US"})

Using ProxyConfig

For any shared logic across all types of proxy providers, we use the ProxyConfig data class to configure them. The full docs for ProxyConfig are here. In this example we will configure it to use a shorter refresh_interval than default.

from proxyproviders import Webshare, ProxyConfig
import time

config = ProxyConfig(refresh_interval=3)
ws = Webshare(api_key="your-api-token", config=config)

proxies = ws.list_proxies() # calls API 

ws.list_proxies() # cached

time.sleep(5)
ws.list_proxies() # calls API since it's more than 3s later

Function Using Generic ProxyProvider

Since all proxy providers implement the same interface, we can make a function that allows us to easily swap out and utilize different providers. This is the main appeal of having a unified interface. It allows other modules to be provider agnostic, like my TikTokAPI package.

from proxyproviders import Webshare, BrightData, ProxyProvider, ProxyConfig

def some_function(provider: ProxyProvider):
    proxies = provider.list_proxies()
    print(proxies)

webshare = Webshare(api_key="your_api_key")
brightdata = BrightData(api_key="your_api_key", zone="my_zone")

some_function(webshare)
some_function(brightdata)

Here's a more meaningful example that takes the Proxy class and uses it to create a python requests http proxy.

from proxyproviders import Webshare, BrightData, ProxyProvider
import requests
import os


def request_with_proxy(provider: ProxyProvider):
    requests_proxy = None

    if provider:
        proxies = provider.list_proxies()

        requests_proxy = {
            "http": f"http://{proxies[0].username}:{proxies[0].password}@{proxies[0].proxy_address}:{proxies[0].port}",
            "https": f"http://{proxies[0].username}:{proxies[0].password}@{proxies[0].proxy_address}:{proxies[0].port}",
        }

    r = requests.get("https://httpbin.org/ip", proxies=requests_proxy)
    return r.json()

webshare = Webshare(api_key="your_api_key")
brightdata = BrightData(api_key="your_api_key", zone="your_zone")

print(f"Your IP: {request_with_proxy(None)}")
print(f"Webshare: {request_with_proxy(webshare)}")
print(f"BrightData: {request_with_proxy(brightdata)}")

Making Your Own Proxy Provider

Here's a skeleton of how you can make your very own ProxyProvider class. You'll need to implemenet all the required functions of the ProxyProvider which may be more than what's here at the time of writing.

If you do find yourself making one of these, consider contributing it back to the repository so everyone can use them :D

from proxyproviders import ProxyProvider, ProxyConfig, Proxy
from typing import List, Optional

class MyProxyProvider(ProxyProvider):
    def __init__(self, config: Optional[ProxyConfig] = None):
        super().__init__(config=config)

    def _fetch_proxies(self):
        proxies: List[Proxy] = []

        for i in range(10):
            # TODO: your real proxy fetching logic

            # There are required fields on the Proxy class, be sure that these are filled out properly
            # especially if you're using it with another library.
            proxy = Proxy(
                id=str(i),
                username="username",
                password="password",
                proxy_address="192.168.1.1",
                port=80,
            )

            proxies.append(proxy)

        return proxies

def some_function(provider: ProxyProvider):
    proxies = provider.list_proxies()
    for proxy in proxies:
        print(proxy)

provider = MyProxyProvider()
some_function(provider) # calls the function with the provider