Configuring python to use Proxy Servers

Below are example Python scripts for testing HTTP proxies using either static IP authentication or username/password authentication.
These scripts fetch a random user agent from Trusted Proxies’ user agent list and use it for the request.
You can set your proxy details and the target URL directly in the script.
The script will print the HTTP status code and status message for the request, or an error if the request fails.

 

Static IP Authentication Example:

import requests
import random

# Set your proxy details and test URL here
proxy_host = "your_proxy_host"
proxy_port = "your_proxy_port"
url = "https://example.com" # The URL you want to test through the proxy
ua_url = "https://customers.trustedproxies.com/downloads/desktop_useragents.txt"

# Fetch a random user agent from Trusted Proxies' list
try:
ua_response = requests.get(ua_url, timeout=10)
user_agents = [ua.strip() for ua in ua_response.text.splitlines() if ua.strip()]
user_agent = random.choice(user_agents)
except Exception as e:
raise RuntimeError(f"Failed to fetch user agents: {e}")

headers = {"User-Agent": user_agent}
proxies = {
"http": f"http://{proxy_host}:{proxy_port}",
"https": f"http://{proxy_host}:{proxy_port}",
}

try:
print("Testing proxy with static IP authentication...")
response = requests.get(url, proxies=proxies, headers=headers, timeout=10)
print(f"Status Code: {response.status_code}")
print(f"Status: {response.reason}")
except Exception as e:
print("Status: Failed")
print("Error:", e)

 

Username/Password Authentication Example:

import requests
import random

# Set your proxy details and test URL here
proxy_user = "your_username"
proxy_pass = "your_password"
proxy_host = "your_proxy_host"
proxy_port = "your_proxy_port"
url = "https://example.com" # The URL you want to test through the proxy
ua_url = "https://customers.trustedproxies.com/downloads/desktop_useragents.txt"

# Fetch a random user agent from Trusted Proxies' list
try:
ua_response = requests.get(ua_url, timeout=10)
user_agents = [ua.strip() for ua in ua_response.text.splitlines() if ua.strip()]
user_agent = random.choice(user_agents)
except Exception as e:
raise RuntimeError(f"Failed to fetch user agents: {e}")

headers = {"User-Agent": user_agent}
proxies = {
"http": f"http://{proxy_user}:{proxy_pass}@{proxy_host}:{proxy_port}",
"https": f"http://{proxy_user}:{proxy_pass}@{proxy_host}:{proxy_port}",
}

try:
print("Testing proxy with username/password authentication...")
response = requests.get(url, proxies=proxies, headers=headers, timeout=10)
print(f"Status Code: {response.status_code}")
print(f"Status: {response.reason}")
except Exception as e:
print("Status: Failed")
print("Error:", e)

Instructions:

 

Replace the placeholder values (your_proxy_host, your_proxy_port, your_username, your_password, and https://example.com) with your actual proxy and target details.

 

The script will print the HTTP status code and message, or an error if the request fails.


These scripts are useful for quickly verifying your proxy setup and connectivity.

  • python
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Configure Opera to Use a Proxy Server

All of our Proxy Server configuration settings can now be found here:   How To Set Up A Proxy...

Configure Microsoft Edge to Use a Proxy Server

All of our Proxy Server configuration settings can now be found here:   How To Set Up A Proxy...

Configuring PHP to use Proxy Servers

If you have a script that needs to send traffic via a Proxy Server, one of the best options is to...

Configure Google Chrome to Use a Proxy Server

All of our Proxy Server configuration settings can now be found here:   How To Set Up A Proxy...

Configure Market Samurai to Use Proxy Servers

This solution has been tested for WebHarvy (v3.4.0.119) To add your Trusted Proxies proxy...