Merge branch 'netrc' into develop
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 6f57352, 8c87371 · Browse files · 5 files·+48 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+48 added−4 removed
- other:+48 added−4 removed
Provenance
- Commit
793d314- Tree
7035228- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
6f57352- Parent 2
8c87371
5 files changed+48 added−4 removed
modified:docs/index.rst+1 added
70 unmodified lines71 - Automatic Decompression72 - Unicode Response Bodies73 - Multipart File Uploads74 - Connection Timeouts75+- ``.netrc`` support76 77 78 User Guide79 ----------52 unmodified linesmodified:requests/models.py+5 added-1 removed
25 unmodified lines26 ConnectionError, HTTPError, RequestException, Timeout, TooManyRedirects,27 URLRequired, SSLError)28 from .utils import (29 get_encoding_from_headers, stream_untransfer, guess_filename, requote_uri,30- dict_from_string, stream_decode_response_unicode)30+ dict_from_string, stream_decode_response_unicode, get_netrc_auth)31 from .compat import (32 urlparse, urlunparse, urljoin, urlsplit, urlencode, str, bytes,33 SimpleCookie, is_py2)34 399 unmodified lines434 # Add content-type if it wasn't explicitly provided.435 if (content_type) and (not 'content-type' in self.headers):436 self.headers['Content-Type'] = content_type437 438+ # Use .netrc auth if none was provided.439+ if not self.auth:440+ self.auth = get_netrc_auth(url)441+442 if self.auth:443 if isinstance(self.auth, tuple) and len(self.auth) == 2:444 # special-case basic HTTP auth445 self.auth = HTTPBasicAuth(*self.auth)348 unmodified linesmodified:requests/sessions.py+1 added-1 removed
38 unmodified lines39 kwargs = default_kwarg.copy()40 kwargs.update(local_kwarg)41 42 # Remove keys that are set to None.43- for (k,v) in list(local_kwarg.items()):43+ for (k, v) in list(local_kwarg.items()):44 if v is None:45 del kwargs[k]46 47 return kwargs250 unmodified linesmodified:requests/utils.py+36 added-2 removed
9 unmodified lines10 """11 12 import cgi13 import codecs14+import os15 import random16 import re17 import zlib18+from netrc import netrc, NetrcParseError19 20 from .compat import parse_http_list as _parse_list_header19-from .compat import quote, cookielib, SimpleCookie, is_py221+from .compat import quote, cookielib, SimpleCookie, is_py2, urlparse22 from .compat import basestring, bytes23 24 25+NETRC_FILES = ('.netrc', '_netrc')26+27+28+def get_netrc_auth(url):29+ """Returns the Requests tuple auth for a given url from netrc."""30+31+ locations = (os.path.expanduser('~/{0}'.format(f)) for f in NETRC_FILES)32+ netrc_path = None33+34+ for loc in locations:35+ if os.path.exists(loc) and not netrc_path:36+ netrc_path = loc37+38+ # Abort early if there isn't one.39+ if netrc_path is None:40+ return netrc_path41+42+ ri = urlparse(url)43+44+ # Strip port numbers from netloc45+ host = ri.netloc.split(':')[0]46+47+ try:48+ _netrc = netrc(netrc_path).authenticators(host)49+ if _netrc:50+ # Return with login / password51+ login_i = (0 if _netrc[0] else 1)52+ return (_netrc[login_i], _netrc[2])53+ except NetrcParseError:54+ pass55+56+57 def dict_from_string(s):58 """Returns a MultiDict with Cookies."""59 60 cookies = dict()121 unmodified lines182 if isinstance(headers, dict):183 headers = list(headers.items())184 elif isinstance(headers, basestring):185 return headers152- elif isinstance(headers, unicode):186+ elif isinstance(headers, str):187 # As discussed in https://github.com/kennethreitz/requests/issues/400188 # latin-1 is the most conservative encoding used on the web. Anyone189 # who needs more can encode to a byte-string before calling190 return headers.encode("latin-1")266 unmodified linesmodified:HISTORY.rst+5 added
1 History2 -------3 4+0.10.4 (2012-02-20)5++++++++++++++++++++6+7+* Honor netrc.8+9 0.10.3 (2012-02-20)10 +++++++++++++++++++11 12 * HEAD requests don't follow redirects anymore.378 unmodified lines