How To Use A Proxy With Python Requests?

Proxies are vital for survival on the internet as they protect you from cyber-attacks. In addition, they help you remain anonymous while protecting your sensitive data. Therefore they are crucial for all online engagements, especially those handling extensive online work such as programming, data science, banking, machine learning, web development, and automation. Such tasks require sensitive details and could land your business or career in trouble if it gets into the hands of hackers. Therefore, proxies are essential even when dealing with python requests.


What is a Proxy?

Proxy is an essential third-party server that keeps you anonymous from the websites you visit by masking your actual IP address. Therefore, it takes your request, delivers them to the websites, and brings the responses using its IP address. This means the websites can’t recognize your device’s IP address.


Python Requests and Proxies

Python requests allow you to make HTTP requests in python. They help you make requests such as GET, POST, or DELETE. When you want to remain anonymous or protect yourself, you can make these requests behind a proxy. Here are the steps to using proxies with python requests.


  1. Import the Requests Package

Importing the requests library makes the process simple when sending the HTTP requests. Although it’s not in the python distribution, it’s crucial and functional since it makes the HTTP working code brief and straightforward. Here’s how to import the requests package.

import requests

proxies = {




response = requests.get(“http://myexample.com”, proxies=proxies)

  1. Create a Proxy Dictionary

Once you import the requests package, create a proxy dictionary to help define the HTTP, HTTPS, and FTP connections. Additionary, the dictionary helps in mapping each protocol to the proxy URL. You should also set a URL variable for the webpage you’re scrapping.


The example above bears three steps including:

  • Importing the requests library
  • Defining a dictionary
  • Making a get request

Authenticating Proxies in Python


After defining the proxy and ports, you need to specify the protocol. But you can work with the same proxy for various protocols. Let’s look at an example of using basic HTTP authentication.

import requests

proxies = {

“http”: “”,


“https”: “”,


auth = (‘username’, ‘password’)

response = requests.get(“http://myexample.com”, proxies=proxies)

Note: As shown in this example, you pass the authentication details in the proxy portal using the auth parameter.


Environmental Variables

Setting environment variables allows you to define the proxies you use with each request. But this applies when you’re not using the same proxies. This means you only make requests without defining any proxies in your code.


Proxies are essential for your online security. If you want to scrape information from websites, you need anonymity; only a proxy can offer that. Using proxies on python isn’t challenging. First, you need to import the requests package. Then you need to create a proxy dictionary to define connections such as HTTPS and HTTP. Finally, with the authentication, you’ll need to specify the protocol.