Skip to content

Instantly share code, notes, and snippets.

@parsa
Forked from kennethreitz/0_urllib2.py
Last active January 25, 2017 19:41
Show Gist options
  • Save parsa/dac1d8dbb9b842470a16ebf086ef496c to your computer and use it in GitHub Desktop.
Save parsa/dac1d8dbb9b842470a16ebf086ef496c to your computer and use it in GitHub Desktop.
urllib2 vs requests
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
from base64 import b64encode
request = urllib2.Request('https://api.github.com/user')
request.add_header('Authorization', 'Basic ' + b64encode('user' + ':' + 'pass'))
r = urllib2.urlopen(request)
print r.getcode()
print r.headers["content-type"]
print r.headers["X-RateLimit-Limit"]
# ------
# 200
# 'application/json'
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
r = requests.get('https://api.github.com', auth=('user', 'pass'))
print r.status_code
print r.headers['content-type']
# ------
# 200
# 'application/json'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment