Guides
How to send requests through a proxy in Python (requests)
Requests through an HTTP proxy and SOCKS5 with a login and password in the requests library.
What you need
The proxy is issued in the format host:port:login:password. In the example, replace HOST, PORT, LOGIN and PASSWORD with your own values. The requests library accepts a proxy only as an address, so the login and password are part of the address and must be percent-encoded.
pip install requestsExample
from urllib.parse import quote
import requests
HOST = "HOST"
PORT = "PORT"
LOGIN = "LOGIN"
PASSWORD = "PASSWORD"
# Special characters in the login and password (@, :, /, # and others) must be encoded,
# otherwise the proxy address is parsed incorrectly
auth = quote(LOGIN, safe="") + ":" + quote(PASSWORD, safe="")
proxy = f"http://{auth}@{HOST}:{PORT}"
# The key is the site's scheme, the value is the proxy address.
# The proxy's own scheme stays http:// even for HTTPS sites
proxies = {"http": proxy, "https": proxy}
# timeout is required: without it a request can wait forever
response = requests.get("https://api.ipify.org", proxies=proxies, timeout=30)
response.raise_for_status()
# The service returns the address the request came from
print(response.text)If SOCKS5
SOCKS5 needs an extra package. The socks5h:// scheme makes site names resolve on the proxy side, while socks5:// does it on your computer.
pip install "requests[socks]"from urllib.parse import quote
import requests
auth = quote("LOGIN", safe="") + ":" + quote("PASSWORD", safe="")
proxy = f"socks5h://{auth}@HOST:SOCKS_PORT"
response = requests.get(
"https://api.ipify.org",
proxies={"http": proxy, "https": proxy},
timeout=30,
)
print(response.text)Common errors
- ProxyError with the text 407 Proxy Authentication Required: the login or password is wrong. Check that you did not mix up the order in the issued host:port:login:password.
- ConnectTimeout or ReadTimeout: check HOST and PORT, whether you put the SOCKS5 port into HTTP mode, and increase timeout.
- Special characters in the password: without quote(..., safe='') a password with @, : or / breaks the address, and you get 407 or an address parsing error.
- The https:// scheme in the proxy address: the issued proxy works over HTTP, so write http://... even in the proxies['https'] key.
- If the HTTP_PROXY or HTTPS_PROXY variables are set, requests reads them too; an explicit proxies argument takes priority for the request.
- The error Missing dependencies for SOCKS support: the requests[socks] package is not installed.
Sources
Checked against the official documentation: October 2026. Program interfaces change: if item names differ, follow the documentation.
All guides are collected on the Guides page.
Find a proxy in the chat