• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
10
11.. versionadded:: 1.5.2
12
13**Source code:** :source:`Lib/netrc.py`
14
15--------------
16
17The :class:`~netrc.netrc` class parses and encapsulates the netrc file format used by
18the Unix :program:`ftp` program and other FTP clients.
19
20
21.. class:: netrc([file])
22
23   A :class:`~netrc.netrc` instance or subclass instance encapsulates data from  a netrc
24   file.  The initialization argument, if present, specifies the file to parse.  If
25   no argument is given, the file :file:`.netrc` in the user's home directory will
26   be read.  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:: 2.7.6 Added the POSIX permissions check.
36
37
38.. exception:: NetrcParseError
39
40   Exception raised by the :class:`~netrc.netrc` class when syntactical errors are
41   encountered in source text.  Instances of this exception provide three
42   interesting attributes:  :attr:`msg` is a textual explanation of the error,
43   :attr:`filename` is the name of the source file, and :attr:`lineno` gives the
44   line number on which the error was found.
45
46
47.. _netrc-objects:
48
49netrc Objects
50-------------
51
52A :class:`~netrc.netrc` instance has the following methods:
53
54
55.. method:: netrc.authenticators(host)
56
57   Return a 3-tuple ``(login, account, password)`` of authenticators for *host*.
58   If the netrc file did not contain an entry for the given host, return the tuple
59   associated with the 'default' entry.  If neither matching host nor default entry
60   is available, return ``None``.
61
62
63.. method:: netrc.__repr__()
64
65   Dump the class data as a string in the format of a netrc file. (This discards
66   comments and may reorder the entries.)
67
68Instances of :class:`~netrc.netrc` have public instance variables:
69
70
71.. attribute:: netrc.hosts
72
73   Dictionary mapping host names to ``(login, account, password)`` tuples.  The
74   'default' entry, if any, is represented as a pseudo-host by that name.
75
76
77.. attribute:: netrc.macros
78
79   Dictionary mapping macro names to string lists.
80
81.. note::
82
83   Passwords are limited to a subset of the ASCII character set. Versions of
84   this module prior to 2.3 were extremely limited.  Starting with 2.3, all
85   ASCII punctuation is allowed in passwords.  However, note that whitespace and
86   non-printable characters are not allowed in passwords.  This is a limitation
87   of the way the .netrc file is parsed and may be removed in the future.
88
89