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.

Revisions

  1. parsa revised this gist Jan 25, 2017. 1 changed file with 7 additions and 15 deletions.
    22 changes: 7 additions & 15 deletions 0_urllib2.py
    Original file line number Diff line number Diff line change
    @@ -2,23 +2,15 @@
    # -*- coding: utf-8 -*-

    import urllib2
    from base64 import b64encode

    gh_url = 'https://api.github.com'
    request = urllib2.Request('https://api.github.com/user')
    request.add_header('Authorization', 'Basic ' + b64encode('user' + ':' + 'pass'))
    r = urllib2.urlopen(request)

    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')
    print r.getcode()
    print r.headers["content-type"]
    print r.headers["X-RateLimit-Limit"]

    # ------
    # 200
  2. Kenneth Reitz revised this gist Aug 14, 2011. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions 0_urllib2.py
    Original file line number Diff line number Diff line change
    @@ -4,13 +4,11 @@
    import urllib2

    gh_url = 'https://api.github.com'
    gh_user= 'user'
    gh_pass = 'pass'

    req = urllib2.Request(gh_url)

    password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
    password_manager.add_password(None, gh_url, gh_user, gh_pass)
    password_manager.add_password(None, gh_url, 'user', 'pass')

    auth_manager = urllib2.HTTPBasicAuthHandler(password_manager)
    opener = urllib2.build_opener(auth_manager)
  3. Kenneth Reitz created this gist May 16, 2011.
    27 changes: 27 additions & 0 deletions 0_urllib2.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    import urllib2

    gh_url = 'https://api.github.com'
    gh_user= 'user'
    gh_pass = 'pass'

    req = urllib2.Request(gh_url)

    password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
    password_manager.add_password(None, gh_url, gh_user, gh_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'
    13 changes: 13 additions & 0 deletions 1_requests.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    #!/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'