ProxyPool is a free, open source databases project written in Python and released under MIT. It has 6,249 GitHub stars, 2,228 forks and 2 open issues, and was last pushed 3 months ago. On this registry it ranks #103 of 143 tracked projects in Databases, with 5 head-to-head comparisons available.

What is ProxyPool?

ProxyPool is an MIT-licensed Python proxy pool that continuously scrapes free public proxy sources, validates them through scheduled testing, stores them in Redis, and serves working proxies over a random-selection HTTP API for web scraping and spider projects.

What it is

ProxyPool is a self-hosted service built from three cooperating processes: a Getter that periodically crawls free public proxy websites, a Tester that periodically checks and filters the collected candidates, and a Server that exposes an HTTP API for retrieving proxies. It lives in the Python web-scraping ecosystem alongside tools such as Flask and Redis, and it is aimed at developers learning how proxy pools are engineered or who want a local source of rotating proxies under their own control.

The concrete problem it solves is proxy supply and rotation. Rather than hardcoding a single proxy or managing a list by hand, a crawler asks ProxyPool for a random validated proxy and receives one that passed the most recent test cycle. What it replaces is the manual workflow of collecting free proxy lists and checking them one at a time. The README is explicit that the pool is composed of publicly available free proxy sources, that availability is consequently low, and that it is not intended to be used directly as the proxy layer for production crawling jobs.

Key capabilities

  • Scheduled scraping of free proxy websites through an extensible Getter component.
  • Storage and availability ranking of proxies in Redis.
  • Periodic testing and filtering that removes unusable proxies and keeps working ones.
  • HTTP proxy API returning a randomly selected validated proxy at http://localhost:5555/random.
  • Docker image germey/proxypool and a docker-compose up deployment that starts Redis, Getter, Server, and Tester under Supervisor.
  • Redis configuration through PROXYPOOL_REDIS_HOST, PROXYPOOL_REDIS_PORT, PROXYPOOL_REDIS_PASSWORD, and PROXYPOOL_REDIS_DB, or a single PROXYPOOL_REDIS_CONNECTION_STRING in the form redis://[:password@]host[:port][/database].
  • Component-level startup via python3 run.py --processor getter, --processor tester, or --processor server.

Who uses it and how

  • Developers studying proxy pool architecture, since the README points to a written explanation of how the pool works and recommends reading it before use.
  • Scraping engineers who need a rotating pool for low-volume or experimental spider runs rather than for heavy production crawling.
  • Teams wanting control over the proxy lifecycle: the Getter, Tester, and Server can be run as separate processes so collection, validation, and serving can be scaled or scheduled independently.
  • Operators who prefer containerized deployment and run the whole stack through Docker Compose, or who run it against a local, Docker-hosted, or remote Redis instance.

Getting started

The recommended path is Docker: install Docker and Docker Compose, then run docker-compose up from the cloned repository, which starts Redis alongside the Getter, Server, and Tester. Without Docker, install Python 3.6 or newer and Redis, run pip3 install -r requirements.txt, configure the Redis environment variables, then start everything with python3 run.py.

How it compares

The README contrasts the project directly with paid proxy services, naming ADSL dial-up proxies, overseas and global proxies, and cellular 4G/5G proxies as alternatives to use when the goal is completing a crawling task quickly. Those services sell guaranteed, higher-quality proxy capacity, while ProxyPool is MIT-licensed and free to run on infrastructure you own, with data staying in your own Redis instance and no per-request billing. The trade-off is quality and effort: the paid options cost money, whereas ProxyPool costs the operational work of running it and accepts a much lower hit rate from free sources.

When to use it — and when not to

Choose ProxyPool when the goal is learning how a proxy pool is built or when a self-maintained, free-source pool is acceptable for lightweight scraping. A self-hoster must operate Redis and keep the Docker or Python environment running, and should expect that only a small fraction of collected proxies will pass testing. Do not pick it as the proxy layer for time-sensitive or large-scale crawling, and note that the README is written in Chinese and that the project documentation directs readers to external paid services for serious workloads.

project readme (upstream, from github) — read inline

ProxyPool

build deploy Docker Pulls

