Created
May 16, 2011 00:17
-
Star
(296)
You must be signed in to star a gist -
Fork
(75)
You must be signed in to fork a gist
-
-
Save kennethreitz/973705 to your computer and use it in GitHub Desktop.
urllib2 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 python | |
# -*- coding: utf-8 -*- | |
import urllib2 | |
gh_url = 'https://api.github.com' | |
req = urllib2.Request(gh_url) | |
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm() | |
password_manager.add_password(None, gh_url, 'user', 'pass') | |
auth_manager = urllib2.HTTPBasicAuthHandler(password_manager) | |
opener = urllib2.build_opener(auth_manager) | |
urllib2.install_opener(opener) | |
handler = urllib2.urlopen(req) | |
print handler.getcode() | |
print handler.headers.getheader('content-type') | |
# ------ | |
# 200 | |
# 'application/json' |
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 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' |
@source-creator wrote...
Unfortunately, code to format an url with requests is longer and more bloated than urllib:
# requests from requests import Request url = Request(None, 'http://example.com/?', params={'Data1': 'data'}).prepare().url # urllib import urllib url = 'http://example.com/?' + urllib.parse.urlencode({'Data1': 'data'})
Just read this: https://stackoverflow.com/a/46783596
You can just use requests.utils
(and other submodules, I've seen annother one for functions included in urllib.parse but I don't remember it) instead of urllib.parse in most cases I think.
(and it does not requires requests.utils to be imported because importing requests also imports requests.utils)
NOTE: The functions are comming right from the URLLIB package, so the actual documentation is in URLLIB for some (not all) of the function of this submodule of requests.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ HGStyle wrote…
Yes, that's the point of @kennethreitz's examples. Operations that are complicated with
urllib
are simple withrequests
.