python requests proxy

Python Requests Proxy

Python Requests is a popular library used for making HTTP requests in Python. It provides a simple and elegant way to make HTTP requests from Python. However, there may be cases where you need to use a proxy to make requests. This may be because the website you are trying to access is blocked in your country, or because you want to hide your identity while accessing a website.

Using a proxy with Python Requests

Python Requests makes it easy to use a proxy for making HTTP requests. You can simply pass the proxy information as a dictionary to the proxies parameter of the requests.get() or requests.post() method.


import requests

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

response = requests.get(url, proxies=proxies)

In the above code, we are passing a dictionary with the proxy information to the proxies parameter of the requests.get() method. The dictionary contains two key-value pairs:

  • 'http': 'http://10.10.1.10:3128' - This specifies the proxy server for HTTP requests.
  • 'https': 'https://10.10.1.10:1080' - This specifies the proxy server for HTTPS requests.

You can replace the proxy server information with your own proxy server information.

Multiple proxies with Python Requests

You can also use multiple proxies with Python Requests. You can pass a list of dictionaries to the proxies parameter of the requests.get() or requests.post() method.


import requests

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

response = requests.get(url, proxies=proxies)

In the above code, we are passing a list of dictionaries with the proxy information to the proxies parameter of the requests.get() method. The list contains two dictionaries, each with two key-value pairs for the proxy server information.

You can replace the proxy server information with your own proxy server information.

Conclusion

In this article, we learned how to use a proxy with Python Requests for making HTTP requests. We also learned how to use multiple proxies with Python Requests.

Using a proxy can be helpful in certain situations, such as when you need to access a website that is blocked in your country or when you want to hide your identity while accessing a website.