简易高效的代理池,提供如下功能:

  • 定时抓取免费代理网站,简易可扩展。
  • 使用 Redis 对代理进行存储并对代理可用性进行排序。
  • 定时测试和筛选,剔除不可用代理,留下可用代理。
  • 提供代理 API,随机取用测试通过的可用代理。

代理池原理解析可见「如何搭建一个高效的代理池」,建议使用之前阅读。

使用前注意

本代理池是基于市面上各种公开代理源搭建的,所以可用性并不高,很可能上百上千个代理中才能找到一两个可用代理,不适合直接用于爬虫爬取任务。

如果您的目的是为了尽快使用代理完成爬取任务,建议您对接一些付费代理或者直接使用已有代理资源;如果您的目的是为了学习如何搭建一个代理池,您可以参考本项目继续完成后续步骤。

付费代理推荐:

使用准备

首先当然是克隆代码并进入 ProxyPool 文件夹:

git clone https://github.com/Python3WebSpider/ProxyPool.git
cd ProxyPool

然后选用下面 Docker 和常规方式任意一个执行即可。

使用要求

可以通过两种方式来运行代理池,一种方式是使用 Docker(推荐),另一种方式是常规方式运行,要求如下:

Docker

如果使用 Docker,则需要安装如下环境:

  • Docker
  • Docker-Compose

安装方法自行搜索即可。

官方 Docker Hub 镜像:germey/proxypool

常规方式

常规方式要求有 Python 环境、Redis 环境,具体要求如下:

  • Python>=3.6
  • Redis

Docker 运行

如果安装好了 Docker 和 Docker-Compose,只需要一条命令即可运行。

docker-compose up

运行结果类似如下:

redis        | 1:M 19 Feb 2020 17:09:43.940 * DB loaded from disk: 0.000 seconds
redis        | 1:M 19 Feb 2020 17:09:43.940 * Ready to accept connections
proxypool    | 2020-02-19 17:09:44,200 CRIT Supervisor is running as root.  Privileges were not dropped because no user is specified in the config file.  If you intend to run as root, you can set user=root in the config file to avoid this message.
proxypool    | 2020-02-19 17:09:44,203 INFO supervisord started with pid 1
proxypool    | 2020-02-19 17:09:45,209 INFO spawned: 'getter' with pid 10
proxypool    | 2020-02-19 17:09:45,212 INFO spawned: 'server' with pid 11
proxypool    | 2020-02-19 17:09:45,216 INFO spawned: 'tester' with pid 12
proxypool    | 2020-02-19 17:09:46,596 INFO success: getter entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
proxypool    | 2020-02-19 17:09:46,596 INFO success: server entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
proxypool    | 2020-02-19 17:09:46,596 INFO success: tester entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)

可以看到 Redis、Getter、Server、Tester 都已经启动成功。

这时候访问 http://localhost:5555/random 即可获取一个随机可用代理。

如果下载速度特别慢,可以自行修改 Dockerfile,修改:

- RUN pip install -r requirements.txt
+ RUN pip install -r requirements.txt -i https://pypi.douban.com/simple

常规方式运行

如果不使用 Docker 运行,配置好 Python、Redis 环境之后也可运行,步骤如下。

安装和配置 Redis

本地安装 Redis、Docker 启动 Redis、远程 Redis 都是可以的,只要能正常连接使用即可。

首先可以需要一下环境变量,代理池会通过环境变量读取这些值。

设置 Redis 的环境变量有两种方式,一种是分别设置 host、port、password,另一种是设置连接字符串,设置方法分别如下:

设置 host、port、password,如果 password 为空可以设置为空字符串,示例如下:

export PROXYPOOL_REDIS_HOST='localhost'
export PROXYPOOL_REDIS_PORT=6379
export PROXYPOOL_REDIS_PASSWORD=''
export PROXYPOOL_REDIS_DB=0

或者只设置连接字符串:

export PROXYPOOL_REDIS_CONNECTION_STRING='redis://localhost'

这里连接字符串的格式需要符合 redis://[:password@]host[:port][/database] 的格式, 中括号参数可以省略,port 默认是 6379,database 默认是 0,密码默认为空。

以上两种设置任选其一即可。

安装依赖包

