How to configure Python requests to use a proxy?

by scrapecrow Dec 19, 2022

Python's requests package supports both HTTP and SOCKS5 proxies which can be set for each request or the whole script:

import requests

# proxy pattern is:
# scheme://username:password@IP:PORT
# For example:
# no auth HTTP proxy:
my_proxy = "http://160.11.12.13:1020"
# or socks5
my_proxy = "socks://160.11.12.13:1020"
# proxy with authentication
my_proxy = "http://my_username:my_password@160.11.12.13:1020"
# note: that username and password should be url quoted if they contain URL sensitive characters like "@":
from urllib.parse import quote
my_proxy = f"http://{quote('foo@bar.com')}:{quote('password@123')}@160.11.12.13:1020"


proxies = {
    # this proxy will be applied to all http:// urls
    'http': 'http://160.11.12.13:1020',
    # this proxy will be applied to all https:// urls (not the S)
    'https': 'http://160.11.12.13:1020',
    # we can also use proxy only for specific pages
    'https://httpbin.dev': 'http://160.11.12.13:1020',
}
requests.get("https://httpbin.dev/ip", proxies=proxies)

Note that proxy can also be set through the standard *_PROXY environment variables:

$ export HTTP_PROXY="http://160.11.12.13:1020"
$ export HTTPS_PROXY="http://160.11.12.13:1020"
$ export ALL_PROXY="socks://160.11.12.13:1020"
$ python
import requests
# this will use the proxies we set
requests.get("https://httpbin.dev/ip")

Finally, when web scraping using proxies we should rotate proxies for each request. See our how to rotate proxies guide for more. For more on proxies see introduction to proxies in web scraping

Related Articles

Guide to Python requests POST method

Discover how to use Python's requests library for POST requests, including JSON, form data, and file uploads, along with response handling tips.

PYTHON
REQUESTS
HTTP
Guide to Python requests POST method

Guide to Python Requests Headers

Our guide to request headers for Python requests library. How to configure and what do they mean.

PYTHON
REQUESTS
HTTP
Guide to Python Requests Headers

How to Scrape YouTube in 2025

Learn how to scrape YouTube, channel, video, and comment data using Python directly in JSON.

SCRAPEGUIDE
PYTHON
HIDDEN-API
How to Scrape YouTube in 2025

Bypass Proxy Detection with Browser Fingerprint Impersonation

Stop proxy blocks with browser fingerprint impersonation using this guide for Playwright, Selenium, curl-impersonate & Scrapfly

PROXIES
SELENIUM
PLAYWRIGHT
PUPPETEER
BLOCKING
Bypass Proxy Detection with Browser Fingerprint Impersonation

How Caching Can Cut Your Proxy Bill by 70%

Learn how intelligent caching strategies can reduce proxy costs by 40-70%. Complete guide to bandwidth optimization and proxy management.

PROXIES
How Caching Can Cut Your Proxy Bill by 70%

How to Optimize NetNut Proxies

Learn how to set up and optimize NetNut proxies for web scraping, including bandwidth reduction techniques and integration with Scrapfly Proxy Saver.

PROXIES
How to Optimize NetNut Proxies