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.

755 lines
54KB

  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="uuid — UUID objects according to RFC 4122" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/uuid.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/uuid.py This module provides immutable UUID objects (the UUID class) and the functions uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5 UUIDs as specified i..." />
  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/uuid.py This module provides immutable UUID objects (the UUID class) and the functions uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5 UUIDs as specified i..." />
  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>uuid — UUID objects according to RFC 4122 &#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="socketserver — A framework for network servers" href="socketserver.html" />
  33. <link rel="prev" title="smtplib — SMTP protocol client" href="smtplib.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/uuid.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">uuid</span></code> — UUID objects according to <strong>RFC 4122</strong></a><ul>
  86. <li><a class="reference internal" href="#command-line-usage">Command-Line Usage</a></li>
  87. <li><a class="reference internal" href="#example">Example</a></li>
  88. <li><a class="reference internal" href="#command-line-example">Command-Line Example</a></li>
  89. </ul>
  90. </li>
  91. </ul>
  92. </div>
  93. <div>
  94. <h4>Previous topic</h4>
  95. <p class="topless"><a href="smtplib.html"
  96. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">smtplib</span></code> — SMTP protocol client</a></p>
  97. </div>
  98. <div>
  99. <h4>Next topic</h4>
  100. <p class="topless"><a href="socketserver.html"
  101. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">socketserver</span></code> — A framework for network servers</a></p>
  102. </div>
  103. <div role="note" aria-label="source link">
  104. <h3>This Page</h3>
  105. <ul class="this-page-menu">
  106. <li><a href="../bugs.html">Report a Bug</a></li>
  107. <li>
  108. <a href="https://github.com/python/cpython/blob/main/Doc/library/uuid.rst"
  109. rel="nofollow">Show Source
  110. </a>
  111. </li>
  112. </ul>
  113. </div>
  114. </nav>
  115. </div>
  116. </div>
  117. <div class="related" role="navigation" aria-label="related navigation">
  118. <h3>Navigation</h3>
  119. <ul>
  120. <li class="right" style="margin-right: 10px">
  121. <a href="../genindex.html" title="General Index"
  122. accesskey="I">index</a></li>
  123. <li class="right" >
  124. <a href="../py-modindex.html" title="Python Module Index"
  125. >modules</a> |</li>
  126. <li class="right" >
  127. <a href="socketserver.html" title="socketserver — A framework for network servers"
  128. accesskey="N">next</a> |</li>
  129. <li class="right" >
  130. <a href="smtplib.html" title="smtplib — SMTP protocol client"
  131. accesskey="P">previous</a> |</li>
  132. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  133. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  134. <li class="switchers">
  135. <div class="language_switcher_placeholder"></div>
  136. <div class="version_switcher_placeholder"></div>
  137. </li>
  138. <li>
  139. </li>
  140. <li id="cpython-language-and-version">
  141. <a href="../index.html">3.12.3 Documentation</a> &#187;
  142. </li>
  143. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  144. <li class="nav-item nav-item-2"><a href="internet.html" accesskey="U">Internet Protocols and Support</a> &#187;</li>
  145. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">uuid</span></code> — UUID objects according to <strong>RFC 4122</strong></a></li>
  146. <li class="right">
  147. <div class="inline-search" role="search">
  148. <form class="inline-search" action="../search.html" method="get">
  149. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  150. <input type="submit" value="Go" />
  151. </form>
  152. </div>
  153. |
  154. </li>
  155. <li class="right">
  156. <label class="theme-selector-label">
  157. Theme
  158. <select class="theme-selector" oninput="activateTheme(this.value)">
  159. <option value="auto" selected>Auto</option>
  160. <option value="light">Light</option>
  161. <option value="dark">Dark</option>
  162. </select>
  163. </label> |</li>
  164. </ul>
  165. </div>
  166. <div class="document">
  167. <div class="documentwrapper">
  168. <div class="bodywrapper">
  169. <div class="body" role="main">
  170. <section id="module-uuid">
  171. <span id="uuid-uuid-objects-according-to-rfc-4122"></span><h1><a class="reference internal" href="#module-uuid" title="uuid: UUID objects (universally unique identifiers) according to RFC 4122"><code class="xref py py-mod docutils literal notranslate"><span class="pre">uuid</span></code></a> — UUID objects according to <span class="target" id="index-0"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4122.html"><strong>RFC 4122</strong></a><a class="headerlink" href="#module-uuid" title="Link to this heading">¶</a></h1>
  172. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/uuid.py">Lib/uuid.py</a></p>
  173. <hr class="docutils" />
  174. <p>This module provides immutable <a class="reference internal" href="#uuid.UUID" title="uuid.UUID"><code class="xref py py-class docutils literal notranslate"><span class="pre">UUID</span></code></a> objects (the <a class="reference internal" href="#uuid.UUID" title="uuid.UUID"><code class="xref py py-class docutils literal notranslate"><span class="pre">UUID</span></code></a> class)
  175. and the functions <a class="reference internal" href="#uuid.uuid1" title="uuid.uuid1"><code class="xref py py-func docutils literal notranslate"><span class="pre">uuid1()</span></code></a>, <a class="reference internal" href="#uuid.uuid3" title="uuid.uuid3"><code class="xref py py-func docutils literal notranslate"><span class="pre">uuid3()</span></code></a>, <a class="reference internal" href="#uuid.uuid4" title="uuid.uuid4"><code class="xref py py-func docutils literal notranslate"><span class="pre">uuid4()</span></code></a>, <a class="reference internal" href="#uuid.uuid5" title="uuid.uuid5"><code class="xref py py-func docutils literal notranslate"><span class="pre">uuid5()</span></code></a> for
  176. generating version 1, 3, 4, and 5 UUIDs as specified in <span class="target" id="index-1"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4122.html"><strong>RFC 4122</strong></a>.</p>
  177. <p>If all you want is a unique ID, you should probably call <a class="reference internal" href="#uuid.uuid1" title="uuid.uuid1"><code class="xref py py-func docutils literal notranslate"><span class="pre">uuid1()</span></code></a> or
  178. <a class="reference internal" href="#uuid.uuid4" title="uuid.uuid4"><code class="xref py py-func docutils literal notranslate"><span class="pre">uuid4()</span></code></a>. Note that <a class="reference internal" href="#uuid.uuid1" title="uuid.uuid1"><code class="xref py py-func docutils literal notranslate"><span class="pre">uuid1()</span></code></a> may compromise privacy since it creates
  179. a UUID containing the computer’s network address. <a class="reference internal" href="#uuid.uuid4" title="uuid.uuid4"><code class="xref py py-func docutils literal notranslate"><span class="pre">uuid4()</span></code></a> creates a
  180. random UUID.</p>
  181. <p>Depending on support from the underlying platform, <a class="reference internal" href="#uuid.uuid1" title="uuid.uuid1"><code class="xref py py-func docutils literal notranslate"><span class="pre">uuid1()</span></code></a> may or may
  182. not return a “safe” UUID. A safe UUID is one which is generated using
  183. synchronization methods that ensure no two processes can obtain the same
  184. UUID. All instances of <a class="reference internal" href="#uuid.UUID" title="uuid.UUID"><code class="xref py py-class docutils literal notranslate"><span class="pre">UUID</span></code></a> have an <a class="reference internal" href="#uuid.UUID.is_safe" title="uuid.UUID.is_safe"><code class="xref py py-attr docutils literal notranslate"><span class="pre">is_safe</span></code></a> attribute
  185. which relays any information about the UUID’s safety, using this enumeration:</p>
  186. <dl class="py class">
  187. <dt class="sig sig-object py" id="uuid.SafeUUID">
  188. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">uuid.</span></span><span class="sig-name descname"><span class="pre">SafeUUID</span></span><a class="headerlink" href="#uuid.SafeUUID" title="Link to this definition">¶</a></dt>
  189. <dd><div class="versionadded">
  190. <p><span class="versionmodified added">New in version 3.7.</span></p>
  191. </div>
  192. <dl class="py attribute">
  193. <dt class="sig sig-object py" id="uuid.SafeUUID.safe">
  194. <span class="sig-name descname"><span class="pre">safe</span></span><a class="headerlink" href="#uuid.SafeUUID.safe" title="Link to this definition">¶</a></dt>
  195. <dd><p>The UUID was generated by the platform in a multiprocessing-safe way.</p>
  196. </dd></dl>
  197. <dl class="py attribute">
  198. <dt class="sig sig-object py" id="uuid.SafeUUID.unsafe">
  199. <span class="sig-name descname"><span class="pre">unsafe</span></span><a class="headerlink" href="#uuid.SafeUUID.unsafe" title="Link to this definition">¶</a></dt>
  200. <dd><p>The UUID was not generated in a multiprocessing-safe way.</p>
  201. </dd></dl>
  202. <dl class="py attribute">
  203. <dt class="sig sig-object py" id="uuid.SafeUUID.unknown">
  204. <span class="sig-name descname"><span class="pre">unknown</span></span><a class="headerlink" href="#uuid.SafeUUID.unknown" title="Link to this definition">¶</a></dt>
  205. <dd><p>The platform does not provide information on whether the UUID was
  206. generated safely or not.</p>
  207. </dd></dl>
  208. </dd></dl>
  209. <dl class="py class">
  210. <dt class="sig sig-object py" id="uuid.UUID">
  211. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">uuid.</span></span><span class="sig-name descname"><span class="pre">UUID</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">hex</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">bytes</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">bytes_le</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">fields</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">int</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">version</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="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">is_safe</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">SafeUUID.unknown</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#uuid.UUID" title="Link to this definition">¶</a></dt>
  212. <dd><p>Create a UUID from either a string of 32 hexadecimal digits, a string of 16
  213. bytes in big-endian order as the <em>bytes</em> argument, a string of 16 bytes in
  214. little-endian order as the <em>bytes_le</em> argument, a tuple of six integers
  215. (32-bit <em>time_low</em>, 16-bit <em>time_mid</em>, 16-bit <em>time_hi_version</em>,
  216. 8-bit <em>clock_seq_hi_variant</em>, 8-bit <em>clock_seq_low</em>, 48-bit <em>node</em>) as the
  217. <em>fields</em> argument, or a single 128-bit integer as the <em>int</em> argument.
  218. When a string of hex digits is given, curly braces, hyphens,
  219. and a URN prefix are all optional. For example, these
  220. expressions all yield the same UUID:</p>
  221. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">UUID</span><span class="p">(</span><span class="s1">&#39;{12345678-1234-5678-1234-567812345678}&#39;</span><span class="p">)</span>
  222. <span class="n">UUID</span><span class="p">(</span><span class="s1">&#39;12345678123456781234567812345678&#39;</span><span class="p">)</span>
  223. <span class="n">UUID</span><span class="p">(</span><span class="s1">&#39;urn:uuid:12345678-1234-5678-1234-567812345678&#39;</span><span class="p">)</span>
  224. <span class="n">UUID</span><span class="p">(</span><span class="nb">bytes</span><span class="o">=</span><span class="sa">b</span><span class="s1">&#39;</span><span class="se">\x12\x34\x56\x78</span><span class="s1">&#39;</span><span class="o">*</span><span class="mi">4</span><span class="p">)</span>
  225. <span class="n">UUID</span><span class="p">(</span><span class="n">bytes_le</span><span class="o">=</span><span class="sa">b</span><span class="s1">&#39;</span><span class="se">\x78\x56\x34\x12\x34\x12\x78\x56</span><span class="s1">&#39;</span> <span class="o">+</span>
  226. <span class="sa">b</span><span class="s1">&#39;</span><span class="se">\x12\x34\x56\x78\x12\x34\x56\x78</span><span class="s1">&#39;</span><span class="p">)</span>
  227. <span class="n">UUID</span><span class="p">(</span><span class="n">fields</span><span class="o">=</span><span class="p">(</span><span class="mh">0x12345678</span><span class="p">,</span> <span class="mh">0x1234</span><span class="p">,</span> <span class="mh">0x5678</span><span class="p">,</span> <span class="mh">0x12</span><span class="p">,</span> <span class="mh">0x34</span><span class="p">,</span> <span class="mh">0x567812345678</span><span class="p">))</span>
  228. <span class="n">UUID</span><span class="p">(</span><span class="nb">int</span><span class="o">=</span><span class="mh">0x12345678123456781234567812345678</span><span class="p">)</span>
  229. </pre></div>
  230. </div>
  231. <p>Exactly one of <em>hex</em>, <em>bytes</em>, <em>bytes_le</em>, <em>fields</em>, or <em>int</em> must be given.
  232. The <em>version</em> argument is optional; if given, the resulting UUID will have its
  233. variant and version number set according to <span class="target" id="index-2"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4122.html"><strong>RFC 4122</strong></a>, overriding bits in the
  234. given <em>hex</em>, <em>bytes</em>, <em>bytes_le</em>, <em>fields</em>, or <em>int</em>.</p>
  235. <p>Comparison of UUID objects are made by way of comparing their
  236. <a class="reference internal" href="#uuid.UUID.int" title="uuid.UUID.int"><code class="xref py py-attr docutils literal notranslate"><span class="pre">UUID.int</span></code></a> attributes. Comparison with a non-UUID object
  237. raises a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>.</p>
  238. <p><code class="docutils literal notranslate"><span class="pre">str(uuid)</span></code> returns a string in the form
  239. <code class="docutils literal notranslate"><span class="pre">12345678-1234-5678-1234-567812345678</span></code> where the 32 hexadecimal digits
  240. represent the UUID.</p>
  241. </dd></dl>
  242. <p><a class="reference internal" href="#uuid.UUID" title="uuid.UUID"><code class="xref py py-class docutils literal notranslate"><span class="pre">UUID</span></code></a> instances have these read-only attributes:</p>
  243. <dl class="py attribute">
  244. <dt class="sig sig-object py" id="uuid.UUID.bytes">
  245. <span class="sig-prename descclassname"><span class="pre">UUID.</span></span><span class="sig-name descname"><span class="pre">bytes</span></span><a class="headerlink" href="#uuid.UUID.bytes" title="Link to this definition">¶</a></dt>
  246. <dd><p>The UUID as a 16-byte string (containing the six integer fields in big-endian
  247. byte order).</p>
  248. </dd></dl>
  249. <dl class="py attribute">
  250. <dt class="sig sig-object py" id="uuid.UUID.bytes_le">
  251. <span class="sig-prename descclassname"><span class="pre">UUID.</span></span><span class="sig-name descname"><span class="pre">bytes_le</span></span><a class="headerlink" href="#uuid.UUID.bytes_le" title="Link to this definition">¶</a></dt>
  252. <dd><p>The UUID as a 16-byte string (with <em>time_low</em>, <em>time_mid</em>, and <em>time_hi_version</em>
  253. in little-endian byte order).</p>
  254. </dd></dl>
  255. <dl class="py attribute">
  256. <dt class="sig sig-object py" id="uuid.UUID.fields">
  257. <span class="sig-prename descclassname"><span class="pre">UUID.</span></span><span class="sig-name descname"><span class="pre">fields</span></span><a class="headerlink" href="#uuid.UUID.fields" title="Link to this definition">¶</a></dt>
  258. <dd><p>A tuple of the six integer fields of the UUID, which are also available as six
  259. individual attributes and two derived attributes:</p>
  260. </dd></dl>
  261. <table class="docutils align-default">
  262. <tbody>
  263. <tr class="row-odd"><td><p>Field</p></td>
  264. <td><p>Meaning</p></td>
  265. </tr>
  266. <tr class="row-even"><td><dl class="py attribute">
  267. <dt class="sig sig-object py" id="uuid.UUID.time_low">
  268. <span class="sig-prename descclassname"><span class="pre">UUID.</span></span><span class="sig-name descname"><span class="pre">time_low</span></span><a class="headerlink" href="#uuid.UUID.time_low" title="Link to this definition">¶</a></dt>
  269. <dd></dd></dl>
  270. </td>
  271. <td><p>The first 32 bits of the UUID.</p></td>
  272. </tr>
  273. <tr class="row-odd"><td><dl class="py attribute">
  274. <dt class="sig sig-object py" id="uuid.UUID.time_mid">
  275. <span class="sig-prename descclassname"><span class="pre">UUID.</span></span><span class="sig-name descname"><span class="pre">time_mid</span></span><a class="headerlink" href="#uuid.UUID.time_mid" title="Link to this definition">¶</a></dt>
  276. <dd></dd></dl>
  277. </td>
  278. <td><p>The next 16 bits of the UUID.</p></td>
  279. </tr>
  280. <tr class="row-even"><td><dl class="py attribute">
  281. <dt class="sig sig-object py" id="uuid.UUID.time_hi_version">
  282. <span class="sig-prename descclassname"><span class="pre">UUID.</span></span><span class="sig-name descname"><span class="pre">time_hi_version</span></span><a class="headerlink" href="#uuid.UUID.time_hi_version" title="Link to this definition">¶</a></dt>
  283. <dd></dd></dl>
  284. </td>
  285. <td><p>The next 16 bits of the UUID.</p></td>
  286. </tr>
  287. <tr class="row-odd"><td><dl class="py attribute">
  288. <dt class="sig sig-object py" id="uuid.UUID.clock_seq_hi_variant">
  289. <span class="sig-prename descclassname"><span class="pre">UUID.</span></span><span class="sig-name descname"><span class="pre">clock_seq_hi_variant</span></span><a class="headerlink" href="#uuid.UUID.clock_seq_hi_variant" title="Link to this definition">¶</a></dt>
  290. <dd></dd></dl>
  291. </td>
  292. <td><p>The next 8 bits of the UUID.</p></td>
  293. </tr>
  294. <tr class="row-even"><td><dl class="py attribute">
  295. <dt class="sig sig-object py" id="uuid.UUID.clock_seq_low">
  296. <span class="sig-prename descclassname"><span class="pre">UUID.</span></span><span class="sig-name descname"><span class="pre">clock_seq_low</span></span><a class="headerlink" href="#uuid.UUID.clock_seq_low" title="Link to this definition">¶</a></dt>
  297. <dd></dd></dl>
  298. </td>
  299. <td><p>The next 8 bits of the UUID.</p></td>
  300. </tr>
  301. <tr class="row-odd"><td><dl class="py attribute">
  302. <dt class="sig sig-object py" id="uuid.UUID.node">
  303. <span class="sig-prename descclassname"><span class="pre">UUID.</span></span><span class="sig-name descname"><span class="pre">node</span></span><a class="headerlink" href="#uuid.UUID.node" title="Link to this definition">¶</a></dt>
  304. <dd></dd></dl>
  305. </td>
  306. <td><p>The last 48 bits of the UUID.</p></td>
  307. </tr>
  308. <tr class="row-even"><td><dl class="py attribute">
  309. <dt class="sig sig-object py" id="uuid.UUID.time">
  310. <span class="sig-prename descclassname"><span class="pre">UUID.</span></span><span class="sig-name descname"><span class="pre">time</span></span><a class="headerlink" href="#uuid.UUID.time" title="Link to this definition">¶</a></dt>
  311. <dd></dd></dl>
  312. </td>
  313. <td><p>The 60-bit timestamp.</p></td>
  314. </tr>
  315. <tr class="row-odd"><td><dl class="py attribute">
  316. <dt class="sig sig-object py" id="uuid.UUID.clock_seq">
  317. <span class="sig-prename descclassname"><span class="pre">UUID.</span></span><span class="sig-name descname"><span class="pre">clock_seq</span></span><a class="headerlink" href="#uuid.UUID.clock_seq" title="Link to this definition">¶</a></dt>
  318. <dd></dd></dl>
  319. </td>
  320. <td><p>The 14-bit sequence number.</p></td>
  321. </tr>
  322. </tbody>
  323. </table>
  324. <dl class="py attribute">
  325. <dt class="sig sig-object py" id="uuid.UUID.hex">
  326. <span class="sig-prename descclassname"><span class="pre">UUID.</span></span><span class="sig-name descname"><span class="pre">hex</span></span><a class="headerlink" href="#uuid.UUID.hex" title="Link to this definition">¶</a></dt>
  327. <dd><p>The UUID as a 32-character lowercase hexadecimal string.</p>
  328. </dd></dl>
  329. <dl class="py attribute">
  330. <dt class="sig sig-object py" id="uuid.UUID.int">
  331. <span class="sig-prename descclassname"><span class="pre">UUID.</span></span><span class="sig-name descname"><span class="pre">int</span></span><a class="headerlink" href="#uuid.UUID.int" title="Link to this definition">¶</a></dt>
  332. <dd><p>The UUID as a 128-bit integer.</p>
  333. </dd></dl>
  334. <dl class="py attribute">
  335. <dt class="sig sig-object py" id="uuid.UUID.urn">
  336. <span class="sig-prename descclassname"><span class="pre">UUID.</span></span><span class="sig-name descname"><span class="pre">urn</span></span><a class="headerlink" href="#uuid.UUID.urn" title="Link to this definition">¶</a></dt>
  337. <dd><p>The UUID as a URN as specified in <span class="target" id="index-3"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4122.html"><strong>RFC 4122</strong></a>.</p>
  338. </dd></dl>
  339. <dl class="py attribute">
  340. <dt class="sig sig-object py" id="uuid.UUID.variant">
  341. <span class="sig-prename descclassname"><span class="pre">UUID.</span></span><span class="sig-name descname"><span class="pre">variant</span></span><a class="headerlink" href="#uuid.UUID.variant" title="Link to this definition">¶</a></dt>
  342. <dd><p>The UUID variant, which determines the internal layout of the UUID. This will be
  343. one of the constants <a class="reference internal" href="#uuid.RESERVED_NCS" title="uuid.RESERVED_NCS"><code class="xref py py-const docutils literal notranslate"><span class="pre">RESERVED_NCS</span></code></a>, <a class="reference internal" href="#uuid.RFC_4122" title="uuid.RFC_4122"><code class="xref py py-const docutils literal notranslate"><span class="pre">RFC_4122</span></code></a>,
  344. <a class="reference internal" href="#uuid.RESERVED_MICROSOFT" title="uuid.RESERVED_MICROSOFT"><code class="xref py py-const docutils literal notranslate"><span class="pre">RESERVED_MICROSOFT</span></code></a>, or <a class="reference internal" href="#uuid.RESERVED_FUTURE" title="uuid.RESERVED_FUTURE"><code class="xref py py-const docutils literal notranslate"><span class="pre">RESERVED_FUTURE</span></code></a>.</p>
  345. </dd></dl>
  346. <dl class="py attribute">
  347. <dt class="sig sig-object py" id="uuid.UUID.version">
  348. <span class="sig-prename descclassname"><span class="pre">UUID.</span></span><span class="sig-name descname"><span class="pre">version</span></span><a class="headerlink" href="#uuid.UUID.version" title="Link to this definition">¶</a></dt>
  349. <dd><p>The UUID version number (1 through 5, meaningful only when the variant is
  350. <a class="reference internal" href="#uuid.RFC_4122" title="uuid.RFC_4122"><code class="xref py py-const docutils literal notranslate"><span class="pre">RFC_4122</span></code></a>).</p>
  351. </dd></dl>
  352. <dl class="py attribute">
  353. <dt class="sig sig-object py" id="uuid.UUID.is_safe">
  354. <span class="sig-prename descclassname"><span class="pre">UUID.</span></span><span class="sig-name descname"><span class="pre">is_safe</span></span><a class="headerlink" href="#uuid.UUID.is_safe" title="Link to this definition">¶</a></dt>
  355. <dd><p>An enumeration of <a class="reference internal" href="#uuid.SafeUUID" title="uuid.SafeUUID"><code class="xref py py-class docutils literal notranslate"><span class="pre">SafeUUID</span></code></a> which indicates whether the platform
  356. generated the UUID in a multiprocessing-safe way.</p>
  357. <div class="versionadded">
  358. <p><span class="versionmodified added">New in version 3.7.</span></p>
  359. </div>
  360. </dd></dl>
  361. <p>The <a class="reference internal" href="#module-uuid" title="uuid: UUID objects (universally unique identifiers) according to RFC 4122"><code class="xref py py-mod docutils literal notranslate"><span class="pre">uuid</span></code></a> module defines the following functions:</p>
  362. <dl class="py function">
  363. <dt class="sig sig-object py" id="uuid.getnode">
  364. <span class="sig-prename descclassname"><span class="pre">uuid.</span></span><span class="sig-name descname"><span class="pre">getnode</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#uuid.getnode" title="Link to this definition">¶</a></dt>
  365. <dd><p>Get the hardware address as a 48-bit positive integer. The first time this
  366. runs, it may launch a separate program, which could be quite slow. If all
  367. attempts to obtain the hardware address fail, we choose a random 48-bit
  368. number with the multicast bit (least significant bit of the first octet)
  369. set to 1 as recommended in <span class="target" id="index-4"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4122.html"><strong>RFC 4122</strong></a>. “Hardware address” means the MAC
  370. address of a network interface. On a machine with multiple network
  371. interfaces, universally administered MAC addresses (i.e. where the second
  372. least significant bit of the first octet is <em>unset</em>) will be preferred over
  373. locally administered MAC addresses, but with no other ordering guarantees.</p>
  374. <div class="versionchanged">
  375. <p><span class="versionmodified changed">Changed in version 3.7: </span>Universally administered MAC addresses are preferred over locally
  376. administered MAC addresses, since the former are guaranteed to be
  377. globally unique, while the latter are not.</p>
  378. </div>
  379. </dd></dl>
  380. <dl class="py function" id="index-5">
  381. <dt class="sig sig-object py" id="uuid.uuid1">
  382. <span class="sig-prename descclassname"><span class="pre">uuid.</span></span><span class="sig-name descname"><span class="pre">uuid1</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">node</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">clock_seq</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="#uuid.uuid1" title="Link to this definition">¶</a></dt>
  383. <dd><p>Generate a UUID from a host ID, sequence number, and the current time. If <em>node</em>
  384. is not given, <a class="reference internal" href="#uuid.getnode" title="uuid.getnode"><code class="xref py py-func docutils literal notranslate"><span class="pre">getnode()</span></code></a> is used to obtain the hardware address. If
  385. <em>clock_seq</em> is given, it is used as the sequence number; otherwise a random
  386. 14-bit sequence number is chosen.</p>
  387. </dd></dl>
  388. <dl class="py function" id="index-6">
  389. <dt class="sig sig-object py" id="uuid.uuid3">
  390. <span class="sig-prename descclassname"><span class="pre">uuid.</span></span><span class="sig-name descname"><span class="pre">uuid3</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">namespace</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#uuid.uuid3" title="Link to this definition">¶</a></dt>
  391. <dd><p>Generate a UUID based on the MD5 hash of a namespace identifier (which is a
  392. UUID) and a name (which is a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object or a string
  393. that will be encoded using UTF-8).</p>
  394. </dd></dl>
  395. <dl class="py function" id="index-7">
  396. <dt class="sig sig-object py" id="uuid.uuid4">
  397. <span class="sig-prename descclassname"><span class="pre">uuid.</span></span><span class="sig-name descname"><span class="pre">uuid4</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#uuid.uuid4" title="Link to this definition">¶</a></dt>
  398. <dd><p>Generate a random UUID.</p>
  399. </dd></dl>
  400. <dl class="py function" id="index-8">
  401. <dt class="sig sig-object py" id="uuid.uuid5">
  402. <span class="sig-prename descclassname"><span class="pre">uuid.</span></span><span class="sig-name descname"><span class="pre">uuid5</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">namespace</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#uuid.uuid5" title="Link to this definition">¶</a></dt>
  403. <dd><p>Generate a UUID based on the SHA-1 hash of a namespace identifier (which is a
  404. UUID) and a name (which is a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object or a string
  405. that will be encoded using UTF-8).</p>
  406. </dd></dl>
  407. <p id="index-9">The <a class="reference internal" href="#module-uuid" title="uuid: UUID objects (universally unique identifiers) according to RFC 4122"><code class="xref py py-mod docutils literal notranslate"><span class="pre">uuid</span></code></a> module defines the following namespace identifiers for use with
  408. <a class="reference internal" href="#uuid.uuid3" title="uuid.uuid3"><code class="xref py py-func docutils literal notranslate"><span class="pre">uuid3()</span></code></a> or <a class="reference internal" href="#uuid.uuid5" title="uuid.uuid5"><code class="xref py py-func docutils literal notranslate"><span class="pre">uuid5()</span></code></a>.</p>
  409. <dl class="py data">
  410. <dt class="sig sig-object py" id="uuid.NAMESPACE_DNS">
  411. <span class="sig-prename descclassname"><span class="pre">uuid.</span></span><span class="sig-name descname"><span class="pre">NAMESPACE_DNS</span></span><a class="headerlink" href="#uuid.NAMESPACE_DNS" title="Link to this definition">¶</a></dt>
  412. <dd><p>When this namespace is specified, the <em>name</em> string is a fully qualified domain
  413. name.</p>
  414. </dd></dl>
  415. <dl class="py data">
  416. <dt class="sig sig-object py" id="uuid.NAMESPACE_URL">
  417. <span class="sig-prename descclassname"><span class="pre">uuid.</span></span><span class="sig-name descname"><span class="pre">NAMESPACE_URL</span></span><a class="headerlink" href="#uuid.NAMESPACE_URL" title="Link to this definition">¶</a></dt>
  418. <dd><p>When this namespace is specified, the <em>name</em> string is a URL.</p>
  419. </dd></dl>
  420. <dl class="py data">
  421. <dt class="sig sig-object py" id="uuid.NAMESPACE_OID">
  422. <span class="sig-prename descclassname"><span class="pre">uuid.</span></span><span class="sig-name descname"><span class="pre">NAMESPACE_OID</span></span><a class="headerlink" href="#uuid.NAMESPACE_OID" title="Link to this definition">¶</a></dt>
  423. <dd><p>When this namespace is specified, the <em>name</em> string is an ISO OID.</p>
  424. </dd></dl>
  425. <dl class="py data">
  426. <dt class="sig sig-object py" id="uuid.NAMESPACE_X500">
  427. <span class="sig-prename descclassname"><span class="pre">uuid.</span></span><span class="sig-name descname"><span class="pre">NAMESPACE_X500</span></span><a class="headerlink" href="#uuid.NAMESPACE_X500" title="Link to this definition">¶</a></dt>
  428. <dd><p>When this namespace is specified, the <em>name</em> string is an X.500 DN in DER or a
  429. text output format.</p>
  430. </dd></dl>
  431. <p>The <a class="reference internal" href="#module-uuid" title="uuid: UUID objects (universally unique identifiers) according to RFC 4122"><code class="xref py py-mod docutils literal notranslate"><span class="pre">uuid</span></code></a> module defines the following constants for the possible values
  432. of the <a class="reference internal" href="#uuid.UUID.variant" title="uuid.UUID.variant"><code class="xref py py-attr docutils literal notranslate"><span class="pre">variant</span></code></a> attribute:</p>
  433. <dl class="py data">
  434. <dt class="sig sig-object py" id="uuid.RESERVED_NCS">
  435. <span class="sig-prename descclassname"><span class="pre">uuid.</span></span><span class="sig-name descname"><span class="pre">RESERVED_NCS</span></span><a class="headerlink" href="#uuid.RESERVED_NCS" title="Link to this definition">¶</a></dt>
  436. <dd><p>Reserved for NCS compatibility.</p>
  437. </dd></dl>
  438. <dl class="py data">
  439. <dt class="sig sig-object py" id="uuid.RFC_4122">
  440. <span class="sig-prename descclassname"><span class="pre">uuid.</span></span><span class="sig-name descname"><span class="pre">RFC_4122</span></span><a class="headerlink" href="#uuid.RFC_4122" title="Link to this definition">¶</a></dt>
  441. <dd><p>Specifies the UUID layout given in <span class="target" id="index-10"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4122.html"><strong>RFC 4122</strong></a>.</p>
  442. </dd></dl>
  443. <dl class="py data">
  444. <dt class="sig sig-object py" id="uuid.RESERVED_MICROSOFT">
  445. <span class="sig-prename descclassname"><span class="pre">uuid.</span></span><span class="sig-name descname"><span class="pre">RESERVED_MICROSOFT</span></span><a class="headerlink" href="#uuid.RESERVED_MICROSOFT" title="Link to this definition">¶</a></dt>
  446. <dd><p>Reserved for Microsoft compatibility.</p>
  447. </dd></dl>
  448. <dl class="py data">
  449. <dt class="sig sig-object py" id="uuid.RESERVED_FUTURE">
  450. <span class="sig-prename descclassname"><span class="pre">uuid.</span></span><span class="sig-name descname"><span class="pre">RESERVED_FUTURE</span></span><a class="headerlink" href="#uuid.RESERVED_FUTURE" title="Link to this definition">¶</a></dt>
  451. <dd><p>Reserved for future definition.</p>
  452. </dd></dl>
  453. <div class="admonition seealso">
  454. <p class="admonition-title">See also</p>
  455. <dl class="simple">
  456. <dt><span class="target" id="index-11"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4122.html"><strong>RFC 4122</strong></a> - A Universally Unique IDentifier (UUID) URN Namespace</dt><dd><p>This specification defines a Uniform Resource Name namespace for UUIDs, the
  457. internal format of UUIDs, and methods of generating UUIDs.</p>
  458. </dd>
  459. </dl>
  460. </div>
  461. <section id="command-line-usage">
  462. <span id="uuid-cli"></span><h2>Command-Line Usage<a class="headerlink" href="#command-line-usage" title="Link to this heading">¶</a></h2>
  463. <div class="versionadded">
  464. <p><span class="versionmodified added">New in version 3.12.</span></p>
  465. </div>
  466. <p>The <a class="reference internal" href="#module-uuid" title="uuid: UUID objects (universally unique identifiers) according to RFC 4122"><code class="xref py py-mod docutils literal notranslate"><span class="pre">uuid</span></code></a> module can be executed as a script from the command line.</p>
  467. <div class="highlight-sh notranslate"><div class="highlight"><pre><span></span>python<span class="w"> </span>-m<span class="w"> </span>uuid<span class="w"> </span><span class="o">[</span>-h<span class="o">]</span><span class="w"> </span><span class="o">[</span>-u<span class="w"> </span><span class="o">{</span>uuid1,uuid3,uuid4,uuid5<span class="o">}]</span><span class="w"> </span><span class="o">[</span>-n<span class="w"> </span>NAMESPACE<span class="o">]</span><span class="w"> </span><span class="o">[</span>-N<span class="w"> </span>NAME<span class="o">]</span>
  468. </pre></div>
  469. </div>
  470. <p>The following options are accepted:</p>
  471. <dl class="std option">
  472. <dt class="sig sig-object std" id="cmdoption-uuid-h">
  473. <span id="cmdoption-uuid-help"></span><span class="sig-name descname"><span class="pre">-h</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--help</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-uuid-h" title="Link to this definition">¶</a></dt>
  474. <dd><p>Show the help message and exit.</p>
  475. </dd></dl>
  476. <dl class="std option">
  477. <dt class="sig sig-object std" id="cmdoption-uuid-u">
  478. <span class="sig-name descname"><span class="pre">-u</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;uuid&gt;</span></span><a class="headerlink" href="#cmdoption-uuid-u" title="Link to this definition">¶</a></dt>
  479. <dt class="sig sig-object std" id="cmdoption-uuid-uuid">
  480. <span class="sig-name descname"><span class="pre">--uuid</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;uuid&gt;</span></span><a class="headerlink" href="#cmdoption-uuid-uuid" title="Link to this definition">¶</a></dt>
  481. <dd><p>Specify the function name to use to generate the uuid. By default <a class="reference internal" href="#uuid.uuid4" title="uuid.uuid4"><code class="xref py py-func docutils literal notranslate"><span class="pre">uuid4()</span></code></a>
  482. is used.</p>
  483. </dd></dl>
  484. <dl class="std option">
  485. <dt class="sig sig-object std" id="cmdoption-uuid-n">
  486. <span class="sig-name descname"><span class="pre">-n</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;namespace&gt;</span></span><a class="headerlink" href="#cmdoption-uuid-n" title="Link to this definition">¶</a></dt>
  487. <dt class="sig sig-object std" id="cmdoption-uuid-namespace">
  488. <span class="sig-name descname"><span class="pre">--namespace</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;namespace&gt;</span></span><a class="headerlink" href="#cmdoption-uuid-namespace" title="Link to this definition">¶</a></dt>
  489. <dd><p>The namespace is a <code class="docutils literal notranslate"><span class="pre">UUID</span></code>, or <code class="docutils literal notranslate"><span class="pre">&#64;ns</span></code> where <code class="docutils literal notranslate"><span class="pre">ns</span></code> is a well-known predefined UUID
  490. addressed by namespace name. Such as <code class="docutils literal notranslate"><span class="pre">&#64;dns</span></code>, <code class="docutils literal notranslate"><span class="pre">&#64;url</span></code>, <code class="docutils literal notranslate"><span class="pre">&#64;oid</span></code>, and <code class="docutils literal notranslate"><span class="pre">&#64;x500</span></code>.
  491. Only required for <a class="reference internal" href="#uuid.uuid3" title="uuid.uuid3"><code class="xref py py-func docutils literal notranslate"><span class="pre">uuid3()</span></code></a> / <a class="reference internal" href="#uuid.uuid5" title="uuid.uuid5"><code class="xref py py-func docutils literal notranslate"><span class="pre">uuid5()</span></code></a> functions.</p>
  492. </dd></dl>
  493. <dl class="std option">
  494. <dt class="sig sig-object std" id="cmdoption-uuid-N">
  495. <span class="sig-name descname"><span class="pre">-N</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;name&gt;</span></span><a class="headerlink" href="#cmdoption-uuid-N" title="Link to this definition">¶</a></dt>
  496. <dt class="sig sig-object std" id="cmdoption-uuid-name">
  497. <span class="sig-name descname"><span class="pre">--name</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;name&gt;</span></span><a class="headerlink" href="#cmdoption-uuid-name" title="Link to this definition">¶</a></dt>
  498. <dd><p>The name used as part of generating the uuid. Only required for
  499. <a class="reference internal" href="#uuid.uuid3" title="uuid.uuid3"><code class="xref py py-func docutils literal notranslate"><span class="pre">uuid3()</span></code></a> / <a class="reference internal" href="#uuid.uuid5" title="uuid.uuid5"><code class="xref py py-func docutils literal notranslate"><span class="pre">uuid5()</span></code></a> functions.</p>
  500. </dd></dl>
  501. </section>
  502. <section id="example">
  503. <span id="uuid-example"></span><h2>Example<a class="headerlink" href="#example" title="Link to this heading">¶</a></h2>
  504. <p>Here are some examples of typical usage of the <a class="reference internal" href="#module-uuid" title="uuid: UUID objects (universally unique identifiers) according to RFC 4122"><code class="xref py py-mod docutils literal notranslate"><span class="pre">uuid</span></code></a> module:</p>
  505. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">uuid</span>
  506. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># make a UUID based on the host ID and current time</span>
  507. <span class="gp">&gt;&gt;&gt; </span><span class="n">uuid</span><span class="o">.</span><span class="n">uuid1</span><span class="p">()</span>
  508. <span class="go">UUID(&#39;a8098c1a-f86e-11da-bd1a-00112444be1e&#39;)</span>
  509. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># make a UUID using an MD5 hash of a namespace UUID and a name</span>
  510. <span class="gp">&gt;&gt;&gt; </span><span class="n">uuid</span><span class="o">.</span><span class="n">uuid3</span><span class="p">(</span><span class="n">uuid</span><span class="o">.</span><span class="n">NAMESPACE_DNS</span><span class="p">,</span> <span class="s1">&#39;python.org&#39;</span><span class="p">)</span>
  511. <span class="go">UUID(&#39;6fa459ea-ee8a-3ca4-894e-db77e160355e&#39;)</span>
  512. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># make a random UUID</span>
  513. <span class="gp">&gt;&gt;&gt; </span><span class="n">uuid</span><span class="o">.</span><span class="n">uuid4</span><span class="p">()</span>
  514. <span class="go">UUID(&#39;16fd2706-8baf-433b-82eb-8c7fada847da&#39;)</span>
  515. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># make a UUID using a SHA-1 hash of a namespace UUID and a name</span>
  516. <span class="gp">&gt;&gt;&gt; </span><span class="n">uuid</span><span class="o">.</span><span class="n">uuid5</span><span class="p">(</span><span class="n">uuid</span><span class="o">.</span><span class="n">NAMESPACE_DNS</span><span class="p">,</span> <span class="s1">&#39;python.org&#39;</span><span class="p">)</span>
  517. <span class="go">UUID(&#39;886313e1-3b8a-5372-9b90-0c9aee199e5d&#39;)</span>
  518. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># make a UUID from a string of hex digits (braces and hyphens ignored)</span>
  519. <span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">uuid</span><span class="o">.</span><span class="n">UUID</span><span class="p">(</span><span class="s1">&#39;{00010203-0405-0607-0809-0a0b0c0d0e0f}&#39;</span><span class="p">)</span>
  520. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># convert a UUID to a string of hex digits in standard form</span>
  521. <span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
  522. <span class="go">&#39;00010203-0405-0607-0809-0a0b0c0d0e0f&#39;</span>
  523. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># get the raw 16 bytes of the UUID</span>
  524. <span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">.</span><span class="n">bytes</span>
  525. <span class="go">b&#39;\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f&#39;</span>
  526. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># make a UUID from a 16-byte string</span>
  527. <span class="gp">&gt;&gt;&gt; </span><span class="n">uuid</span><span class="o">.</span><span class="n">UUID</span><span class="p">(</span><span class="nb">bytes</span><span class="o">=</span><span class="n">x</span><span class="o">.</span><span class="n">bytes</span><span class="p">)</span>
  528. <span class="go">UUID(&#39;00010203-0405-0607-0809-0a0b0c0d0e0f&#39;)</span>
  529. </pre></div>
  530. </div>
  531. </section>
  532. <section id="command-line-example">
  533. <span id="uuid-cli-example"></span><h2>Command-Line Example<a class="headerlink" href="#command-line-example" title="Link to this heading">¶</a></h2>
  534. <p>Here are some examples of typical usage of the <a class="reference internal" href="#module-uuid" title="uuid: UUID objects (universally unique identifiers) according to RFC 4122"><code class="xref py py-mod docutils literal notranslate"><span class="pre">uuid</span></code></a> command line interface:</p>
  535. <div class="highlight-shell notranslate"><div class="highlight"><pre><span></span><span class="c1"># generate a random uuid - by default uuid4() is used</span>
  536. $<span class="w"> </span>python<span class="w"> </span>-m<span class="w"> </span>uuid
  537. <span class="c1"># generate a uuid using uuid1()</span>
  538. $<span class="w"> </span>python<span class="w"> </span>-m<span class="w"> </span>uuid<span class="w"> </span>-u<span class="w"> </span>uuid1
  539. <span class="c1"># generate a uuid using uuid5</span>
  540. $<span class="w"> </span>python<span class="w"> </span>-m<span class="w"> </span>uuid<span class="w"> </span>-u<span class="w"> </span>uuid5<span class="w"> </span>-n<span class="w"> </span>@url<span class="w"> </span>-N<span class="w"> </span>example.com
  541. </pre></div>
  542. </div>
  543. </section>
  544. </section>
  545. <div class="clearer"></div>
  546. </div>
  547. </div>
  548. </div>
  549. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  550. <div class="sphinxsidebarwrapper">
  551. <div>
  552. <h3><a href="../contents.html">Table of Contents</a></h3>
  553. <ul>
  554. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">uuid</span></code> — UUID objects according to <strong>RFC 4122</strong></a><ul>
  555. <li><a class="reference internal" href="#command-line-usage">Command-Line Usage</a></li>
  556. <li><a class="reference internal" href="#example">Example</a></li>
  557. <li><a class="reference internal" href="#command-line-example">Command-Line Example</a></li>
  558. </ul>
  559. </li>
  560. </ul>
  561. </div>
  562. <div>
  563. <h4>Previous topic</h4>
  564. <p class="topless"><a href="smtplib.html"
  565. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">smtplib</span></code> — SMTP protocol client</a></p>
  566. </div>
  567. <div>
  568. <h4>Next topic</h4>
  569. <p class="topless"><a href="socketserver.html"
  570. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">socketserver</span></code> — A framework for network servers</a></p>
  571. </div>
  572. <div role="note" aria-label="source link">
  573. <h3>This Page</h3>
  574. <ul class="this-page-menu">
  575. <li><a href="../bugs.html">Report a Bug</a></li>
  576. <li>
  577. <a href="https://github.com/python/cpython/blob/main/Doc/library/uuid.rst"
  578. rel="nofollow">Show Source
  579. </a>
  580. </li>
  581. </ul>
  582. </div>
  583. </div>
  584. <div id="sidebarbutton" title="Collapse sidebar">
  585. <span>«</span>
  586. </div>
  587. </div>
  588. <div class="clearer"></div>
  589. </div>
  590. <div class="related" role="navigation" aria-label="related navigation">
  591. <h3>Navigation</h3>
  592. <ul>
  593. <li class="right" style="margin-right: 10px">
  594. <a href="../genindex.html" title="General Index"
  595. >index</a></li>
  596. <li class="right" >
  597. <a href="../py-modindex.html" title="Python Module Index"
  598. >modules</a> |</li>
  599. <li class="right" >
  600. <a href="socketserver.html" title="socketserver — A framework for network servers"
  601. >next</a> |</li>
  602. <li class="right" >
  603. <a href="smtplib.html" title="smtplib — SMTP protocol client"
  604. >previous</a> |</li>
  605. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  606. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  607. <li class="switchers">
  608. <div class="language_switcher_placeholder"></div>
  609. <div class="version_switcher_placeholder"></div>
  610. </li>
  611. <li>
  612. </li>
  613. <li id="cpython-language-and-version">
  614. <a href="../index.html">3.12.3 Documentation</a> &#187;
  615. </li>
  616. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  617. <li class="nav-item nav-item-2"><a href="internet.html" >Internet Protocols and Support</a> &#187;</li>
  618. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">uuid</span></code> — UUID objects according to <strong>RFC 4122</strong></a></li>
  619. <li class="right">
  620. <div class="inline-search" role="search">
  621. <form class="inline-search" action="../search.html" method="get">
  622. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  623. <input type="submit" value="Go" />
  624. </form>
  625. </div>
  626. |
  627. </li>
  628. <li class="right">
  629. <label class="theme-selector-label">
  630. Theme
  631. <select class="theme-selector" oninput="activateTheme(this.value)">
  632. <option value="auto" selected>Auto</option>
  633. <option value="light">Light</option>
  634. <option value="dark">Dark</option>
  635. </select>
  636. </label> |</li>
  637. </ul>
  638. </div>
  639. <div class="footer">
  640. &copy;
  641. <a href="../copyright.html">
  642. Copyright
  643. </a>
  644. 2001-2024, Python Software Foundation.
  645. <br />
  646. This page is licensed under the Python Software Foundation License Version 2.
  647. <br />
  648. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  649. <br />
  650. See <a href="/license.html">History and License</a> for more information.<br />
  651. <br />
  652. The Python Software Foundation is a non-profit corporation.
  653. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  654. <br />
  655. <br />
  656. Last updated on Apr 09, 2024 (13:47 UTC).
  657. <a href="/bugs.html">Found a bug</a>?
  658. <br />
  659. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  660. </div>
  661. </body>
  662. </html>
上海开阖软件有限公司 沪ICP备12045867号-1