Skip to content
Myr

netrc parsing

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 · parent 2f33679 · Browse files · 1 file+33 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+33 added−1 removed

  • other:+33 added−1 removed

1 file+33 added−1 removed

modified:requests/utils.py+33 added-1 removed
9 unmodified lines
10"""1112import cgi13import codecs14import os15import random16import re17import zlib18from netrc import netrc, NetrcParseError1920from .compat import parse_http_list as _parse_list_header19from .compat import quote, cookielib, SimpleCookie, is_py221from .compat import quote, cookielib, SimpleCookie, is_py2, urlparse22from .compat import basestring, bytes232425NETRC_FILES = ('.netrc', '_netrc')262728def get_netrc_auth(url):29 """Returns the Requests tuple auth for a given url from netrc."""3031 locations = (os.path.expanduser('~/{0}'.format(f)) for f in NETRC_FILES)32 netrc_path = None3334 for loc in locations:35 if os.path.exists(loc) and not netrc_path:36 netrc_path = loc3738 # Abort early if there isn't one.39 if netrc_path is None:40 return netrc_path4142 ri = urlparse(url)4344 # Strip port numbers from netloc45 host = ri.netloc.split(':')[0]4647 try:48 _netrc = netrc(netrc_path).authenticators(host)49 # Return with login / password50 return (_netrc[0 if _netrc[0] else 1], _netrc[2])51 except NetrcParseError:52 pass535455def dict_from_string(s):56 """Returns a MultiDict with Cookies."""5758 cookies = dict()
396 unmodified lines