Skip to content

Instantly share code, notes, and snippets.

@dieterplex
Forked from andrewwatts/request_time.py
Created April 26, 2012 07:19
Show Gist options
  • Save dieterplex/2497103 to your computer and use it in GitHub Desktop.
Save dieterplex/2497103 to your computer and use it in GitHub Desktop.
urllib2 vs urllib3 vs requests
#!/usr/bin/env python2.7
import urllib2
import time
_ITERATIONS = 200
start_time = time.time()
for i in range(0, _ITERATIONS):
try:
response = urllib2.urlopen('http://localhost/tmp/derp.html')
except urllib2.HTTPError, e:
response = e
response.code
print 'time with urllib2: {0}'.format(time.time()-start_time)
import urllib3
start_time = time.time()
http = urllib3.PoolManager()
for i in range(0, _ITERATIONS):
response = http.request('GET', 'http://localhost/tmp/derp.html')
response.status
print 'time with urllib3: {0}'.format(time.time()-start_time)
import requests
start_time = time.time()
for i in range(0, _ITERATIONS):
response = requests.get('http://localhost/tmp/derp.html')
response.status_code
print 'time with requests: {0}'.format(time.time()-start_time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment