How to use proxies with an HTTP session using the Python requests package
December 26, 2017 Comments
I was working on a client project yesterday where I needed to use a proxy to make HTTP requests with the Python requests package.
In my use case, I needed to create a session and I wanted to just specify the proxy settings in one place and use that for all requests made through that session. I didn't see how to do this in the documentation so I thought I'd share my solution here.
Here's a quick code sample:
import requests proxies = { 'http': 'http://user:pass@10.10.1.0:3128', 'https': 'http://user:pass@10.10.1.0:3128', } # Create the session and set the proxies. s = requests.Session() s.proxies = proxies # Make the HTTP request through the session. r = s.get('http://www.showmemyip.com/') # Check if the proxy was indeed used (the text should contain the proxy IP). print(r.text)
As you can see, the code is very straightforward. Love this package. :)
Tags: howto, python, tech, software development