Live: 23 providers tested · Updated Aug 2026
100% Independent · No Sponsored Rankings
Tutorial 8 min read Updated Jun 17, 2026 By ProxyAudit Editorial

Set Up Proxies in Python

A Python setup guide for requests, sessions, authentication, and rotation basics.

The 30-Second Answer

How to Set Up a Proxy in Python: A Complete Beginner-Friendly Guide

When building automation tools, web scrapers, API testing scripts, or data collection systems with Python, proxies are often an important part of the workflow. A proxy acts as an intermediary server between your Python script and the website or service you are trying to access.

Instead of sending a request directly from your real IP address, your script sends the request through a proxy server. The target website then sees the proxy IP address rather than your original one.

Setting up a proxy in Python is not difficult, but using it correctly requires understanding how proxies work, what types of proxies are available, and how to configure them with popular Python libraries such as requests, urllib, httpx, or Selenium.

What Is a Proxy in Python?

A proxy in Python is a network configuration that allows your Python program to route internet traffic through another server. When your script sends a request to a website, the request first goes to the proxy server. The proxy server then forwards the request to the target website and sends the response back to your script.

This can be useful for region testing, request distribution, server IP protection, availability monitoring, public data collection, SEO rank tracking, and price monitoring workflows.

Use proxies responsibly. Respect website terms, robots.txt rules, API limits, and local laws. A proxy is a technical tool, not permission to bypass security or access restricted systems.

Common Types of Proxies

Before setting up a proxy in Python, it helps to understand the main proxy types.

Type What It Means Best Fit
HTTP Proxy for standard HTTP traffic. Basic web requests and non-secure URLs.
HTTPS Proxy that supports secure web connections. Most modern websites and API requests.
SOCKS5 Protocol-flexible proxy that can handle more than HTTP traffic. Tools that explicitly support SOCKS routing.
Datacenter Fast server-hosted IPs. Low-risk targets where speed and price matter.
Residential ISP-associated consumer IPs. Geo testing, market research, and protected public targets.
Mobile Carrier-network IPs. Mobile app testing, ad verification, and mobile-sensitive workflows.

Basic Proxy Format

Most proxy providers give proxy details in one of these formats:

http://IP_ADDRESS:PORT
http://USERNAME:PASSWORD@IP_ADDRESS:PORT

For example:

http://123.45.67.89:8080
http://user123:pass456@123.45.67.89:8080

If your proxy requires authentication, include the username and password in the proxy URL. Some providers also support IP whitelisting, where your server IP is allowed without a username and password.

Setting Up a Proxy with Python Requests

The requests library is one of the most common ways to send HTTP requests in Python.

pip install requests

Basic proxy example:

import requests

proxies = {
    "http": "http://123.45.67.89:8080",
    "https": "http://123.45.67.89:8080"
}

response = requests.get("https://httpbin.org/ip", proxies=proxies, timeout=10)

print(response.text)

If the proxy works correctly, the returned IP should be the proxy IP instead of your real IP.

Using an Authenticated Proxy

Many paid proxy services require username and password authentication.

import requests

proxies = {
    "http": "http://username:password@123.45.67.89:8080",
    "https": "http://username:password@123.45.67.89:8080"
}

response = requests.get("https://httpbin.org/ip", proxies=proxies, timeout=10)

print(response.text)

A safer production approach is to load proxy credentials from environment variables instead of hardcoding them.

import os
import requests

proxy_user = os.getenv("PROXY_USER")
proxy_pass = os.getenv("PROXY_PASS")
proxy_host = os.getenv("PROXY_HOST")
proxy_port = os.getenv("PROXY_PORT")

proxy_url = f"http://{proxy_user}:{proxy_pass}@{proxy_host}:{proxy_port}"

proxies = {
    "http": proxy_url,
    "https": proxy_url
}

response = requests.get("https://httpbin.org/ip", proxies=proxies, timeout=10)

print(response.text)

Using SOCKS Proxies in Python

