Skip to content

Instantly share code, notes, and snippets.

@lukasgraf
Created October 27, 2015 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukasgraf/16c6db9dd9aaea1c2456 to your computer and use it in GitHub Desktop.
Save lukasgraf/16c6db9dd9aaea1c2456 to your computer and use it in GitHub Desktop.
Using requests sessions
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