Skip to content

Commit e4c8a7b

Browse files
committed
more python proxy examples
1 parent 818bfb4 commit e4c8a7b

7 files changed

Lines changed: 83 additions & 2 deletions

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,25 @@
1-
# proxy-examples
2-
Example code for using proxy servers in different programming languages
1+
# Proxy Examples
2+
3+
Example code for using proxy servers in different programming languages. Currently we have examples for these languages:
4+
5+
* Python
6+
* Ruby
7+
8+
## Python Proxy Examples
9+
10+
* [requests-proxy](https://github.com/proxymesh/proxy-examples/blob/main/python/requests-proxy.py)
11+
* [requests-random-proxy](https://github.com/proxymesh/proxy-examples/blob/main/python/requests-random-proxy.py)
12+
* [requests-proxy-headers](https://github.com/proxymesh/proxy-examples/blob/main/python/requests-proxy-headers.py)
13+
* [urllib3-proxy-headers](https://github.com/proxymesh/proxy-examples/blob/main/python/urllib3-proxy-headers.py)
14+
* [aiohttp-proxy-headers](https://github.com/proxymesh/proxy-examples/blob/main/python/aiohttp-proxy-headers.py)
15+
* [httpx-proxy-headers](https://github.com/proxymesh/proxy-examples/blob/main/python/httpx-proxy-headers.py)
16+
* [httpx-async-proxy-headers](https://github.com/proxymesh/proxy-examples/blob/main/python/httpx-async-proxy-headers.py)
17+
* [scrapy-proxy-headers](https://github.com/proxymesh/proxy-examples/blob/main/python/scrapy-proxy-headers.py)
18+
19+
## Ruby Proxy Examples
20+
21+
* requests_proxy
22+
23+
## Contributing
24+
25+
If you have example code for another language, please share it with a Pull Request.

python/aiohttp-proxy-headers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# See https://github.com/proxymesh/python-proxy-headers
2+
from python_proxy_headers import aiohttp_proxy
3+
4+
async with aiohttp_proxy.ProxyClientSession() as session:
5+
async with session.get('https://api.ipify.org?format=json', proxy="http://PROXYHOST:PORT", proxy_headers={'X-ProxyMesh-Country': 'US'}) as r:
6+
await r.text()
7+
8+
r.headers['X-ProxyMesh-IP']
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import httpx
2+
# See https://github.com/proxymesh/python-proxy-headers
3+
from python_proxy_headers.httpx_proxy import AsyncHTTPProxyTransport
4+
5+
proxy = httpx.Proxy('http://PROXYHOST:PORT', headers={'X-ProxyMesh-Country': 'US'})
6+
transport = AsyncHTTPProxyTransport(proxy=proxy)
7+
8+
async with httpx.AsyncClient(mounts={'http://': transport, 'https://': transport}) as client:
9+
r = await client.get('https://api.ipify.org?format=json')
10+
11+
r.headers['X-ProxyMesh-IP']

python/httpx-proxy-headers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import httpx
2+
# See https://github.com/proxymesh/python-proxy-headers
3+
from python_proxy_headers import httpx_proxy
4+
5+
proxy = httpx.Proxy('http://PROXYHOST:PORT', headers={'X-ProxyMesh-Country': 'US'})
6+
r = httpx_proxy.get('https://api.ipify.org?format=json', proxy=proxy)
7+
r.headers['X-ProxyMesh-IP']

python/requests-proxy-headers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# See https://github.com/proxymesh/python-proxy-headers
2+
from python_proxy_headers import requests_adapter
3+
4+
proxies = {
5+
# USERNAME:PASSWORD is optional if you have IP authentication
6+
'http': 'http://USERNAME:PASSWORD@HOST:PORT',
7+
'https': 'http://USERNAME:PASSWORD@HOST:PORT'
8+
}
9+
10+
r = requests_adapter.get('https://api.ipify.org?format=json',
11+
proxies=proxies,
12+
proxy_headers={'X-ProxyMesh-Country': 'US'})
13+
14+
r.headers['X-ProxyMesh-IP']

python/scrapy-proxy-headers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# See https://github.com/proxymesh/scrapy-proxy-headers
2+
3+
# In your Scrapy `settings.py`, add the following:
4+
DOWNLOAD_HANDLERS = {
5+
"https": "scrapy_proxy_headers.HTTP11ProxyDownloadHandler"
6+
}
7+
8+
# add to your request procesing method
9+
request.meta["proxy_headers"] = {"X-ProxyMesh-Country": "US"}
10+
11+
# then when you get a response
12+
response.headers["X-ProxyMesh-IP"]

python/urllib3-proxy-headers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# See https://github.com/proxymesh/python-proxy-headers
2+
from python_proxy_headers import urllib3_proxy_manager
3+
4+
proxy = urllib3_proxy_manager.ProxyHeaderManager('http://PROXYHOST:PORT', proxy_headers={'X-ProxyMesh-Country': 'US'})
5+
r = proxy.request('GET', 'https://api.ipify.org?format=json')
6+
r.headers['X-ProxyMesh-IP']

0 commit comments

Comments
 (0)