Skip to content

Commit 3cf2006

Browse files
committed
add example
1 parent 822da37 commit 3cf2006

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# ProxyProviders
2-
Unified python interface for proxy provider APIs, supports BrightData, WebShare, and more.
2+
3+
The Unified Python Proxy API with support for BrightData, WebShare, and more.
34

45
[![LinkedIn](https://img.shields.io/badge/LinkedIn-0077B5?style=for-the-badge&logo=linkedin&logoColor=white&style=flat-square)](https://www.linkedin.com/in/davidteather/) [![Sponsor Me](https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub)](https://github.com/sponsors/davidteather) [![GitHub release (latest by date)](https://img.shields.io/github/v/release/davidteather/proxyproviders)](https://github.com/davidteather/proxyproviders/releases) [![GitHub](https://img.shields.io/github/license/davidteather/proxyproviders)](https://github.com/davidteather/proxyproviders/blob/main/LICENSE) [![Downloads](https://pepy.tech/badge/proxyproviders)](https://pypi.org/project/proxyproviders/) ![](https://visitor-badge.laobi.icu/badge?page_id=davidteather.proxyproviders) [![Support Server](https://img.shields.io/discord/783108952111579166.svg?color=7289da&logo=discord&style=flat-square)](https://discord.gg/yyPhbfma6f)
56

@@ -91,7 +92,7 @@ ws.list_proxies() # calls API since it's more than 3s later
9192
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](https://github.com/davidteather/TikTok-Api) package.
9293

9394
```py
94-
from proxyproviders import Webshare, ProxyProvider, ProxyConfig
95+
from proxyproviders import Webshare, BrightData, ProxyProvider, ProxyConfig
9596

9697
def some_function(provider: ProxyProvider):
9798
proxies = provider.list_proxies()
@@ -104,6 +105,36 @@ some_function(webshare)
104105
some_function(brightdata)
105106
```
106107

108+
Here's a more meaningful example that takes the `Proxy` class and uses it to create a python requests http proxy.
109+
110+
```py
111+
from proxyproviders import Webshare, BrightData, ProxyProvider
112+
import requests
113+
import os
114+
115+
116+
def request_with_proxy(provider: ProxyProvider):
117+
requests_proxy = None
118+
119+
if provider:
120+
proxies = provider.list_proxies()
121+
122+
requests_proxy = {
123+
"http": f"http://{proxies[0].username}:{proxies[0].password}@{proxies[0].proxy_address}:{proxies[0].port}",
124+
"https": f"http://{proxies[0].username}:{proxies[0].password}@{proxies[0].proxy_address}:{proxies[0].port}",
125+
}
126+
127+
r = requests.get("https://httpbin.org/ip", proxies=requests_proxy)
128+
return r.json()
129+
130+
webshare = Webshare(api_key="your_api_key")
131+
brightdata = BrightData(api_key="your_api_key", zone="your_zone")
132+
133+
print(f"Your IP: {request_with_proxy(None)}")
134+
print(f"Webshare: {request_with_proxy(webshare)}")
135+
print(f"BrightData: {request_with_proxy(brightdata)}")
136+
```
137+
107138
### Making Your Own Proxy Provider
108139

109140
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.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from proxyproviders import Webshare, BrightData, ProxyProvider
2+
import requests
3+
import os
4+
5+
6+
def request_with_proxy(provider: ProxyProvider):
7+
requests_proxy = None
8+
9+
if provider:
10+
proxies = provider.list_proxies()
11+
12+
requests_proxy = {
13+
"http": f"http://{proxies[0].username}:{proxies[0].password}@{proxies[0].proxy_address}:{proxies[0].port}",
14+
"https": f"http://{proxies[0].username}:{proxies[0].password}@{proxies[0].proxy_address}:{proxies[0].port}",
15+
}
16+
17+
r = requests.get("https://httpbin.org/ip", proxies=requests_proxy)
18+
return r.json()
19+
20+
webshare = Webshare(api_key="your_api_key")
21+
brightdata = BrightData(api_key="your_api_key", zone="your_zone")
22+
23+
print(f"Your IP: {request_with_proxy(None)}")
24+
print(f"Webshare: {request_with_proxy(webshare)}")
25+
print(f"BrightData: {request_with_proxy(brightdata)}")

0 commit comments

Comments
 (0)