python requests module proxy

Python Requests Module Proxy

If you are working with Python and need to interact with a web server, you might have heard about the Python Requests module. It allows you to send HTTP/1.1 requests extremely easily. But what if you need to use a proxy to make your requests? In this case, you can use the proxies parameter of the requests function to specify the proxy server you want to use.

Using a Proxy

To use a proxy with Python Requests, you simply need to pass in a dictionary with your proxy settings as the proxies parameter when you send your request. Here is an example:


import requests

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

response = requests.get("http://www.example.com", proxies=proxies)

print(response.text)
    

In this example, we have defined a dictionary with two keys: "http" and "https". Each key has a value that specifies the proxy server to use for that protocol. We then pass this dictionary as the proxies parameter to the requests.get function.

Authentication with a Proxy

If your proxy server requires authentication, you can include the username and password in the proxy URL. Here is an example:


import requests

proxies = {
  "http": "http://user:[email protected]:3128",
  "https": "http://user:[email protected]:1080",
}

response = requests.get("http://www.example.com", proxies=proxies)

print(response.text)
    

In this example, we have included the username and password in the proxy URL.

Using Environment Variables

Another way to specify a proxy is to set environment variables. Here is an example:


import requests
import os

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

response = requests.get("http://www.example.com")

print(response.text)
    

In this example, we have set the HTTP_PROXY and HTTPS_PROXY environment variables to our proxy URLs. We then send our request as normal, without specifying any proxies in our code.