-
-
Save dieterplex/2497103 to your computer and use it in GitHub Desktop.
urllib2 vs urllib3 vs requests
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
#!/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