这里强烈推荐使用 Condavirtualenv 创建虚拟环境,Python 版本不低于 3.6。

然后 pip 安装依赖即可:

pip3 install -r requirements.txt

运行代理池

两种方式运行代理池,一种是 Tester、Getter、Server 全部运行,另一种是按需分别运行。

一般来说可以选择全部运行,命令如下:

python3 run.py

运行之后会启动 Tester、Getter、Server,这时访问 http://localhost:5555/random 即可获取一个随机可用代理。

或者如果你弄清楚了代理池的架构,可以按需分别运行,命令如下:

python3 run.py --processor getter
python3 run.py --processor tester
python3 run.py --processor server

这里 processor 可以指定运行 Tester、Getter 还是 Server。

使用

成功运行之后可以通过 http://localhost:5555/random 获取一个随机可用代理。

可以用程序对接实现,下面的示例展示了获取代理并爬取网页的过程:

import requests

proxypool_url = 'http://127.0.0.1:5555/random'
target_url = 'http://httpbin.org/get'

def get_random_proxy():
    """
    get random proxy from proxypool
    :return: proxy
    """
    return requests.get(proxypool_url).text.strip()

def crawl(url, proxy):
    """
    use proxy to crawl page
    :param url: page url
    :param proxy: proxy, such as 8.8.8.8:8888
    :return: html
    """
    proxies = {'http': 'http://' + proxy}
    return requests.get(url, proxies=proxies).text


def main():
    """
    main method, entry point
    :return: none
    """
    proxy = get_random_proxy()
    print('get random proxy', proxy)
    html = crawl(target_url, proxy)
    print(html)

if __name__ == '__main__':
    main()

运行结果如下:

get random proxy 116.196.115.209:8080
{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.22.0",
    "X-Amzn-Trace-Id": "Root=1-5e4d7140-662d9053c0a2e513c7278364"
  },
  "origin": "116.196.115.209",
  "url": "https://httpbin.org/get"
}

可以看到成功获取了代理,并请求 httpbin.org 验证了代理的可用性。

获取多个代理

如果一次需要多个代理,可以给 /random 接口传入 count 参数,一次返回多个随机代理(每行一个):

GET http://localhost:5555/random?count=5

count 不传或为 1 时行为不变,仍返回单个代理;count 大于可用数量时返回全部可用代理。也可与 key 参数组合使用。

按地区(国家)筛选代理

可以给 /random/all 接口传入 area 参数,按代理 IP 所属国家筛选(ISO 国家码,大小写不敏感),例如只获取国内(中国)代理:

GET http://localhost:5555/random?area=CN
GET http://localhost:5555/all?area=CN

国家信息由内置的 GeoLite2 离线库解析,无法解析归属地的代理会被排除。area 可与 countkey 参数组合使用。

可配置项

代理池可以通过设置环境变量来配置一些参数。

开关

  • ENABLE_TESTER:允许 Tester 启动,默认 true
  • ENABLE_GETTER:允许 Getter 启动,默认 true
  • ENABLE_SERVER:运行 Server 启动,默认 true

环境

  • APP_ENV:运行环境,可以设置 dev、test、prod,即开发、测试、生产环境,默认 dev
  • APP_DEBUG:调试模式,可以设置 true 或 false,默认 true
  • APP_PROD_METHOD: 正式环境启动应用方式,默认是gevent, 可选:tornadomeinheld(分别需要安装 tornado 或 meinheld 模块)

Redis 连接

  • PROXYPOOL_REDIS_HOST / REDIS_HOST:Redis 的 Host,其中 PROXYPOOL_REDIS_HOST 会覆盖 REDIS_HOST 的值。
  • PROXYPOOL_REDIS_PORT / REDIS_PORT:Redis 的端口,其中 PROXYPOOL_REDIS_PORT 会覆盖 REDIS_PORT 的值。
  • PROXYPOOL_REDIS_PASSWORD / REDIS_PASSWORD:Redis 的密码,其中 PROXYPOOL_REDIS_PASSWORD 会覆盖 REDIS_PASSWORD 的值。
  • PROXYPOOL_REDIS_DB / REDIS_DB:Redis 的数据库索引,如 0、1,其中 PROXYPOOL_REDIS_DB 会覆盖 REDIS_DB 的值。
  • PROXYPOOL_REDIS_CONNECTION_STRING / REDIS_CONNECTION_STRING:Redis 连接字符串,其中 PROXYPOOL_REDIS_CONNECTION_STRING 会覆盖 REDIS_CONNECTION_STRING 的值。
  • PROXYPOOL_REDIS_KEY / REDIS_KEY:Redis 储存代理使用字典的名称,其中 PROXYPOOL_REDIS_KEY 会覆盖 REDIS_KEY 的值。

