Created
October 27, 2015 20:54
-
-
Save lukasgraf/16c6db9dd9aaea1c2456 to your computer and use it in GitHub Desktop.
Using requests sessions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
urls = ['https://en.wikipedia.org/wiki/%s' % n for n in range(100)] | |
# Code that creates a new connection for every request | |
for url in urls: | |
r = requests.get(url) | |
data = r.text | |
print len(data) | |
# The same code using sessions | |
with requests.Session() as session: | |
for url in urls: | |
r = session.get(url) | |
data = r.text | |
print len(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment