1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<span id="netrc-netrc-file-processing"></span><h1>netrc — netrc file processing</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/netrc.py">Lib/netrc.py</a></p> <p>The <a class="reference internal" href="#netrc.netrc" title="netrc.netrc"><code>netrc</code></a> class parses and encapsulates the netrc file format used by the Unix <strong class="program">ftp</strong> program and other FTP clients.</p> <dl class="py class"> <dt class="sig sig-object py" id="netrc.netrc">
<code>class netrc.netrc([file])</code> </dt> <dd>
<p>A <a class="reference internal" href="#netrc.netrc" title="netrc.netrc"><code>netrc</code></a> instance or subclass instance encapsulates data from a netrc file. The initialization argument, if present, specifies the file to parse. If no argument is given, the file <code>.netrc</code> in the user’s home directory – as determined by <a class="reference internal" href="os.path#os.path.expanduser" title="os.path.expanduser"><code>os.path.expanduser()</code></a> – will be read. Otherwise, a <a class="reference internal" href="exceptions#FileNotFoundError" title="FileNotFoundError"><code>FileNotFoundError</code></a> exception will be raised. Parse errors will raise <a class="reference internal" href="#netrc.NetrcParseError" title="netrc.NetrcParseError"><code>NetrcParseError</code></a> with diagnostic information including the file name, line number, and terminating token. If no argument is specified on a POSIX system, the presence of passwords in the <code>.netrc</code> file will raise a <a class="reference internal" href="#netrc.NetrcParseError" title="netrc.NetrcParseError"><code>NetrcParseError</code></a> if the file ownership or permissions are insecure (owned by a user other than the user running the process, or accessible for read or write by any other user). This implements security behavior equivalent to that of ftp and other programs that use <code>.netrc</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>Added the POSIX permission check.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.7: </span><a class="reference internal" href="os.path#os.path.expanduser" title="os.path.expanduser"><code>os.path.expanduser()</code></a> is used to find the location of the <code>.netrc</code> file when <em>file</em> is not passed as argument.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span><a class="reference internal" href="#module-netrc" title="netrc: Loading of .netrc files."><code>netrc</code></a> try UTF-8 encoding before using locale specific encoding. The entry in the netrc file no longer needs to contain all tokens. The missing tokens’ value default to an empty string. All the tokens and their values now can contain arbitrary characters, like whitespace and non-ASCII characters. If the login name is anonymous, it won’t trigger the security check.</p> </div> </dd>
</dl> <dl class="py exception"> <dt class="sig sig-object py" id="netrc.NetrcParseError">
<code>exception netrc.NetrcParseError</code> </dt> <dd>
<p>Exception raised by the <a class="reference internal" href="#netrc.netrc" title="netrc.netrc"><code>netrc</code></a> class when syntactical errors are encountered in source text. Instances of this exception provide three interesting attributes:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="netrc.NetrcParseError.msg">
<code>msg</code> </dt> <dd>
<p>Textual explanation of the error.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="netrc.NetrcParseError.filename">
<code>filename</code> </dt> <dd>
<p>The name of the source file.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="netrc.NetrcParseError.lineno">
<code>lineno</code> </dt> <dd>
<p>The line number on which the error was found.</p> </dd>
</dl> </dd>
</dl> <section id="netrc-objects"> <span id="id1"></span><h2>netrc Objects</h2> <p>A <a class="reference internal" href="#netrc.netrc" title="netrc.netrc"><code>netrc</code></a> instance has the following methods:</p> <dl class="py method"> <dt class="sig sig-object py" id="netrc.netrc.authenticators">
<code>netrc.authenticators(host)</code> </dt> <dd>
<p>Return a 3-tuple <code>(login, account, password)</code> of authenticators for <em>host</em>. If the netrc file did not contain an entry for the given host, return the tuple associated with the ‘default’ entry. If neither matching host nor default entry is available, return <code>None</code>.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="netrc.netrc.__repr__">
<code>netrc.__repr__()</code> </dt> <dd>
<p>Dump the class data as a string in the format of a netrc file. (This discards comments and may reorder the entries.)</p> </dd>
</dl> <p>Instances of <a class="reference internal" href="#netrc.netrc" title="netrc.netrc"><code>netrc</code></a> have public instance variables:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="netrc.netrc.hosts">
<code>netrc.hosts</code> </dt> <dd>
<p>Dictionary mapping host names to <code>(login, account, password)</code> tuples. The ‘default’ entry, if any, is represented as a pseudo-host by that name.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="netrc.netrc.macros">
<code>netrc.macros</code> </dt> <dd>
<p>Dictionary mapping macro names to string lists.</p> </dd>
</dl> </section> <div class="_attribution">
<p class="_attribution-p">
© 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br>
<a href="https://docs.python.org/3.12/library/netrc.html" class="_attribution-link">https://docs.python.org/3.12/library/netrc.html</a>
</p>
</div>
|