https://github.com/romis2012/httpx-socks
Proxy (HTTP, SOCKS) transports for httpx
https://github.com/romis2012/httpx-socks
asyncio http httpx proxy python socks4 socks5 trio
Last synced: about 2 months ago
JSON representation
Proxy (HTTP, SOCKS) transports for httpx
- Host: GitHub
- URL: https://github.com/romis2012/httpx-socks
- Owner: romis2012
- License: apache-2.0
- Created: 2020-07-09T11:49:15.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-12-10T06:08:19.000Z (6 months ago)
- Last Synced: 2025-04-01T11:06:13.191Z (2 months ago)
- Topics: asyncio, http, httpx, proxy, python, socks4, socks5, trio
- Language: Python
- Homepage:
- Size: 1.34 MB
- Stars: 81
- Watchers: 2
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# httpx-socks
[](https://github.com/romis2012/httpx-socks/actions/workflows/ci.yml)
[](https://codecov.io/gh/romis2012/httpx-socks)
[](https://pypi.python.org/pypi/httpx-socks)The `httpx-socks` package provides proxy transports for [httpx](https://github.com/encode/httpx) client.
SOCKS4(a), SOCKS5(h), HTTP (tunneling) proxy supported.
It uses [python-socks](https://github.com/romis2012/python-socks) for core proxy functionality.## Requirements
- Python >= 3.6
- httpx>=0.21.0
- python-socks>=2.0.0
- async-timeout>=3.0.1 (optional)
- trio>=0.16.0 (optional)## Installation
only sync proxy support:
```
pip install httpx-socks
```to include optional asyncio support (it requires async-timeout):
```
pip install httpx-socks[asyncio]
```to include optional trio support:
```
pip install httpx-socks[trio]
```## Usage
#### sync transport
```python
import httpx
from httpx_socks import SyncProxyTransportdef fetch(url):
transport = SyncProxyTransport.from_url('socks5://user:[email protected]:1080')
with httpx.Client(transport=transport) as client:
res = client.get(url)
return res.text
```#### async transport (asyncio, trio)
```python
import httpx
from httpx_socks import AsyncProxyTransportasync def fetch(url):
transport = AsyncProxyTransport.from_url('socks5://user:[email protected]:1080')
async with httpx.AsyncClient(transport=transport) as client:
res = await client.get(url)
return res.text
```#### secure proxy connections (aka "HTTPS proxies", experimental feature, both sync and async support)
```python
import ssl
import httpx
from httpx_socks import AsyncProxyTransportasync def fetch(url):
proxy_ssl = ssl.SSLContext(ssl.PROTOCOL_TLS)
proxy_ssl.verify_mode = ssl.CERT_REQUIRED
proxy_ssl.load_verify_locations(...)
transport = AsyncProxyTransport.from_url('http://user:[email protected]:8080', proxy_ssl=proxy_ssl)
async with httpx.AsyncClient(transport=transport) as client:
res = await client.get(url)
return res.text
```