多协议代理中转器,支持HTTP/HTTPS/SOCKS5/SOCKS5H协议互转,本地代理无需账密认证
Project description
Proxy Relay - Proxy Relay/Conversion
English | 简体中文
Proxy Relay is a pure-Python proxy relay/conversion tool that can convert upstream HTTP/HTTPS/SOCKS5/SOCKS5H proxies into local HTTP or SOCKS5 proxies. The local proxy itself does not require authentication.
Typical use cases: proxy relay for automation tools such as
Playwright, Selenium and DrissionPage.
Features
- Protocol conversion: supports 8 protocol combinations
- Sync & async APIs: suitable for scripts and async apps
- Auto cleanup: resources are released on process exit
- Thread/process safe: works in multithreaded and multiprocess scenarios
- Zero runtime dependencies: implemented in pure Python
Installation
pip install proxy-relay
To install the latest version from GitHub:
pip install "git+https://github.com/huazz233/proxy_relay.git"
Quick Start
from proxy_relay import create_proxy
import requests
# Create a local proxy from an upstream SOCKS5 proxy
url = create_proxy("socks5://user:pass@proxy.com:1080")
# Use the local proxy to access a test URL
resp = requests.get("https://api.ipify.org/", proxies={"http": url, "https": url})
print(resp.text)
Use Cases
Scripts / Tests
from proxy_relay import create_proxy
url = create_proxy("http://proxy.com:8080")
# Use the proxy...
# Resources will be cleaned up automatically when the process exits
Long-running Services
from proxy_relay import create_proxy, cleanup
url = create_proxy("http://proxy.com:8080")
# Use the proxy...
cleanup() # Manually cleanup in long-running services
Async Applications
import asyncio
from proxy_relay import create_proxy_async, HttpProxy
async def main():
# Simple: directly create a local proxy URL
url = await create_proxy_async("http://proxy.com:8080")
# Context manager: automatically start and stop the proxy
async with HttpProxy("http://proxy.com:8080") as proxy:
url = proxy.get_local_url()
# Use the proxy...
asyncio.run(main())
Playwright Integration (Python)
import asyncio
from proxy_relay import create_proxy_async
from playwright.async_api import ProxySettings, async_playwright
UPSTREAM_PROXY = "socks5://user:pass@proxy.com:1080"
TEST_URL = "https://api.ipify.org/"
async def main():
# Convert upstream proxy to a local HTTP proxy
local_url = await create_proxy_async(UPSTREAM_PROXY, local_type="http")
async with async_playwright() as p:
proxy: ProxySettings = {"server": local_url}
browser = await p.chromium.launch(proxy=proxy, headless=False)
page = await browser.new_page()
await page.goto(TEST_URL)
print(await page.text_content("body"))
await browser.close()
asyncio.run(main())
Selenium Integration (Python)
from proxy_relay import create_proxy
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
UPSTREAM_PROXY = "socks5://user:pass@proxy.com:1080"
TEST_URL = "https://api.ipify.org/"
def main():
# Create a local HTTP proxy
local_url = create_proxy(UPSTREAM_PROXY, local_type="http")
options = Options()
options.add_argument(f"--proxy-server={local_url}")
driver = webdriver.Chrome(options=options)
try:
driver.get(TEST_URL)
print(driver.page_source)
finally:
driver.quit()
if __name__ == "__main__":
main()
DrissionPage Integration (Python)
from proxy_relay import create_proxy
from DrissionPage import ChromiumPage, ChromiumOptions
UPSTREAM_PROXY = "socks5://user:pass@proxy.com:1080"
TEST_URL = "https://api.ipify.org/"
def main():
# Create a local HTTP proxy
local_url = create_proxy(UPSTREAM_PROXY, local_type="http")
options = ChromiumOptions()
# DrissionPage accepts a full proxy URL, e.g. http://127.0.0.1:12345
options.set_proxy(local_url)
page = ChromiumPage(options)
page.get(TEST_URL)
print(page.html)
page.quit()
if __name__ == "__main__":
main()
For more detailed integration examples, see
docs/integration-examples.md.
API Reference
Sync APIs
# Create a proxy
create_proxy(upstream_url, local_type="http", connect_timeout=30.0, idle_timeout=300.0, timeout=30.0)
create_http_proxy(upstream_url, ...) # Shortcut for HTTP proxies
create_socks5_proxy(upstream_url, ...) # Shortcut for SOCKS5 proxies
# Cleanup (optional, resources are also cleaned on process exit)
cleanup()
Async APIs
# Create a proxy
await create_proxy_async(upstream_url, local_type="http", ...)
await create_http_proxy_async(upstream_url, ...)
await create_socks5_proxy_async(upstream_url, ...)
# Context managers
async with HttpProxy(upstream_url) as proxy:
url = proxy.get_local_url()
async with Socks5Proxy(upstream_url) as proxy:
url = proxy.get_local_url()
Advanced APIs
# ProxyManager - manage multiple proxies
import asyncio
from proxy_relay import ProxyManager
async def main():
async with ProxyManager() as manager:
url = await manager.create(upstream_url, local_type="http")
await manager.stop(url) # Stop a single proxy
await manager.stop_all() # Stop all proxies
asyncio.run(main())
Supported Protocols
| Upstream | Local | Example |
|---|---|---|
| HTTP/HTTPS | HTTP/SOCKS5 | http://proxy.com:8080 |
| SOCKS5 | HTTP/SOCKS5 | socks5://user:pass@proxy.com:1080 |
| SOCKS5H | HTTP/SOCKS5 | socks5h://proxy.com:1080 |
Parameters
upstream_url: upstream proxy URLlocal_type: local proxy type ("http"or"socks5")connect_timeout: connection timeout in seconds (default 30)idle_timeout: idle timeout in seconds (default 300)timeout: creation timeout in seconds (default 30)
Multi-process / Multi-thread
- Fully thread-safe
- Safe for multi-process use (Windows / macOS default
spawnmode) - On Linux
forkmode, it is recommended not to create proxies before forking
FAQ
Q: Will the proxy keep running?
A: Yes, until the process exits or cleanup() is called.
Q: How to avoid resource leaks in long-running services?
A: Call cleanup() periodically or use ProxyManager to manage proxies.
Q: What's the difference between sync and async APIs?
A: Sync APIs are convenient for simple scripts; async APIs are more suitable for async applications (e.g. FastAPI).
License
MIT License
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file proxy_relay-1.3.0.tar.gz.
File metadata
- Download URL: proxy_relay-1.3.0.tar.gz
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aba9534dbb292834751d80104cee358195fe469dca6398607717712ef6397883
|
|
| MD5 |
21ddd2909653bf7f89a17acd1f0ad2ef
|
|
| BLAKE2b-256 |
6c17f0901e8cd2688118b00998b8e18c5fc768dcd489cbbcb4e0a2bf82af9fb2
|
Provenance
The following attestation bundles were made for proxy_relay-1.3.0.tar.gz:
Publisher:
publish-to-pypi.yml on huazz233/proxy_relay
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
proxy_relay-1.3.0.tar.gz -
Subject digest:
aba9534dbb292834751d80104cee358195fe469dca6398607717712ef6397883 - Sigstore transparency entry: 984724893
- Sigstore integration time:
-
Permalink:
huazz233/proxy_relay@fb57fc21aedf54f90c07b17ac666c81ae619a7c0 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/huazz233
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@fb57fc21aedf54f90c07b17ac666c81ae619a7c0 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file proxy_relay-1.3.0-py3-none-any.whl.
File metadata
- Download URL: proxy_relay-1.3.0-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c99c7ba541ed1d9fab060d8e936900c8dc80dc24c741c317543d86c987e5513
|
|
| MD5 |
54d38d9d14415d51d60e6ee0bc669a85
|
|
| BLAKE2b-256 |
f80e4370f4d9c1b3a3d56b04ca78e12c783299e136a117f4835f65a135a0fda7
|
Provenance
The following attestation bundles were made for proxy_relay-1.3.0-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on huazz233/proxy_relay
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
proxy_relay-1.3.0-py3-none-any.whl -
Subject digest:
2c99c7ba541ed1d9fab060d8e936900c8dc80dc24c741c317543d86c987e5513 - Sigstore transparency entry: 984724898
- Sigstore integration time:
-
Permalink:
huazz233/proxy_relay@fb57fc21aedf54f90c07b17ac666c81ae619a7c0 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/huazz233
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@fb57fc21aedf54f90c07b17ac666c81ae619a7c0 -
Trigger Event:
workflow_dispatch
-
Statement type: