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