python requests use proxy

How to Use Proxy with Python Requests?

If you are working with Python requests to access certain websites, sometimes you may need to use a proxy to hide your IP or bypass certain restrictions. In this article, we will discuss how to use proxy with Python requests.

Method 1: Using a Proxy Dictionary

The first method is to pass a dictionary of proxies as the proxies parameter in the requests.get() function. The dictionary should contain the protocol (http or https) as the key and the proxy address as the value. Here's an example:


import requests

proxies = {
  'http': 'http://10.10.1.10:3128',
  'https': 'https://10.10.1.10:1080',
}

response = requests.get('https://www.example.com', proxies=proxies)

print(response.content)
        

In the above example, we create a dictionary of proxy addresses for http and https protocols and pass it as the proxies parameter in the requests.get() function. The response from the website will go through the specified proxy.

Method 2: Using Environment Variables

The second method is to set the HTTP_PROXY and HTTPS_PROXY environment variables with the proxy address. Here's an example:


import os
import requests

os.environ['HTTP_PROXY'] = 'http://10.10.1.10:3128'
os.environ['HTTPS_PROXY'] = 'https://10.10.1.10:1080'

response = requests.get('https://www.example.com')

print(response.content)
        

In the above example, we set the HTTP_PROXY and HTTPS_PROXY environment variables to the proxy addresses and then make the requests.get() call. The requests library automatically looks for these environment variables and uses them as the proxy.

Method 3: Using Session Objects

The third method is to use Session objects in requests library. Here's an example:


import requests

session = requests.Session()
session.proxies = {
  'http': 'http://10.10.1.10:3128',
  'https': 'https://10.10.1.10:1080',
}

response = session.get('https://www.example.com')

print(response.content)
        

In the above example, we create a session object and set its proxies property with the proxy addresses. Then we use the session object to make the GET call. This way, all subsequent calls made with the session object will use the same proxy.