gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

427 lines
23KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="netrc — netrc file processing" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/netrc.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/netrc.py The netrc class parses and encapsulates the netrc file format used by the Unix ftp program and other FTP clients. netrc Objects: A netrc instance has the following methods..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/netrc.py The netrc class parses and encapsulates the netrc file format used by the Unix ftp program and other FTP clients. netrc Objects: A netrc instance has the following methods..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>netrc — netrc file processing &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="plistlib — Generate and parse Apple .plist files" href="plistlib.html" />
  33. <link rel="prev" title="tomllib — Parse TOML files" href="tomllib.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/netrc.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">netrc</span></code> — netrc file processing</a><ul>
  86. <li><a class="reference internal" href="#netrc-objects">netrc Objects</a></li>
  87. </ul>
  88. </li>
  89. </ul>
  90. </div>
  91. <div>
  92. <h4>Previous topic</h4>
  93. <p class="topless"><a href="tomllib.html"
  94. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tomllib</span></code> — Parse TOML files</a></p>
  95. </div>
  96. <div>
  97. <h4>Next topic</h4>
  98. <p class="topless"><a href="plistlib.html"
  99. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">plistlib</span></code> — Generate and parse Apple <code class="docutils literal notranslate"><span class="pre">.plist</span></code> files</a></p>
  100. </div>
  101. <div role="note" aria-label="source link">
  102. <h3>This Page</h3>
  103. <ul class="this-page-menu">
  104. <li><a href="../bugs.html">Report a Bug</a></li>
  105. <li>
  106. <a href="https://github.com/python/cpython/blob/main/Doc/library/netrc.rst"
  107. rel="nofollow">Show Source
  108. </a>
  109. </li>
  110. </ul>
  111. </div>
  112. </nav>
  113. </div>
  114. </div>
  115. <div class="related" role="navigation" aria-label="related navigation">
  116. <h3>Navigation</h3>
  117. <ul>
  118. <li class="right" style="margin-right: 10px">
  119. <a href="../genindex.html" title="General Index"
  120. accesskey="I">index</a></li>
  121. <li class="right" >
  122. <a href="../py-modindex.html" title="Python Module Index"
  123. >modules</a> |</li>
  124. <li class="right" >
  125. <a href="plistlib.html" title="plistlib — Generate and parse Apple .plist files"
  126. accesskey="N">next</a> |</li>
  127. <li class="right" >
  128. <a href="tomllib.html" title="tomllib — Parse TOML files"
  129. accesskey="P">previous</a> |</li>
  130. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  131. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  132. <li class="switchers">
  133. <div class="language_switcher_placeholder"></div>
  134. <div class="version_switcher_placeholder"></div>
  135. </li>
  136. <li>
  137. </li>
  138. <li id="cpython-language-and-version">
  139. <a href="../index.html">3.12.3 Documentation</a> &#187;
  140. </li>
  141. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  142. <li class="nav-item nav-item-2"><a href="fileformats.html" accesskey="U">File Formats</a> &#187;</li>
  143. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">netrc</span></code> — netrc file processing</a></li>
  144. <li class="right">
  145. <div class="inline-search" role="search">
  146. <form class="inline-search" action="../search.html" method="get">
  147. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  148. <input type="submit" value="Go" />
  149. </form>
  150. </div>
  151. |
  152. </li>
  153. <li class="right">
  154. <label class="theme-selector-label">
  155. Theme
  156. <select class="theme-selector" oninput="activateTheme(this.value)">
  157. <option value="auto" selected>Auto</option>
  158. <option value="light">Light</option>
  159. <option value="dark">Dark</option>
  160. </select>
  161. </label> |</li>
  162. </ul>
  163. </div>
  164. <div class="document">
  165. <div class="documentwrapper">
  166. <div class="bodywrapper">
  167. <div class="body" role="main">
  168. <section id="module-netrc">
  169. <span id="netrc-netrc-file-processing"></span><h1><a class="reference internal" href="#module-netrc" title="netrc: Loading of .netrc files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">netrc</span></code></a> — netrc file processing<a class="headerlink" href="#module-netrc" title="Link to this heading">¶</a></h1>
  170. <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>
  171. <hr class="docutils" />
  172. <p>The <a class="reference internal" href="#netrc.netrc" title="netrc.netrc"><code class="xref py py-class docutils literal notranslate"><span class="pre">netrc</span></code></a> class parses and encapsulates the netrc file format used by
  173. the Unix <strong class="program">ftp</strong> program and other FTP clients.</p>
  174. <dl class="py class">
  175. <dt class="sig sig-object py" id="netrc.netrc">
  176. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">netrc.</span></span><span class="sig-name descname"><span class="pre">netrc</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">file</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#netrc.netrc" title="Link to this definition">¶</a></dt>
  177. <dd><p>A <a class="reference internal" href="#netrc.netrc" title="netrc.netrc"><code class="xref py py-class docutils literal notranslate"><span class="pre">netrc</span></code></a> instance or subclass instance encapsulates data from a netrc
  178. file. The initialization argument, if present, specifies the file to parse. If
  179. no argument is given, the file <code class="file docutils literal notranslate"><span class="pre">.netrc</span></code> in the user’s home directory –
  180. as determined by <a class="reference internal" href="os.path.html#os.path.expanduser" title="os.path.expanduser"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.expanduser()</span></code></a> – will be read. Otherwise,
  181. a <a class="reference internal" href="exceptions.html#FileNotFoundError" title="FileNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileNotFoundError</span></code></a> exception will be raised.
  182. Parse errors will raise <a class="reference internal" href="#netrc.NetrcParseError" title="netrc.NetrcParseError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NetrcParseError</span></code></a> with diagnostic
  183. information including the file name, line number, and terminating token.
  184. If no argument is specified on a POSIX system, the presence of passwords in
  185. the <code class="file docutils literal notranslate"><span class="pre">.netrc</span></code> file will raise a <a class="reference internal" href="#netrc.NetrcParseError" title="netrc.NetrcParseError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NetrcParseError</span></code></a> if the file
  186. ownership or permissions are insecure (owned by a user other than the user
  187. running the process, or accessible for read or write by any other user).
  188. This implements security behavior equivalent to that of ftp and other
  189. programs that use <code class="file docutils literal notranslate"><span class="pre">.netrc</span></code>.</p>
  190. <div class="versionchanged">
  191. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added the POSIX permission check.</p>
  192. </div>
  193. <div class="versionchanged">
  194. <p><span class="versionmodified changed">Changed in version 3.7: </span><a class="reference internal" href="os.path.html#os.path.expanduser" title="os.path.expanduser"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.expanduser()</span></code></a> is used to find the location of the
  195. <code class="file docutils literal notranslate"><span class="pre">.netrc</span></code> file when <em>file</em> is not passed as argument.</p>
  196. </div>
  197. <div class="versionchanged">
  198. <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 class="xref py py-class docutils literal notranslate"><span class="pre">netrc</span></code></a> try UTF-8 encoding before using locale specific
  199. encoding.
  200. The entry in the netrc file no longer needs to contain all tokens. The missing
  201. tokens’ value default to an empty string. All the tokens and their values now
  202. can contain arbitrary characters, like whitespace and non-ASCII characters.
  203. If the login name is anonymous, it won’t trigger the security check.</p>
  204. </div>
  205. </dd></dl>
  206. <dl class="py exception">
  207. <dt class="sig sig-object py" id="netrc.NetrcParseError">
  208. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">netrc.</span></span><span class="sig-name descname"><span class="pre">NetrcParseError</span></span><a class="headerlink" href="#netrc.NetrcParseError" title="Link to this definition">¶</a></dt>
  209. <dd><p>Exception raised by the <a class="reference internal" href="#netrc.netrc" title="netrc.netrc"><code class="xref py py-class docutils literal notranslate"><span class="pre">netrc</span></code></a> class when syntactical errors are
  210. encountered in source text. Instances of this exception provide three
  211. interesting attributes:</p>
  212. <dl class="py attribute">
  213. <dt class="sig sig-object py" id="netrc.NetrcParseError.msg">
  214. <span class="sig-name descname"><span class="pre">msg</span></span><a class="headerlink" href="#netrc.NetrcParseError.msg" title="Link to this definition">¶</a></dt>
  215. <dd><p>Textual explanation of the error.</p>
  216. </dd></dl>
  217. <dl class="py attribute">
  218. <dt class="sig sig-object py" id="netrc.NetrcParseError.filename">
  219. <span class="sig-name descname"><span class="pre">filename</span></span><a class="headerlink" href="#netrc.NetrcParseError.filename" title="Link to this definition">¶</a></dt>
  220. <dd><p>The name of the source file.</p>
  221. </dd></dl>
  222. <dl class="py attribute">
  223. <dt class="sig sig-object py" id="netrc.NetrcParseError.lineno">
  224. <span class="sig-name descname"><span class="pre">lineno</span></span><a class="headerlink" href="#netrc.NetrcParseError.lineno" title="Link to this definition">¶</a></dt>
  225. <dd><p>The line number on which the error was found.</p>
  226. </dd></dl>
  227. </dd></dl>
  228. <section id="netrc-objects">
  229. <span id="id1"></span><h2>netrc Objects<a class="headerlink" href="#netrc-objects" title="Link to this heading">¶</a></h2>
  230. <p>A <a class="reference internal" href="#netrc.netrc" title="netrc.netrc"><code class="xref py py-class docutils literal notranslate"><span class="pre">netrc</span></code></a> instance has the following methods:</p>
  231. <dl class="py method">
  232. <dt class="sig sig-object py" id="netrc.netrc.authenticators">
  233. <span class="sig-prename descclassname"><span class="pre">netrc.</span></span><span class="sig-name descname"><span class="pre">authenticators</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">host</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#netrc.netrc.authenticators" title="Link to this definition">¶</a></dt>
  234. <dd><p>Return a 3-tuple <code class="docutils literal notranslate"><span class="pre">(login,</span> <span class="pre">account,</span> <span class="pre">password)</span></code> of authenticators for <em>host</em>.
  235. If the netrc file did not contain an entry for the given host, return the tuple
  236. associated with the ‘default’ entry. If neither matching host nor default entry
  237. is available, return <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  238. </dd></dl>
  239. <dl class="py method">
  240. <dt class="sig sig-object py" id="netrc.netrc.__repr__">
  241. <span class="sig-prename descclassname"><span class="pre">netrc.</span></span><span class="sig-name descname"><span class="pre">__repr__</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#netrc.netrc.__repr__" title="Link to this definition">¶</a></dt>
  242. <dd><p>Dump the class data as a string in the format of a netrc file. (This discards
  243. comments and may reorder the entries.)</p>
  244. </dd></dl>
  245. <p>Instances of <a class="reference internal" href="#netrc.netrc" title="netrc.netrc"><code class="xref py py-class docutils literal notranslate"><span class="pre">netrc</span></code></a> have public instance variables:</p>
  246. <dl class="py attribute">
  247. <dt class="sig sig-object py" id="netrc.netrc.hosts">
  248. <span class="sig-prename descclassname"><span class="pre">netrc.</span></span><span class="sig-name descname"><span class="pre">hosts</span></span><a class="headerlink" href="#netrc.netrc.hosts" title="Link to this definition">¶</a></dt>
  249. <dd><p>Dictionary mapping host names to <code class="docutils literal notranslate"><span class="pre">(login,</span> <span class="pre">account,</span> <span class="pre">password)</span></code> tuples. The
  250. ‘default’ entry, if any, is represented as a pseudo-host by that name.</p>
  251. </dd></dl>
  252. <dl class="py attribute">
  253. <dt class="sig sig-object py" id="netrc.netrc.macros">
  254. <span class="sig-prename descclassname"><span class="pre">netrc.</span></span><span class="sig-name descname"><span class="pre">macros</span></span><a class="headerlink" href="#netrc.netrc.macros" title="Link to this definition">¶</a></dt>
  255. <dd><p>Dictionary mapping macro names to string lists.</p>
  256. </dd></dl>
  257. </section>
  258. </section>
  259. <div class="clearer"></div>
  260. </div>
  261. </div>
  262. </div>
  263. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  264. <div class="sphinxsidebarwrapper">
  265. <div>
  266. <h3><a href="../contents.html">Table of Contents</a></h3>
  267. <ul>
  268. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">netrc</span></code> — netrc file processing</a><ul>
  269. <li><a class="reference internal" href="#netrc-objects">netrc Objects</a></li>
  270. </ul>
  271. </li>
  272. </ul>
  273. </div>
  274. <div>
  275. <h4>Previous topic</h4>
  276. <p class="topless"><a href="tomllib.html"
  277. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tomllib</span></code> — Parse TOML files</a></p>
  278. </div>
  279. <div>
  280. <h4>Next topic</h4>
  281. <p class="topless"><a href="plistlib.html"
  282. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">plistlib</span></code> — Generate and parse Apple <code class="docutils literal notranslate"><span class="pre">.plist</span></code> files</a></p>
  283. </div>
  284. <div role="note" aria-label="source link">
  285. <h3>This Page</h3>
  286. <ul class="this-page-menu">
  287. <li><a href="../bugs.html">Report a Bug</a></li>
  288. <li>
  289. <a href="https://github.com/python/cpython/blob/main/Doc/library/netrc.rst"
  290. rel="nofollow">Show Source
  291. </a>
  292. </li>
  293. </ul>
  294. </div>
  295. </div>
  296. <div id="sidebarbutton" title="Collapse sidebar">
  297. <span>«</span>
  298. </div>
  299. </div>
  300. <div class="clearer"></div>
  301. </div>
  302. <div class="related" role="navigation" aria-label="related navigation">
  303. <h3>Navigation</h3>
  304. <ul>
  305. <li class="right" style="margin-right: 10px">
  306. <a href="../genindex.html" title="General Index"
  307. >index</a></li>
  308. <li class="right" >
  309. <a href="../py-modindex.html" title="Python Module Index"
  310. >modules</a> |</li>
  311. <li class="right" >
  312. <a href="plistlib.html" title="plistlib — Generate and parse Apple .plist files"
  313. >next</a> |</li>
  314. <li class="right" >
  315. <a href="tomllib.html" title="tomllib — Parse TOML files"
  316. >previous</a> |</li>
  317. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  318. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  319. <li class="switchers">
  320. <div class="language_switcher_placeholder"></div>
  321. <div class="version_switcher_placeholder"></div>
  322. </li>
  323. <li>
  324. </li>
  325. <li id="cpython-language-and-version">
  326. <a href="../index.html">3.12.3 Documentation</a> &#187;
  327. </li>
  328. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  329. <li class="nav-item nav-item-2"><a href="fileformats.html" >File Formats</a> &#187;</li>
  330. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">netrc</span></code> — netrc file processing</a></li>
  331. <li class="right">
  332. <div class="inline-search" role="search">
  333. <form class="inline-search" action="../search.html" method="get">
  334. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  335. <input type="submit" value="Go" />
  336. </form>
  337. </div>
  338. |
  339. </li>
  340. <li class="right">
  341. <label class="theme-selector-label">
  342. Theme
  343. <select class="theme-selector" oninput="activateTheme(this.value)">
  344. <option value="auto" selected>Auto</option>
  345. <option value="light">Light</option>
  346. <option value="dark">Dark</option>
  347. </select>
  348. </label> |</li>
  349. </ul>
  350. </div>
  351. <div class="footer">
  352. &copy;
  353. <a href="../copyright.html">
  354. Copyright
  355. </a>
  356. 2001-2024, Python Software Foundation.
  357. <br />
  358. This page is licensed under the Python Software Foundation License Version 2.
  359. <br />
  360. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  361. <br />
  362. See <a href="/license.html">History and License</a> for more information.<br />
  363. <br />
  364. The Python Software Foundation is a non-profit corporation.
  365. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  366. <br />
  367. <br />
  368. Last updated on Apr 09, 2024 (13:47 UTC).
  369. <a href="/bugs.html">Found a bug</a>?
  370. <br />
  371. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  372. </div>
  373. </body>
  374. </html>
上海开阖软件有限公司 沪ICP备12045867号-1