If your provider gives you a SOCKS5 proxy, install SOCKS support for requests.

pip install "requests[socks]"
import requests

proxies = {
    "http": "socks5://username:password@123.45.67.89:1080",
    "https": "socks5://username:password@123.45.67.89:1080"
}

response = requests.get("https://httpbin.org/ip", proxies=proxies, timeout=10)

print(response.text)

Rotating Proxies in Python

Proxy rotation means using a different proxy IP for different requests. This can help distribute traffic, but rotation should match the workflow and target rules.

import requests
import random

proxy_list = [
    "http://user:pass@111.111.111.111:8000",
    "http://user:pass@222.222.222.222:8000",
    "http://user:pass@333.333.333.333:8000"
]

url = "https://httpbin.org/ip"
proxy = random.choice(proxy_list)

proxies = {
    "http": proxy,
    "https": proxy
}

response = requests.get(url, proxies=proxies, timeout=10)

print(response.text)

For larger systems, track failed proxies, retry failed requests carefully, and avoid overusing the same IP address.

Handling Common Proxy Errors

Timeout errors usually mean the proxy is slow or unreachable. Connection errors may mean the host, port, or protocol is wrong. A 407 Proxy Authentication Required error usually means the credentials are incorrect.

import requests

proxies = {
    "http": "http://username:password@123.45.67.89:8080",
    "https": "http://username:password@123.45.67.89:8080"
}

try:
    response = requests.get(
        "https://httpbin.org/ip",
        proxies=proxies,
        timeout=10
    )
    response.raise_for_status()
    print(response.text)
except requests.exceptions.Timeout:
    print("The proxy connection timed out.")
except requests.exceptions.ProxyError:
    print("There was a proxy error.")
except requests.exceptions.RequestException as e:
    print(f"Request failed: {e}")

Best Practices for Using Proxies in Python

01

Pick the right proxy type

Use datacenter proxies for speed and low-risk targets. Use residential, ISP, or mobile proxies when reputation or location accuracy matters.

02

Use timeouts and retries

Set request timeouts, log failures, and retry with backoff instead of repeating rapidly.

03

Protect credentials

Store proxy usernames and passwords in environment variables or secret managers.

04

Respect limits

Follow website terms, API limits, and legal requirements. Proxies do not remove compliance responsibilities.

Conclusion

Setting up a proxy in Python is straightforward once you understand the basic structure. With requests, you can configure HTTP, HTTPS, and SOCKS proxies using a simple proxies dictionary.

For advanced workflows, add proxy rotation, failure handling, secure credential storage, and careful logging. A good proxy setup is not just about changing IP addresses; it is about building a stable and responsible request system.

How the workflow fits together

You
->
Proxy
->
Target Site
// Route requests through the right proxy type, then validate success rate, latency, and cost.

Proxy Context

OKUse case: web-scraping
OKLevel: Tutorial
OKTime: 8 min read

FAQ

Pass the full proxy URL, including username, password, host, and port, in the requests proxies dictionary for both http and https keys. Keep credentials in environment variables rather than hardcoding them.

Use HTTP or HTTPS proxies for most requests-based scraping. Use SOCKS5 when the target workflow or library needs broader protocol support, and install the requests SOCKS extra before testing.

Start with a short connect timeout and a slightly longer read timeout, then tune against your real target. Always set explicit timeouts so slow proxy endpoints do not stall the whole job.

Use rotation when repeated requests from one IP create rate limits, blocks, or biased geo results. For login or cart workflows, use sticky sessions instead of rotating every request.

Log status codes and exception types, retry with backoff, and cap retries per target. If failures cluster by provider, country, or proxy type, validate the provider settings before scaling traffic.

PA
ProxyAudit Editorial
ProxyAudit Educational Content

Guides are maintained alongside provider benchmarks and pricing data so educational advice stays tied to practical buying decisions.

Ready to pick a provider?

Compare proxy reviews, pricing, and benchmark fields before you buy traffic.

Browse Reviews