Skip to content

Instantly share code, notes, and snippets.

@pythoninthegrass
Created February 12, 2025 01:32
Show Gist options
  • Save pythoninthegrass/a8f575c775cd208fba7d7f45e3878083 to your computer and use it in GitHub Desktop.
Save pythoninthegrass/a8f575c775cd208fba7d7f45e3878083 to your computer and use it in GitHub Desktop.
Proxy requests from a free list at https://api.proxyscrape.com
#!/usr/bin/env python
import json
import requests
from pathlib import Path
from requests.exceptions import RequestException
from urllib.parse import urlencode
def get_my_ip():
response = requests.get('http://httpbin.org/ip')
return response.json()['origin']
def get_proxy():
base_url = 'https://api.proxyscrape.com/v4/free-proxy-list/get'
params = {
'request': 'display_proxies',
'country': 'us',
'protocol': 'http',
'proxy_format': 'ipport',
'format': 'json',
'timeout': 420
}
url = f"{base_url}?{urlencode(params)}"
raw = requests.get(url)
proxy_data = json.loads(raw.content)
return sorted(proxy_data['proxies'],
key=lambda x: (
{'elite': 0, 'anonymous': 1, 'transparent': 2}.get(x['anonymity'], 3),
x['timeout']
))
def send_request():
original_ip = get_my_ip()
print(f"{'Original IP:':<20} {original_ip}")
sorted_proxies = get_proxy()
proxy_ips = {proxy['ip'] for proxy in sorted_proxies}
for proxy in sorted_proxies:
proxy_entry = proxy['proxy']
proxies = {
'http': f'http://{proxy_entry}',
'https': f'https://{proxy_entry}'
}
try:
response = requests.get(
url="http://httpbin.org/ip",
proxies=proxies,
verify=False,
timeout=10
)
if response.status_code == 200:
proxied_ip = response.json()['origin'].split(',')[0].strip()
print(f"{'Proxied IP:':<20} {proxied_ip}")
if proxied_ip in proxy_ips and proxied_ip != original_ip:
print(f"{'Proxy verification:':<20} Success")
return
else:
print(f"{'Proxy verification:':<20} Failed")
except (RequestException, ValueError):
continue
print("No working proxies found")
send_request()
@pythoninthegrass
Copy link
Author

Original IP:         <SNIP>
Proxied IP:          147.182.180.242
Proxy verification:  Success

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment