gooderp18绿色标准版
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

541 líneas
44KB

  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="email.utils: Miscellaneous utilities" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/email.utils.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/email/utils.py There are a couple of useful utilities provided in the email.utils module: The remaining functions are part of the legacy ( Compat32) email API. There is no need to ..." />
  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/email/utils.py There are a couple of useful utilities provided in the email.utils module: The remaining functions are part of the legacy ( Compat32) email API. There is no need to ..." />
  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>email.utils: Miscellaneous utilities &#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="email.iterators: Iterators" href="email.iterators.html" />
  33. <link rel="prev" title="email.encoders: Encoders" href="email.encoders.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/email.utils.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. <h4>Previous topic</h4>
  84. <p class="topless"><a href="email.encoders.html"
  85. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.encoders</span></code>: Encoders</a></p>
  86. </div>
  87. <div>
  88. <h4>Next topic</h4>
  89. <p class="topless"><a href="email.iterators.html"
  90. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.iterators</span></code>: Iterators</a></p>
  91. </div>
  92. <div role="note" aria-label="source link">
  93. <h3>This Page</h3>
  94. <ul class="this-page-menu">
  95. <li><a href="../bugs.html">Report a Bug</a></li>
  96. <li>
  97. <a href="https://github.com/python/cpython/blob/main/Doc/library/email.utils.rst"
  98. rel="nofollow">Show Source
  99. </a>
  100. </li>
  101. </ul>
  102. </div>
  103. </nav>
  104. </div>
  105. </div>
  106. <div class="related" role="navigation" aria-label="related navigation">
  107. <h3>Navigation</h3>
  108. <ul>
  109. <li class="right" style="margin-right: 10px">
  110. <a href="../genindex.html" title="General Index"
  111. accesskey="I">index</a></li>
  112. <li class="right" >
  113. <a href="../py-modindex.html" title="Python Module Index"
  114. >modules</a> |</li>
  115. <li class="right" >
  116. <a href="email.iterators.html" title="email.iterators: Iterators"
  117. accesskey="N">next</a> |</li>
  118. <li class="right" >
  119. <a href="email.encoders.html" title="email.encoders: Encoders"
  120. accesskey="P">previous</a> |</li>
  121. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  122. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  123. <li class="switchers">
  124. <div class="language_switcher_placeholder"></div>
  125. <div class="version_switcher_placeholder"></div>
  126. </li>
  127. <li>
  128. </li>
  129. <li id="cpython-language-and-version">
  130. <a href="../index.html">3.12.3 Documentation</a> &#187;
  131. </li>
  132. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  133. <li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> &#187;</li>
  134. <li class="nav-item nav-item-3"><a href="email.html" accesskey="U"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code> — An email and MIME handling package</a> &#187;</li>
  135. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.utils</span></code>: Miscellaneous utilities</a></li>
  136. <li class="right">
  137. <div class="inline-search" role="search">
  138. <form class="inline-search" action="../search.html" method="get">
  139. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  140. <input type="submit" value="Go" />
  141. </form>
  142. </div>
  143. |
  144. </li>
  145. <li class="right">
  146. <label class="theme-selector-label">
  147. Theme
  148. <select class="theme-selector" oninput="activateTheme(this.value)">
  149. <option value="auto" selected>Auto</option>
  150. <option value="light">Light</option>
  151. <option value="dark">Dark</option>
  152. </select>
  153. </label> |</li>
  154. </ul>
  155. </div>
  156. <div class="document">
  157. <div class="documentwrapper">
  158. <div class="bodywrapper">
  159. <div class="body" role="main">
  160. <section id="module-email.utils">
  161. <span id="email-utils-miscellaneous-utilities"></span><h1><a class="reference internal" href="#module-email.utils" title="email.utils: Miscellaneous email package utilities."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.utils</span></code></a>: Miscellaneous utilities<a class="headerlink" href="#module-email.utils" title="Link to this heading">¶</a></h1>
  162. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/email/utils.py">Lib/email/utils.py</a></p>
  163. <hr class="docutils" />
  164. <p>There are a couple of useful utilities provided in the <a class="reference internal" href="#module-email.utils" title="email.utils: Miscellaneous email package utilities."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.utils</span></code></a>
  165. module:</p>
  166. <dl class="py function">
  167. <dt class="sig sig-object py" id="email.utils.localtime">
  168. <span class="sig-prename descclassname"><span class="pre">email.utils.</span></span><span class="sig-name descname"><span class="pre">localtime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">dt</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.utils.localtime" title="Link to this definition">¶</a></dt>
  169. <dd><p>Return local time as an aware datetime object. If called without
  170. arguments, return current time. Otherwise <em>dt</em> argument should be a
  171. <a class="reference internal" href="datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> instance, and it is converted to the local time
  172. zone according to the system time zone database. If <em>dt</em> is naive (that
  173. is, <code class="docutils literal notranslate"><span class="pre">dt.tzinfo</span></code> is <code class="docutils literal notranslate"><span class="pre">None</span></code>), it is assumed to be in local time. The
  174. <em>isdst</em> parameter is ignored.</p>
  175. <div class="versionadded">
  176. <p><span class="versionmodified added">New in version 3.3.</span></p>
  177. </div>
  178. <div class="deprecated-removed">
  179. <p><span class="versionmodified">Deprecated since version 3.12, will be removed in version 3.14: </span>The <em>isdst</em> parameter.</p>
  180. </div>
  181. </dd></dl>
  182. <dl class="py function">
  183. <dt class="sig sig-object py" id="email.utils.make_msgid">
  184. <span class="sig-prename descclassname"><span class="pre">email.utils.</span></span><span class="sig-name descname"><span class="pre">make_msgid</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">idstring</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">domain</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.utils.make_msgid" title="Link to this definition">¶</a></dt>
  185. <dd><p>Returns a string suitable for an <span class="target" id="index-0"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2822.html"><strong>RFC 2822</strong></a>-compliant
  186. <em class="mailheader">Message-ID</em> header. Optional <em>idstring</em> if given, is a string
  187. used to strengthen the uniqueness of the message id. Optional <em>domain</em> if
  188. given provides the portion of the msgid after the ‘&#64;’. The default is the
  189. local hostname. It is not normally necessary to override this default, but
  190. may be useful certain cases, such as a constructing distributed system that
  191. uses a consistent domain name across multiple hosts.</p>
  192. <div class="versionchanged">
  193. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added the <em>domain</em> keyword.</p>
  194. </div>
  195. </dd></dl>
  196. <p>The remaining functions are part of the legacy (<code class="docutils literal notranslate"><span class="pre">Compat32</span></code>) email API. There
  197. is no need to directly use these with the new API, since the parsing and
  198. formatting they provide is done automatically by the header parsing machinery
  199. of the new API.</p>
  200. <dl class="py function">
  201. <dt class="sig sig-object py" id="email.utils.quote">
  202. <span class="sig-prename descclassname"><span class="pre">email.utils.</span></span><span class="sig-name descname"><span class="pre">quote</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.utils.quote" title="Link to this definition">¶</a></dt>
  203. <dd><p>Return a new string with backslashes in <em>str</em> replaced by two backslashes, and
  204. double quotes replaced by backslash-double quote.</p>
  205. </dd></dl>
  206. <dl class="py function">
  207. <dt class="sig sig-object py" id="email.utils.unquote">
  208. <span class="sig-prename descclassname"><span class="pre">email.utils.</span></span><span class="sig-name descname"><span class="pre">unquote</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.utils.unquote" title="Link to this definition">¶</a></dt>
  209. <dd><p>Return a new string which is an <em>unquoted</em> version of <em>str</em>. If <em>str</em> ends and
  210. begins with double quotes, they are stripped off. Likewise if <em>str</em> ends and
  211. begins with angle brackets, they are stripped off.</p>
  212. </dd></dl>
  213. <dl class="py function">
  214. <dt class="sig sig-object py" id="email.utils.parseaddr">
  215. <span class="sig-prename descclassname"><span class="pre">email.utils.</span></span><span class="sig-name descname"><span class="pre">parseaddr</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">address</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.utils.parseaddr" title="Link to this definition">¶</a></dt>
  216. <dd><p>Parse address – which should be the value of some address-containing field such
  217. as <em class="mailheader">To</em> or <em class="mailheader">Cc</em> – into its constituent <em>realname</em> and
  218. <em>email address</em> parts. Returns a tuple of that information, unless the parse
  219. fails, in which case a 2-tuple of <code class="docutils literal notranslate"><span class="pre">('',</span> <span class="pre">'')</span></code> is returned.</p>
  220. </dd></dl>
  221. <dl class="py function">
  222. <dt class="sig sig-object py" id="email.utils.formataddr">
  223. <span class="sig-prename descclassname"><span class="pre">email.utils.</span></span><span class="sig-name descname"><span class="pre">formataddr</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pair</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">charset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'utf-8'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.utils.formataddr" title="Link to this definition">¶</a></dt>
  224. <dd><p>The inverse of <a class="reference internal" href="#email.utils.parseaddr" title="email.utils.parseaddr"><code class="xref py py-meth docutils literal notranslate"><span class="pre">parseaddr()</span></code></a>, this takes a 2-tuple of the form <code class="docutils literal notranslate"><span class="pre">(realname,</span>
  225. <span class="pre">email_address)</span></code> and returns the string value suitable for a <em class="mailheader">To</em> or
  226. <em class="mailheader">Cc</em> header. If the first element of <em>pair</em> is false, then the
  227. second element is returned unmodified.</p>
  228. <p>Optional <em>charset</em> is the character set that will be used in the <span class="target" id="index-1"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2047.html"><strong>RFC 2047</strong></a>
  229. encoding of the <code class="docutils literal notranslate"><span class="pre">realname</span></code> if the <code class="docutils literal notranslate"><span class="pre">realname</span></code> contains non-ASCII
  230. characters. Can be an instance of <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> or a
  231. <a class="reference internal" href="email.charset.html#email.charset.Charset" title="email.charset.Charset"><code class="xref py py-class docutils literal notranslate"><span class="pre">Charset</span></code></a>. Defaults to <code class="docutils literal notranslate"><span class="pre">utf-8</span></code>.</p>
  232. <div class="versionchanged">
  233. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>charset</em> option.</p>
  234. </div>
  235. </dd></dl>
  236. <dl class="py function">
  237. <dt class="sig sig-object py" id="email.utils.getaddresses">
  238. <span class="sig-prename descclassname"><span class="pre">email.utils.</span></span><span class="sig-name descname"><span class="pre">getaddresses</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fieldvalues</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.utils.getaddresses" title="Link to this definition">¶</a></dt>
  239. <dd><p>This method returns a list of 2-tuples of the form returned by <code class="docutils literal notranslate"><span class="pre">parseaddr()</span></code>.
  240. <em>fieldvalues</em> is a sequence of header field values as might be returned by
  241. <a class="reference internal" href="email.compat32-message.html#email.message.Message.get_all" title="email.message.Message.get_all"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Message.get_all</span></code></a>. Here’s a simple
  242. example that gets all the recipients of a message:</p>
  243. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">email.utils</span> <span class="kn">import</span> <span class="n">getaddresses</span>
  244. <span class="n">tos</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="n">get_all</span><span class="p">(</span><span class="s1">&#39;to&#39;</span><span class="p">,</span> <span class="p">[])</span>
  245. <span class="n">ccs</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="n">get_all</span><span class="p">(</span><span class="s1">&#39;cc&#39;</span><span class="p">,</span> <span class="p">[])</span>
  246. <span class="n">resent_tos</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="n">get_all</span><span class="p">(</span><span class="s1">&#39;resent-to&#39;</span><span class="p">,</span> <span class="p">[])</span>
  247. <span class="n">resent_ccs</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="n">get_all</span><span class="p">(</span><span class="s1">&#39;resent-cc&#39;</span><span class="p">,</span> <span class="p">[])</span>
  248. <span class="n">all_recipients</span> <span class="o">=</span> <span class="n">getaddresses</span><span class="p">(</span><span class="n">tos</span> <span class="o">+</span> <span class="n">ccs</span> <span class="o">+</span> <span class="n">resent_tos</span> <span class="o">+</span> <span class="n">resent_ccs</span><span class="p">)</span>
  249. </pre></div>
  250. </div>
  251. </dd></dl>
  252. <dl class="py function">
  253. <dt class="sig sig-object py" id="email.utils.parsedate">
  254. <span class="sig-prename descclassname"><span class="pre">email.utils.</span></span><span class="sig-name descname"><span class="pre">parsedate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">date</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.utils.parsedate" title="Link to this definition">¶</a></dt>
  255. <dd><p>Attempts to parse a date according to the rules in <span class="target" id="index-2"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2822.html"><strong>RFC 2822</strong></a>. however, some
  256. mailers don’t follow that format as specified, so <a class="reference internal" href="#email.utils.parsedate" title="email.utils.parsedate"><code class="xref py py-func docutils literal notranslate"><span class="pre">parsedate()</span></code></a> tries to
  257. guess correctly in such cases. <em>date</em> is a string containing an <span class="target" id="index-3"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2822.html"><strong>RFC 2822</strong></a>
  258. date, such as <code class="docutils literal notranslate"><span class="pre">&quot;Mon,</span> <span class="pre">20</span> <span class="pre">Nov</span> <span class="pre">1995</span> <span class="pre">19:12:08</span> <span class="pre">-0500&quot;</span></code>. If it succeeds in parsing
  259. the date, <a class="reference internal" href="#email.utils.parsedate" title="email.utils.parsedate"><code class="xref py py-func docutils literal notranslate"><span class="pre">parsedate()</span></code></a> returns a 9-tuple that can be passed directly to
  260. <a class="reference internal" href="time.html#time.mktime" title="time.mktime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.mktime()</span></code></a>; otherwise <code class="docutils literal notranslate"><span class="pre">None</span></code> will be returned. Note that indexes 6,
  261. 7, and 8 of the result tuple are not usable.</p>
  262. </dd></dl>
  263. <dl class="py function">
  264. <dt class="sig sig-object py" id="email.utils.parsedate_tz">
  265. <span class="sig-prename descclassname"><span class="pre">email.utils.</span></span><span class="sig-name descname"><span class="pre">parsedate_tz</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">date</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.utils.parsedate_tz" title="Link to this definition">¶</a></dt>
  266. <dd><p>Performs the same function as <a class="reference internal" href="#email.utils.parsedate" title="email.utils.parsedate"><code class="xref py py-func docutils literal notranslate"><span class="pre">parsedate()</span></code></a>, but returns either <code class="docutils literal notranslate"><span class="pre">None</span></code> or
  267. a 10-tuple; the first 9 elements make up a tuple that can be passed directly to
  268. <a class="reference internal" href="time.html#time.mktime" title="time.mktime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.mktime()</span></code></a>, and the tenth is the offset of the date’s timezone from UTC
  269. (which is the official term for Greenwich Mean Time) <a class="footnote-reference brackets" href="#id2" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a>. If the input string
  270. has no timezone, the last element of the tuple returned is <code class="docutils literal notranslate"><span class="pre">0</span></code>, which represents
  271. UTC. Note that indexes 6, 7, and 8 of the result tuple are not usable.</p>
  272. </dd></dl>
  273. <dl class="py function">
  274. <dt class="sig sig-object py" id="email.utils.parsedate_to_datetime">
  275. <span class="sig-prename descclassname"><span class="pre">email.utils.</span></span><span class="sig-name descname"><span class="pre">parsedate_to_datetime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">date</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.utils.parsedate_to_datetime" title="Link to this definition">¶</a></dt>
  276. <dd><p>The inverse of <a class="reference internal" href="#email.utils.format_datetime" title="email.utils.format_datetime"><code class="xref py py-func docutils literal notranslate"><span class="pre">format_datetime()</span></code></a>. Performs the same function as
  277. <a class="reference internal" href="#email.utils.parsedate" title="email.utils.parsedate"><code class="xref py py-func docutils literal notranslate"><span class="pre">parsedate()</span></code></a>, but on success returns a <a class="reference internal" href="datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a>;
  278. otherwise <code class="docutils literal notranslate"><span class="pre">ValueError</span></code> is raised if <em>date</em> contains an invalid value such
  279. as an hour greater than 23 or a timezone offset not between -24 and 24 hours.
  280. If the input date has a timezone of <code class="docutils literal notranslate"><span class="pre">-0000</span></code>, the <code class="docutils literal notranslate"><span class="pre">datetime</span></code> will be a naive
  281. <code class="docutils literal notranslate"><span class="pre">datetime</span></code>, and if the date is conforming to the RFCs it will represent a
  282. time in UTC but with no indication of the actual source timezone of the
  283. message the date comes from. If the input date has any other valid timezone
  284. offset, the <code class="docutils literal notranslate"><span class="pre">datetime</span></code> will be an aware <code class="docutils literal notranslate"><span class="pre">datetime</span></code> with the
  285. corresponding a <a class="reference internal" href="datetime.html#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> <a class="reference internal" href="datetime.html#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a>.</p>
  286. <div class="versionadded">
  287. <p><span class="versionmodified added">New in version 3.3.</span></p>
  288. </div>
  289. </dd></dl>
  290. <dl class="py function">
  291. <dt class="sig sig-object py" id="email.utils.mktime_tz">
  292. <span class="sig-prename descclassname"><span class="pre">email.utils.</span></span><span class="sig-name descname"><span class="pre">mktime_tz</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tuple</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.utils.mktime_tz" title="Link to this definition">¶</a></dt>
  293. <dd><p>Turn a 10-tuple as returned by <a class="reference internal" href="#email.utils.parsedate_tz" title="email.utils.parsedate_tz"><code class="xref py py-func docutils literal notranslate"><span class="pre">parsedate_tz()</span></code></a> into a UTC
  294. timestamp (seconds since the Epoch). If the timezone item in the
  295. tuple is <code class="docutils literal notranslate"><span class="pre">None</span></code>, assume local time.</p>
  296. </dd></dl>
  297. <dl class="py function">
  298. <dt class="sig sig-object py" id="email.utils.formatdate">
  299. <span class="sig-prename descclassname"><span class="pre">email.utils.</span></span><span class="sig-name descname"><span class="pre">formatdate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">timeval</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">localtime</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">usegmt</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.utils.formatdate" title="Link to this definition">¶</a></dt>
  300. <dd><p>Returns a date string as per <span class="target" id="index-4"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2822.html"><strong>RFC 2822</strong></a>, e.g.:</p>
  301. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Fri</span><span class="p">,</span> <span class="mi">09</span> <span class="n">Nov</span> <span class="mi">2001</span> <span class="mi">01</span><span class="p">:</span><span class="mi">08</span><span class="p">:</span><span class="mi">47</span> <span class="o">-</span><span class="mi">0000</span>
  302. </pre></div>
  303. </div>
  304. <p>Optional <em>timeval</em> if given is a floating point time value as accepted by
  305. <a class="reference internal" href="time.html#time.gmtime" title="time.gmtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.gmtime()</span></code></a> and <a class="reference internal" href="time.html#time.localtime" title="time.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.localtime()</span></code></a>, otherwise the current time is
  306. used.</p>
  307. <p>Optional <em>localtime</em> is a flag that when <code class="docutils literal notranslate"><span class="pre">True</span></code>, interprets <em>timeval</em>, and
  308. returns a date relative to the local timezone instead of UTC, properly taking
  309. daylight savings time into account. The default is <code class="docutils literal notranslate"><span class="pre">False</span></code> meaning UTC is
  310. used.</p>
  311. <p>Optional <em>usegmt</em> is a flag that when <code class="docutils literal notranslate"><span class="pre">True</span></code>, outputs a date string with the
  312. timezone as an ascii string <code class="docutils literal notranslate"><span class="pre">GMT</span></code>, rather than a numeric <code class="docutils literal notranslate"><span class="pre">-0000</span></code>. This is
  313. needed for some protocols (such as HTTP). This only applies when <em>localtime</em> is
  314. <code class="docutils literal notranslate"><span class="pre">False</span></code>. The default is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  315. </dd></dl>
  316. <dl class="py function">
  317. <dt class="sig sig-object py" id="email.utils.format_datetime">
  318. <span class="sig-prename descclassname"><span class="pre">email.utils.</span></span><span class="sig-name descname"><span class="pre">format_datetime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">dt</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">usegmt</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.utils.format_datetime" title="Link to this definition">¶</a></dt>
  319. <dd><p>Like <code class="docutils literal notranslate"><span class="pre">formatdate</span></code>, but the input is a <a class="reference internal" href="datetime.html#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> instance. If it is
  320. a naive datetime, it is assumed to be “UTC with no information about the
  321. source timezone”, and the conventional <code class="docutils literal notranslate"><span class="pre">-0000</span></code> is used for the timezone.
  322. If it is an aware <code class="docutils literal notranslate"><span class="pre">datetime</span></code>, then the numeric timezone offset is used.
  323. If it is an aware timezone with offset zero, then <em>usegmt</em> may be set to
  324. <code class="docutils literal notranslate"><span class="pre">True</span></code>, in which case the string <code class="docutils literal notranslate"><span class="pre">GMT</span></code> is used instead of the numeric
  325. timezone offset. This provides a way to generate standards conformant HTTP
  326. date headers.</p>
  327. <div class="versionadded">
  328. <p><span class="versionmodified added">New in version 3.3.</span></p>
  329. </div>
  330. </dd></dl>
  331. <dl class="py function">
  332. <dt class="sig sig-object py" id="email.utils.decode_rfc2231">
  333. <span class="sig-prename descclassname"><span class="pre">email.utils.</span></span><span class="sig-name descname"><span class="pre">decode_rfc2231</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.utils.decode_rfc2231" title="Link to this definition">¶</a></dt>
  334. <dd><p>Decode the string <em>s</em> according to <span class="target" id="index-5"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2231.html"><strong>RFC 2231</strong></a>.</p>
  335. </dd></dl>
  336. <dl class="py function">
  337. <dt class="sig sig-object py" id="email.utils.encode_rfc2231">
  338. <span class="sig-prename descclassname"><span class="pre">email.utils.</span></span><span class="sig-name descname"><span class="pre">encode_rfc2231</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">charset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">language</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.utils.encode_rfc2231" title="Link to this definition">¶</a></dt>
  339. <dd><p>Encode the string <em>s</em> according to <span class="target" id="index-6"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2231.html"><strong>RFC 2231</strong></a>. Optional <em>charset</em> and
  340. <em>language</em>, if given is the character set name and language name to use. If
  341. neither is given, <em>s</em> is returned as-is. If <em>charset</em> is given but <em>language</em>
  342. is not, the string is encoded using the empty string for <em>language</em>.</p>
  343. </dd></dl>
  344. <dl class="py function">
  345. <dt class="sig sig-object py" id="email.utils.collapse_rfc2231_value">
  346. <span class="sig-prename descclassname"><span class="pre">email.utils.</span></span><span class="sig-name descname"><span class="pre">collapse_rfc2231_value</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">value</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'replace'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fallback_charset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'us-ascii'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.utils.collapse_rfc2231_value" title="Link to this definition">¶</a></dt>
  347. <dd><p>When a header parameter is encoded in <span class="target" id="index-7"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2231.html"><strong>RFC 2231</strong></a> format,
  348. <a class="reference internal" href="email.compat32-message.html#email.message.Message.get_param" title="email.message.Message.get_param"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Message.get_param</span></code></a> may return a
  349. 3-tuple containing the character set,
  350. language, and value. <a class="reference internal" href="#email.utils.collapse_rfc2231_value" title="email.utils.collapse_rfc2231_value"><code class="xref py py-func docutils literal notranslate"><span class="pre">collapse_rfc2231_value()</span></code></a> turns this into a unicode
  351. string. Optional <em>errors</em> is passed to the <em>errors</em> argument of <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>’s
  352. <a class="reference internal" href="stdtypes.html#str.encode" title="str.encode"><code class="xref py py-func docutils literal notranslate"><span class="pre">encode()</span></code></a> method; it defaults to <code class="docutils literal notranslate"><span class="pre">'replace'</span></code>. Optional
  353. <em>fallback_charset</em> specifies the character set to use if the one in the
  354. <span class="target" id="index-8"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2231.html"><strong>RFC 2231</strong></a> header is not known by Python; it defaults to <code class="docutils literal notranslate"><span class="pre">'us-ascii'</span></code>.</p>
  355. <p>For convenience, if the <em>value</em> passed to <a class="reference internal" href="#email.utils.collapse_rfc2231_value" title="email.utils.collapse_rfc2231_value"><code class="xref py py-func docutils literal notranslate"><span class="pre">collapse_rfc2231_value()</span></code></a> is not
  356. a tuple, it should be a string and it is returned unquoted.</p>
  357. </dd></dl>
  358. <dl class="py function">
  359. <dt class="sig sig-object py" id="email.utils.decode_params">
  360. <span class="sig-prename descclassname"><span class="pre">email.utils.</span></span><span class="sig-name descname"><span class="pre">decode_params</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">params</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.utils.decode_params" title="Link to this definition">¶</a></dt>
  361. <dd><p>Decode parameters list according to <span class="target" id="index-9"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2231.html"><strong>RFC 2231</strong></a>. <em>params</em> is a sequence of
  362. 2-tuples containing elements of the form <code class="docutils literal notranslate"><span class="pre">(content-type,</span> <span class="pre">string-value)</span></code>.</p>
  363. </dd></dl>
  364. <p class="rubric">Footnotes</p>
  365. <aside class="footnote-list brackets">
  366. <aside class="footnote brackets" id="id2" role="doc-footnote">
  367. <span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id1">1</a><span class="fn-bracket">]</span></span>
  368. <p>Note that the sign of the timezone offset is the opposite of the sign of the
  369. <code class="docutils literal notranslate"><span class="pre">time.timezone</span></code> variable for the same timezone; the latter variable follows
  370. the POSIX standard while this module follows <span class="target" id="index-10"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2822.html"><strong>RFC 2822</strong></a>.</p>
  371. </aside>
  372. </aside>
  373. </section>
  374. <div class="clearer"></div>
  375. </div>
  376. </div>
  377. </div>
  378. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  379. <div class="sphinxsidebarwrapper">
  380. <div>
  381. <h4>Previous topic</h4>
  382. <p class="topless"><a href="email.encoders.html"
  383. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.encoders</span></code>: Encoders</a></p>
  384. </div>
  385. <div>
  386. <h4>Next topic</h4>
  387. <p class="topless"><a href="email.iterators.html"
  388. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.iterators</span></code>: Iterators</a></p>
  389. </div>
  390. <div role="note" aria-label="source link">
  391. <h3>This Page</h3>
  392. <ul class="this-page-menu">
  393. <li><a href="../bugs.html">Report a Bug</a></li>
  394. <li>
  395. <a href="https://github.com/python/cpython/blob/main/Doc/library/email.utils.rst"
  396. rel="nofollow">Show Source
  397. </a>
  398. </li>
  399. </ul>
  400. </div>
  401. </div>
  402. <div id="sidebarbutton" title="Collapse sidebar">
  403. <span>«</span>
  404. </div>
  405. </div>
  406. <div class="clearer"></div>
  407. </div>
  408. <div class="related" role="navigation" aria-label="related navigation">
  409. <h3>Navigation</h3>
  410. <ul>
  411. <li class="right" style="margin-right: 10px">
  412. <a href="../genindex.html" title="General Index"
  413. >index</a></li>
  414. <li class="right" >
  415. <a href="../py-modindex.html" title="Python Module Index"
  416. >modules</a> |</li>
  417. <li class="right" >
  418. <a href="email.iterators.html" title="email.iterators: Iterators"
  419. >next</a> |</li>
  420. <li class="right" >
  421. <a href="email.encoders.html" title="email.encoders: Encoders"
  422. >previous</a> |</li>
  423. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  424. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  425. <li class="switchers">
  426. <div class="language_switcher_placeholder"></div>
  427. <div class="version_switcher_placeholder"></div>
  428. </li>
  429. <li>
  430. </li>
  431. <li id="cpython-language-and-version">
  432. <a href="../index.html">3.12.3 Documentation</a> &#187;
  433. </li>
  434. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  435. <li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> &#187;</li>
  436. <li class="nav-item nav-item-3"><a href="email.html" ><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code> — An email and MIME handling package</a> &#187;</li>
  437. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.utils</span></code>: Miscellaneous utilities</a></li>
  438. <li class="right">
  439. <div class="inline-search" role="search">
  440. <form class="inline-search" action="../search.html" method="get">
  441. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  442. <input type="submit" value="Go" />
  443. </form>
  444. </div>
  445. |
  446. </li>
  447. <li class="right">
  448. <label class="theme-selector-label">
  449. Theme
  450. <select class="theme-selector" oninput="activateTheme(this.value)">
  451. <option value="auto" selected>Auto</option>
  452. <option value="light">Light</option>
  453. <option value="dark">Dark</option>
  454. </select>
  455. </label> |</li>
  456. </ul>
  457. </div>
  458. <div class="footer">
  459. &copy;
  460. <a href="../copyright.html">
  461. Copyright
  462. </a>
  463. 2001-2024, Python Software Foundation.
  464. <br />
  465. This page is licensed under the Python Software Foundation License Version 2.
  466. <br />
  467. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  468. <br />
  469. See <a href="/license.html">History and License</a> for more information.<br />
  470. <br />
  471. The Python Software Foundation is a non-profit corporation.
  472. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  473. <br />
  474. <br />
  475. Last updated on Apr 09, 2024 (13:47 UTC).
  476. <a href="/bugs.html">Found a bug</a>?
  477. <br />
  478. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  479. </div>
  480. </body>
  481. </html>
上海开阖软件有限公司 沪ICP备12045867号-1