Skip to content

Instantly share code, notes, and snippets.

@rollys
Forked from kennethreitz/0_urllib2.py
Created February 11, 2014 16:50
Show Gist options
  • Save rollys/8938879 to your computer and use it in GitHub Desktop.
Save rollys/8938879 to your computer and use it in GitHub Desktop.

Revisions

  1. 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)
  2. 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'