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