1 2:mod:`netrc` --- netrc file processing 3====================================== 4 5.. module:: netrc 6 :synopsis: Loading of .netrc files. 7 8.. moduleauthor:: Eric S. Raymond <esr@snark.thyrsus.com> 9.. sectionauthor:: Eric S. Raymond <esr@snark.thyrsus.com> 10 11**Source code:** :source:`Lib/netrc.py` 12 13-------------- 14 15The :class:`~netrc.netrc` class parses and encapsulates the netrc file format used by 16the Unix :program:`ftp` program and other FTP clients. 17 18 19.. class:: netrc([file]) 20 21 A :class:`~netrc.netrc` instance or subclass instance encapsulates data from a netrc 22 file. The initialization argument, if present, specifies the file to parse. If 23 no argument is given, the file :file:`.netrc` in the user's home directory -- 24 as determined by :func:`os.path.expanduser` -- will be read. Otherwise, 25 a :exc:`FileNotFoundError` exception will be raised. 26 Parse errors will raise :exc:`NetrcParseError` with diagnostic 27 information including the file name, line number, and terminating token. 28 If no argument is specified on a POSIX system, the presence of passwords in 29 the :file:`.netrc` file will raise a :exc:`NetrcParseError` if the file 30 ownership or permissions are insecure (owned by a user other than the user 31 running the process, or accessible for read or write by any other user). 32 This implements security behavior equivalent to that of ftp and other 33 programs that use :file:`.netrc`. 34 35 .. versionchanged:: 3.4 Added the POSIX permission check. 36 37 .. versionchanged:: 3.7 38 :func:`os.path.expanduser` is used to find the location of the 39 :file:`.netrc` file when *file* is not passed as argument. 40 41 .. versionchanged:: 3.10 42 :class:`netrc` try UTF-8 encoding before using locale specific 43 encoding. 44 45 46.. exception:: NetrcParseError 47 48 Exception raised by the :class:`~netrc.netrc` class when syntactical errors are 49 encountered in source text. Instances of this exception provide three 50 interesting attributes: :attr:`msg` is a textual explanation of the error, 51 :attr:`filename` is the name of the source file, and :attr:`lineno` gives the 52 line number on which the error was found. 53 54 55.. _netrc-objects: 56 57netrc Objects 58------------- 59 60A :class:`~netrc.netrc` instance has the following methods: 61 62 63.. method:: netrc.authenticators(host) 64 65 Return a 3-tuple ``(login, account, password)`` of authenticators for *host*. 66 If the netrc file did not contain an entry for the given host, return the tuple 67 associated with the 'default' entry. If neither matching host nor default entry 68 is available, return ``None``. 69 70 71.. method:: netrc.__repr__() 72 73 Dump the class data as a string in the format of a netrc file. (This discards 74 comments and may reorder the entries.) 75 76Instances of :class:`~netrc.netrc` have public instance variables: 77 78 79.. attribute:: netrc.hosts 80 81 Dictionary mapping host names to ``(login, account, password)`` tuples. The 82 'default' entry, if any, is represented as a pseudo-host by that name. 83 84 85.. attribute:: netrc.macros 86 87 Dictionary mapping macro names to string lists. 88 89.. note:: 90 91 Passwords are limited to a subset of the ASCII character set. All ASCII 92 punctuation is allowed in passwords, however, note that whitespace and 93 non-printable characters are not allowed in passwords. This is a limitation 94 of the way the .netrc file is parsed and may be removed in the future. 95