python requests proxy example

Python Requests Proxy Example

Proxy is an intermediary server that sits between the client and the remote server. It helps to protect the identity of the client and also to reduce network congestion. Python Requests is a popular library that allows you to send HTTP/1.1 requests using Python.

Using a Proxy with Python Requests

To use a proxy with Python Requests, you need to pass the proxy details as a dictionary to the proxies parameter of the requests.get() method.

Here is an example:


import requests

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

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

print(response.content)
  • The above code creates a dictionary that specifies the proxy details.
  • The values of the dictionary represent the proxy servers for both HTTP and HTTPS protocols.
  • The requests.get() method is then used to make a request to a website, and the proxy details are passed as the value of the proxies parameter.
  • The response content of the request is then printed out.

Using Authentication with Proxy

If the proxy server requires authentication, the username and password can also be included in the proxy dictionary.

Here is an example:


import requests

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

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

print(response.content)
  • The above code includes the username and password in the proxy URL.
  • The requests.get() method is then used to make a request to a website, and the proxy details are passed as the value of the proxies parameter.
  • The response content of the request is then printed out.

Conclusion

In this blog post, we have seen how to use a proxy with Python Requests. We have also seen how to include authentication details in the proxy URL. When using a proxy, make sure to choose a reliable and trustworthy one to ensure the security of your network traffic.