处理器

  • CYCLE_TESTER:Tester 运行周期,即间隔多久运行一次测试,默认 20 秒
  • CYCLE_GETTER:Getter 运行周期,即间隔多久运行一次代理获取,默认 100 秒
  • TEST_URL:测试 URL,默认百度
  • TEST_TIMEOUT:测试超时时间,默认 10 秒
  • TEST_BATCH:批量测试数量,默认 20 个代理
  • TEST_VALID_STATUS:测试有效的状态码
  • TEST_ANONYMOUS:是否只保留匿名代理,默认 true
  • TEST_ANONYMOUS_URL:匿名 / 出口 IP 检测地址,默认 https://httpbin.org/ip,需返回 httpbin 格式的 JSON({"origin": "1.2.3.4"})。可指向自建 httpbin 服务以避免公共服务限流
  • API_HOST:代理 Server 运行 Host,默认 0.0.0.0
  • API_PORT:代理 Server 运行端口,默认 5555
  • API_THREADED:代理 Server 是否使用多线程,默认 true
  • API_KEY:API 访问鉴权密钥,默认空(即不鉴权)。设置后,调用 /random/all/count 需在请求头携带 API-KEY,详见下方「安全性」说明

⚠️ 安全提示:代理 Server 默认监听 0.0.0.0API_KEY 默认为空,任何能访问该端口的人都可以调用 /random/all/count。如果将代理池暴露到公网,请务必设置 API_KEY,并配合防火墙/安全组限制来源。key 查询参数已做格式校验,仅允许字母、数字及 _ : -,最长 64 位。

日志

  • LOG_DIR:日志相对路径
  • LOG_RUNTIME_FILE:运行日志文件名称
  • LOG_ERROR_FILE:错误日志文件名称
  • LOG_ROTATION: 日志记录周转周期或大小,默认 500MB,见 loguru - rotation
  • LOG_RETENTION: 日志保留日期,默认 7 天,见 loguru - retention
  • ENABLE_LOG_FILE:是否输出 log 文件,默认 true,如果设置为 false,那么 ENABLE_LOG_RUNTIME_FILE 和 ENABLE_LOG_ERROR_FILE 都不会生效
  • ENABLE_LOG_RUNTIME_FILE:是否输出 runtime log 文件,默认 true
  • ENABLE_LOG_ERROR_FILE:是否输出 error log 文件,默认 true

以上内容均可使用环境变量配置,即在运行前设置对应环境变量值即可,如更改测试地址和 Redis 键名:

export TEST_URL=http://weibo.cn
export REDIS_KEY=proxies:weibo

即可构建一个专属于微博的代理池,有效的代理都是可以爬取微博的。

如果使用 Docker-Compose 启动代理池,则需要在 docker-compose.yml 文件里面指定环境变量,如:

version: "3"
services:
  redis:
    image: redis:alpine
    container_name: redis
    command: redis-server
    ports:
      - "6379:6379"
    restart: always
  proxypool:
    build: .
    image: "germey/proxypool"
    container_name: proxypool
    ports:
      - "5555:5555"
    restart: always
    envi

readme truncated — read the full docs on github

Frequently asked questions

Is ProxyPool free to use?

ProxyPool is open source under the MIT licence. There is no licence fee and no seat count — you can self-host it or, where the project offers one, pay a vendor for a managed version instead.

What does ProxyPool do?

An Efficient ProxyPool with Getter, Tester and Server

What is ProxyPool written in?

ProxyPool is primarily written in Python. Its source is publicly available at https://github.com/Python3WebSpider/ProxyPool, and it has 6,249 GitHub stars.