blob: 1b53efc542a006bd6832832be27c51d962611e44 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
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
Georg Brandl8ec7f652007-08-15 14:28:01 +000011.. versionadded:: 1.5.2
12
13The :class:`netrc` class parses and encapsulates the netrc file format used by
14the Unix :program:`ftp` program and other FTP clients.
15
16
17.. class:: netrc([file])
18
19 A :class:`netrc` instance or subclass instance encapsulates data from a netrc
20 file. The initialization argument, if present, specifies the file to parse. If
21 no argument is given, the file :file:`.netrc` in the user's home directory will
22 be read. Parse errors will raise :exc:`NetrcParseError` with diagnostic
23 information including the file name, line number, and terminating token.
R David Murray4189b672013-09-16 13:48:44 -040024 If no argument is specified on a POSIX system, the presence of passwords in
25 the :file:`.netrc` file will raise a :exc:`NetrcParseError` if the file
26 ownership or permissions are insecure (owned by a user other than the user
27 running the process, or accessible for read or write by any other user).
28 This implements security behavior equivalent to that of ftp and other
29 programs that use :file:`.netrc`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000030
R David Murray137b5722013-09-17 20:08:09 -040031 .. versionchanged:: 2.6.9 Added the POSIX permissions check.
32
Georg Brandl8ec7f652007-08-15 14:28:01 +000033
34.. exception:: NetrcParseError
35
36 Exception raised by the :class:`netrc` class when syntactical errors are
37 encountered in source text. Instances of this exception provide three
38 interesting attributes: :attr:`msg` is a textual explanation of the error,
39 :attr:`filename` is the name of the source file, and :attr:`lineno` gives the
40 line number on which the error was found.
41
42
43.. _netrc-objects:
44
45netrc Objects
46-------------
47
48A :class:`netrc` instance has the following methods:
49
50
51.. method:: netrc.authenticators(host)
52
53 Return a 3-tuple ``(login, account, password)`` of authenticators for *host*.
54 If the netrc file did not contain an entry for the given host, return the tuple
55 associated with the 'default' entry. If neither matching host nor default entry
56 is available, return ``None``.
57
58
59.. method:: netrc.__repr__()
60
61 Dump the class data as a string in the format of a netrc file. (This discards
62 comments and may reorder the entries.)
63
64Instances of :class:`netrc` have public instance variables:
65
66
67.. attribute:: netrc.hosts
68
69 Dictionary mapping host names to ``(login, account, password)`` tuples. The
70 'default' entry, if any, is represented as a pseudo-host by that name.
71
72
73.. attribute:: netrc.macros
74
75 Dictionary mapping macro names to string lists.
76
77.. note::
78
79 Passwords are limited to a subset of the ASCII character set. Versions of
80 this module prior to 2.3 were extremely limited. Starting with 2.3, all
81 ASCII punctuation is allowed in passwords. However, note that whitespace and
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