From 2f33679cb39c144a9fef591e5140a766c92d66f5 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Mon, 20 Feb 2012 15:35:02 -0500 Subject: [PATCH 1/7] spacing --- requests/sessions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requests/sessions.py b/requests/sessions.py index 7aafd0ea..29ae1d9a 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -40,7 +40,7 @@ def merge_kwargs(local_kwarg, default_kwarg): kwargs.update(local_kwarg) # Remove keys that are set to None. - for (k,v) in list(local_kwarg.items()): + for (k, v) in list(local_kwarg.items()): if v is None: del kwargs[k] From 79bb9ee1417afe2231972c1331308500104e5dc7 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Mon, 20 Feb 2012 15:35:19 -0500 Subject: [PATCH 2/7] netrc parsing --- requests/utils.py | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/requests/utils.py b/requests/utils.py index 97f58600..c9f5ad74 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -11,15 +11,47 @@ that are also useful for external consumption. import cgi import codecs +import os import random import re import zlib +from netrc import netrc, NetrcParseError from .compat import parse_http_list as _parse_list_header -from .compat import quote, cookielib, SimpleCookie, is_py2 +from .compat import quote, cookielib, SimpleCookie, is_py2, urlparse from .compat import basestring, bytes +NETRC_FILES = ('.netrc', '_netrc') + + +def get_netrc_auth(url): + """Returns the Requests tuple auth for a given url from netrc.""" + + locations = (os.path.expanduser('~/{0}'.format(f)) for f in NETRC_FILES) + netrc_path = None + + for loc in locations: + if os.path.exists(loc) and not netrc_path: + netrc_path = loc + + # Abort early if there isn't one. + if netrc_path is None: + return netrc_path + + ri = urlparse(url) + + # Strip port numbers from netloc + host = ri.netloc.split(':')[0] + + try: + _netrc = netrc(netrc_path).authenticators(host) + # Return with login / password + return (_netrc[0 if _netrc[0] else 1], _netrc[2]) + except NetrcParseError: + pass + + def dict_from_string(s): """Returns a MultiDict with Cookies.""" From de0658f7e82982a398e8acef3470ed6be0e3c52e Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Mon, 20 Feb 2012 15:43:38 -0500 Subject: [PATCH 3/7] stick .netrc auth in pipeline --- requests/models.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/requests/models.py b/requests/models.py index 2c0b2e08..a238214a 100644 --- a/requests/models.py +++ b/requests/models.py @@ -27,7 +27,7 @@ from .exceptions import ( URLRequired, SSLError) from .utils import ( get_encoding_from_headers, stream_untransfer, guess_filename, requote_uri, - dict_from_string, stream_decode_response_unicode) + dict_from_string, stream_decode_response_unicode, get_netrc_auth) from .compat import ( urlparse, urlunparse, urljoin, urlsplit, urlencode, str, bytes, SimpleCookie, is_py2) @@ -435,6 +435,10 @@ class Request(object): if (content_type) and (not 'content-type' in self.headers): self.headers['Content-Type'] = content_type + # Use .netrc auth if none was provided. + if not self.auth: + self.auth = get_netrc_auth(url) + if self.auth: if isinstance(self.auth, tuple) and len(self.auth) == 2: # special-case basic HTTP auth From b0e18650735dc667f76f6aff108877f03b729c23 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Mon, 20 Feb 2012 15:43:45 -0500 Subject: [PATCH 4/7] netrc fix --- requests/utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/requests/utils.py b/requests/utils.py index c9f5ad74..dd93b056 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -46,8 +46,10 @@ def get_netrc_auth(url): try: _netrc = netrc(netrc_path).authenticators(host) - # Return with login / password - return (_netrc[0 if _netrc[0] else 1], _netrc[2]) + if _netrc: + # Return with login / password + login_i = (0 if _netrc[0] else 1) + return (_netrc[login_i], _netrc[2]) except NetrcParseError: pass From fd7610dd7a144879481b48e3b5412753b8499776 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Mon, 20 Feb 2012 16:07:36 -0500 Subject: [PATCH 5/7] python 3 unicode grr --- requests/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requests/utils.py b/requests/utils.py index dd93b056..68efa469 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -183,7 +183,7 @@ def header_expand(headers): headers = list(headers.items()) elif isinstance(headers, basestring): return headers - elif isinstance(headers, unicode): + elif isinstance(headers, str): # As discussed in https://github.com/kennethreitz/requests/issues/400 # latin-1 is the most conservative encoding used on the web. Anyone # who needs more can encode to a byte-string before calling From faae6b87b14851a75a62c3c4e82e0b571e3a8fa3 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Mon, 20 Feb 2012 16:12:08 -0500 Subject: [PATCH 6/7] v0.10.4 --- HISTORY.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/HISTORY.rst b/HISTORY.rst index 2091ac84..d6b8f496 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,6 +1,11 @@ History ------- +0.10.4 (2012-02-20) ++++++++++++++++++++ + +* Honor netrc. + 0.10.3 (2012-02-20) +++++++++++++++++++ From 8c873712fe89b9a4378cf069191a197d1eb6500a Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Mon, 20 Feb 2012 16:20:07 -0500 Subject: [PATCH 7/7] .netrc --- docs/index.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/index.rst b/docs/index.rst index df0f1937..232d6830 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -72,6 +72,7 @@ Requests is ready for today's web. - Unicode Response Bodies - Multipart File Uploads - Connection Timeouts +- ``.netrc`` support User Guide