Python Requests proxy setup: HTTP and SOCKS5

Pass a proxies dictionary to requests.get(). Its keys select the destination scheme; its values describe how to connect to the proxy.

On this page

Use an HTTP proxy for an HTTPS request

Install Requests with python -m pip install requests. Choose a recent endpoint from the HTTPS CONNECT list and replace the documentation address below.

Python example
import requests

# Documentation address: replace it with an endpoint from the HTTPS list.
proxy = "http://203.0.113.10:8080"
proxies = {"http": proxy, "https": proxy}

try:
    response = requests.get(
        "https://example.com",
        proxies=proxies,
        timeout=(5, 15),
    )
    response.raise_for_status()
    print(response.status_code)
except requests.exceptions.SSLError:
    print("TLS check failed. Keep verification enabled; try another endpoint.")
except requests.exceptions.ProxyError:
    print("Proxy connection failed. Check its address, port and protocol.")
except requests.exceptions.Timeout:
    print("The connection or response timed out.")
except requests.exceptions.RequestException as error:
    print(type(error).__name__)

The dictionary’s https key describes the destination. The http:// value connects to an HTTP proxy that can establish a CONNECT tunnel. These are different settings.

Set connection and read timeouts

timeout=(5, 15) sets a five-second connection timeout and a fifteen-second read timeout. It is not a fifteen-second deadline for the entire download. Keep Requests’ default certificate verification enabled.

A successful response applies only to that request. FreeProxyHub removes entries after 15 minutes without a new check; a saved endpoint can still stop working sooner.

Switch to SOCKS5 with remote DNS

Install the optional support with python -m pip install "requests[socks]". Select an endpoint from the SOCKS5 list and change the proxy value to socks5h://IP:PORT. The h requests hostname resolution at the proxy; socks5:// resolves it locally.

Read the failure before retrying

A connection failure, a TLS error and a rejected destination response need different fixes. Start with the troubleshooting sequence, test a non-sensitive page and choose another recent endpoint if needed. FreeProxyHub does not provide authentication credentials or promise access to any destination.

Supplying proxies on the request also avoids relying solely on session settings that environment proxy variables may override.

Reference

The Requests proxy documentation covers proxy dictionaries and SOCKS support. See its timeout documentation for connection and read behavior.