Learn how to retrieve all proxies associated with a specific proxy user, handling service restrictions
This example demonstrates how to fetch all proxies associated with a specific proxy user ID. The process involves:
Retrieving the proxy user details to check for service restrictions
If the user is not service-restricted, fetching all proxies across all services
If the user is service-restricted, fetching proxies only from the services they have access to
The code handles pagination to ensure you get the complete list of proxies even when there are many results.
import requestsimport math# API credentialsAPI_PUBLIC_KEY ="your_public_key"API_PRIVATE_KEY ="your_private_key"BASE_URL ="https://api.pingproxies.com/1.0/public"# Headers for authenticationheaders ={"X-API-Public-Key": API_PUBLIC_KEY,"X-API-Private-Key": API_PRIVATE_KEY}# Set the proxy user ID you want to get proxies forproxy_user_id ="your_proxy_user_id"# Initialize the list to store all proxiesall_proxies =[]# Step 1: Retrieve the proxy user details to check for service restrictionsproxy_user_url =f"{BASE_URL}/user/proxy_user/retrieve/{proxy_user_id}"proxy_user_response = requests.get(proxy_user_url, headers=headers)if proxy_user_response.status_code !=200:print(f"Error retrieving proxy user: {proxy_user_response.status_code}") exit(1)proxy_user_data = proxy_user_response.json()["data"]is_service_restricted = proxy_user_data.get("proxy_user_is_service_restricted",False)# Step 2: Handle based on whether the user is service-restricted or notifnot is_service_restricted:# User is not service-restricted, get all proxies across all servicesprint(f"Proxy user {proxy_user_id} is not service-restricted. Fetching all proxies...")# Set up pagination variables page =1 per_page =100# You can adjust this value as needed has_more_pages =True# Use the search endpoint with paginationwhile has_more_pages: search_url =f"{BASE_URL}/user/proxy/search" params ={"page": page,"per_page": per_page}# Make the request search_response = requests.get(search_url, params=params, headers=headers)if search_response.status_code !=200:print(f"Error searching proxies on page {page}: {search_response.status_code}")break# Parse the response search_data = search_response.json() proxies = search_data.get("data",[])# Add the proxies to our list all_proxies.extend(proxies)# Check if there are more pages total_count = search_data.get("total_count",0) total_pages = math.ceil(total_count / per_page)if page >= total_pages: has_more_pages =Falseelse: page +=1else:# User is service-restricted, fetch proxies only from the restricted servicesprint(f"Proxy user {proxy_user_id} is service-restricted. Fetching proxies from restricted services...")# Get the list of restricted services restricted_service_ids =[]if"restricted_service_ids"in proxy_user_data:for service_item in proxy_user_data["restricted_service_ids"]:ifisinstance(service_item,dict)and"value"in service_item: restricted_service_ids.append(service_item["value"])elifisinstance(service_item,str): restricted_service_ids.append(service_item)# Fetch proxies for each restricted servicefor service_id in restricted_service_ids:print(f"Fetching proxies for service: {service_id}")# Set up pagination variables for this service page =1 per_page =100# You can adjust this value as needed has_more_pages =True# Use the search endpoint with pagination and service_id filterwhile has_more_pages: search_url =f"{BASE_URL}/user/proxy/search" params ={"service_id": service_id,"page": page,"per_page": per_page}# Make the request search_response = requests.get(search_url, params=params, headers=headers)if search_response.status_code !=200:print(f"Error searching proxies for service {service_id} on page {page}: {search_response.status_code}")break# Parse the response search_data = search_response.json() proxies = search_data.get("data",[])# Add the proxies to our list all_proxies.extend(proxies)# Check if there are more pages total_count = search_data.get("total_count",0) total_pages = math.ceil(total_count / per_page)if page >= total_pages: has_more_pages =Falseelse: page +=1# At this point, all_proxies contains all the proxies associated with the proxy userprint(f"Total proxies found: {len(all_proxies)}")# You can now work with the proxies as needed# Example: first_proxy = all_proxies[0] if all_proxies else None