Python
    rest-api

    Python Requests Proxy with Authentication Guide

    Step-by-step tutorial on configuring HTTP, HTTPS, and SOCKS proxies with username/password credentials in Python Requests. Complete python code script.

    0 views
    Updated 7/6/2026

    Official Documentation Reference

    For in-depth explanations, configuration options, and standards compliance, visit the official documentation:

    Python Requests Documentation

    Ready to test this code?

    Load this example into the app

    Code Example

    Copy and run
    import requests
    
    proxies = {
        'http': 'http://user:pass@proxy_ip:port',
        'https': 'http://user:pass@proxy_ip:port',
    }
    
    try:
        response = requests.get('https://httpbin.org/ip', proxies=proxies, timeout=10)
        print("IP address:", response.json()['origin'])
    except requests.exceptions.RequestException as e:
        print(f"Failed: {e}")

    Overview

    Python Requests Proxy with Authentication Guide

    When building web scrapers, data pipelines, or secure server integrations, routing your traffic through proxies is critical to bypass IP blocks, bypass rate-limiting, and mask source credentials. The standard Python requests library provides native support for HTTP, HTTPS, and SOCKS proxies, including basic credentials authentication.

    1. Setting Up basic Proxy Authentication

    To configure proxy credentials (username and password), embed them directly inside the proxy connection string using the http://username:password@ip:port format.

    import requests
    
    # Configure proxy with credentials
    proxies = {
        'http': 'http://my_proxy_user:my_proxy_password@104.248.60.25:8080',
        'https': 'http://my_proxy_user:my_proxy_password@104.248.60.25:8080',
    }
    
    try:
        # Send request through the proxy
        response = requests.get(
            'https://httpbin.org/ip', 
            proxies=proxies, 
            timeout=10
        )
        response.raise_for_status()
        print("Success! Routed IP:", response.json()['origin'])
    except requests.exceptions.RequestException as e:
        print(f"Proxy request failed: {e}")
    

    2. Using SOCKS5 Proxies with Authentication

    SOCKS5 proxies are more versatile than HTTP proxies because they support arbitrary TCP connections. To use SOCKS proxies, install the optional PySocks dependency (pip install requests[socks]) and change the scheme to socks5:

    import requests
    
    proxies = {
        'http': 'socks5://user:pass@192.168.1.100:1080',
        'https': 'socks5://user:pass@192.168.1.100:1080'
    }
    
    response = requests.get('https://httpbin.org/ip', proxies=proxies)
    print(response.json())
    

    3. Configuring Proxies via Environment Variables

    Instead of hardcoding proxy configurations in your code, you can use standard environment variables. Python requests automatically detects and respects these variables if they are present on your system.

    Set Environment Variables:

    export HTTP_PROXY="http://user:pass@proxy.example.com:8080"
    export HTTPS_PROXY="http://user:pass@proxy.example.com:8080"
    

    Run Code cleanly:

    import requests
    
    # Requests automatically routes through HTTP_PROXY / HTTPS_PROXY variables
    response = requests.get('https://httpbin.org/ip')
    print(response.json())
    

    To skip environment proxies inside the code, instantiate an empty Session object or specify trust_env=False:

    session = requests.Session()
    session.trust_env = False
    

    References

    Frequently Asked Questions

    Q: What error is thrown if proxy credentials are wrong? A: If authentication fails, requests throws a ProxyError wrapping an HTTP 407 Proxy Authentication Required status code.

    Q: Can I use different proxies for HTTP and HTTPS? A: Yes. The proxies dictionary allows you to map different endpoints to the http and https keys respectively.

    Related Topics

    python
    requests
    proxy
    proxy-authentication
    scraping
    security

    Ready to test APIs like a pro?

    Apicurl is a free, powerful API testing tool.