blob: 97927998fabab95d33143f6169cb8f0eff71943a [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
31
32.. exception:: NetrcParseError
33
34 Exception raised by the :class:`netrc` class when syntactical errors are
35 encountered in source text. Instances of this exception provide three
36 interesting attributes: :attr:`msg` is a textual explanation of the error,
37 :attr:`filename` is the name of the source file, and :attr:`lineno` gives the
38 line number on which the error was found.
39
40
41.. _netrc-objects:
42
43netrc Objects
44-------------
45
46A :class:`netrc` instance has the following methods:
47
48
49.. method:: netrc.authenticators(host)
50
51 Return a 3-tuple ``(login, account, password)`` of authenticators for *host*.
52 If the netrc file did not contain an entry for the given host, return the tuple
53 associated with the 'default' entry. If neither matching host nor default entry
54 is available, return ``None``.
55
56
57.. method:: netrc.__repr__()
58
59 Dump the class data as a string in the format of a netrc file. (This discards
60 comments and may reorder the entries.)
61
62Instances of :class:`netrc` have public instance variables:
63
64
65.. attribute:: netrc.hosts
66
67 Dictionary mapping host names to ``(login, account, password)`` tuples. The
68 'default' entry, if any, is represented as a pseudo-host by that name.
69
70
71.. attribute:: netrc.macros
72
73 Dictionary mapping macro names to string lists.
74
75.. note::
76
77 Passwords are limited to a subset of the ASCII character set. Versions of
78 this module prior to 2.3 were extremely limited. Starting with 2.3, all
79 ASCII punctuation is allowed in passwords. However, note that whitespace and
80 non-printable characters are not allowed in passwords. This is a limitation
81 of the way the .netrc file is parsed and may be removed in the future.
82