2024-05-08 16:34:40 -03:00
|
|
|
:mod:`!netrc` --- netrc file processing
|
|
|
|
=======================================
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. module:: netrc
|
|
|
|
:synopsis: Loading of .netrc files.
|
2016-06-11 16:02:54 -03:00
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. moduleauthor:: Eric S. Raymond <esr@snark.thyrsus.com>
|
|
|
|
.. sectionauthor:: Eric S. Raymond <esr@snark.thyrsus.com>
|
|
|
|
|
2011-01-27 16:38:46 -04:00
|
|
|
**Source code:** :source:`Lib/netrc.py`
|
|
|
|
|
|
|
|
--------------
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2016-12-02 17:13:53 -04:00
|
|
|
The :class:`~netrc.netrc` class parses and encapsulates the netrc file format used by
|
2007-08-15 11:28:22 -03:00
|
|
|
the Unix :program:`ftp` program and other FTP clients.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: netrc([file])
|
|
|
|
|
2016-12-02 17:13:53 -04:00
|
|
|
A :class:`~netrc.netrc` instance or subclass instance encapsulates data from a netrc
|
2007-08-15 11:28:22 -03:00
|
|
|
file. The initialization argument, if present, specifies the file to parse. If
|
2017-11-25 06:37:22 -04:00
|
|
|
no argument is given, the file :file:`.netrc` in the user's home directory --
|
|
|
|
as determined by :func:`os.path.expanduser` -- will be read. Otherwise,
|
|
|
|
a :exc:`FileNotFoundError` exception will be raised.
|
|
|
|
Parse errors will raise :exc:`NetrcParseError` with diagnostic
|
2007-08-15 11:28:22 -03:00
|
|
|
information including the file name, line number, and terminating token.
|
2013-09-17 21:30:02 -03:00
|
|
|
If no argument is specified on a POSIX system, the presence of passwords in
|
|
|
|
the :file:`.netrc` file will raise a :exc:`NetrcParseError` if the file
|
|
|
|
ownership or permissions are insecure (owned by a user other than the user
|
|
|
|
running the process, or accessible for read or write by any other user).
|
|
|
|
This implements security behavior equivalent to that of ftp and other
|
|
|
|
programs that use :file:`.netrc`.
|
|
|
|
|
2013-09-17 22:28:17 -03:00
|
|
|
.. versionchanged:: 3.4 Added the POSIX permission check.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2017-11-25 06:37:22 -04:00
|
|
|
.. versionchanged:: 3.7
|
|
|
|
:func:`os.path.expanduser` is used to find the location of the
|
|
|
|
:file:`.netrc` file when *file* is not passed as argument.
|
|
|
|
|
2021-05-02 02:01:02 -03:00
|
|
|
.. versionchanged:: 3.10
|
|
|
|
:class:`netrc` try UTF-8 encoding before using locale specific
|
|
|
|
encoding.
|
2021-11-17 05:07:54 -04:00
|
|
|
The entry in the netrc file no longer needs to contain all tokens. The missing
|
|
|
|
tokens' value default to an empty string. All the tokens and their values now
|
|
|
|
can contain arbitrary characters, like whitespace and non-ASCII characters.
|
|
|
|
If the login name is anonymous, it won't trigger the security check.
|
2021-05-02 02:01:02 -03:00
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. exception:: NetrcParseError
|
|
|
|
|
2016-12-02 17:13:53 -04:00
|
|
|
Exception raised by the :class:`~netrc.netrc` class when syntactical errors are
|
2007-08-15 11:28:22 -03:00
|
|
|
encountered in source text. Instances of this exception provide three
|
2023-07-29 02:48:10 -03:00
|
|
|
interesting attributes:
|
|
|
|
|
|
|
|
.. attribute:: msg
|
|
|
|
|
|
|
|
Textual explanation of the error.
|
|
|
|
|
|
|
|
.. attribute:: filename
|
|
|
|
|
|
|
|
The name of the source file.
|
|
|
|
|
|
|
|
.. attribute:: lineno
|
|
|
|
|
|
|
|
The line number on which the error was found.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. _netrc-objects:
|
|
|
|
|
|
|
|
netrc Objects
|
|
|
|
-------------
|
|
|
|
|
2016-12-02 17:13:53 -04:00
|
|
|
A :class:`~netrc.netrc` instance has the following methods:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. method:: netrc.authenticators(host)
|
|
|
|
|
|
|
|
Return a 3-tuple ``(login, account, password)`` of authenticators for *host*.
|
|
|
|
If the netrc file did not contain an entry for the given host, return the tuple
|
|
|
|
associated with the 'default' entry. If neither matching host nor default entry
|
|
|
|
is available, return ``None``.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: netrc.__repr__()
|
|
|
|
|
|
|
|
Dump the class data as a string in the format of a netrc file. (This discards
|
|
|
|
comments and may reorder the entries.)
|
|
|
|
|
2016-12-02 17:13:53 -04:00
|
|
|
Instances of :class:`~netrc.netrc` have public instance variables:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. attribute:: netrc.hosts
|
|
|
|
|
|
|
|
Dictionary mapping host names to ``(login, account, password)`` tuples. The
|
|
|
|
'default' entry, if any, is represented as a pseudo-host by that name.
|
|
|
|
|
|
|
|
|
|
|
|
.. attribute:: netrc.macros
|
|
|
|
|
|
|
|
Dictionary mapping macro names to string lists.
|