blob: c693cea87efed56e02434904cb12b7f22bbbe075 [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
Éric Araujo29a0b572011-08-19 02:14:03 +020013**Source code:** :source:`Lib/netrc.py`
14
15--------------
16
Georg Brandl8ec7f652007-08-15 14:28:01 +000017The :class:`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` 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.
R David Murray4189b672013-09-16 13:48:44 -040028 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`.
Georg Brandl8ec7f652007-08-15 14:28:01 +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
81 Passwords are limited to a subset of the ASCII character set. Versions of
82 this module prior to 2.3 were extremely limited. Starting with 2.3, all
83 ASCII punctuation is allowed in passwords. However, note that whitespace and
84 non-printable characters are not allowed in passwords. This is a limitation
85 of the way the .netrc file is parsed and may be removed in the future.
86