gooderp18绿色标准版
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

1643 rindas
134KB

  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="ipaddress — IPv4/IPv6 manipulation library" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/ipaddress.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/ipaddress.py ipaddress provides the capabilities to create, manipulate and operate on IPv4 and IPv6 addresses and networks. The functions and classes in this module make it straigh..." />
  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/ipaddress.py ipaddress provides the capabilities to create, manipulate and operate on IPv4 and IPv6 addresses and networks. The functions and classes in this module make it straigh..." />
  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>ipaddress — IPv4/IPv6 manipulation library &#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="Multimedia Services" href="mm.html" />
  33. <link rel="prev" title="xmlrpc.server — Basic XML-RPC servers" href="xmlrpc.server.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/ipaddress.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">ipaddress</span></code> — IPv4/IPv6 manipulation library</a><ul>
  86. <li><a class="reference internal" href="#convenience-factory-functions">Convenience factory functions</a></li>
  87. <li><a class="reference internal" href="#ip-addresses">IP Addresses</a><ul>
  88. <li><a class="reference internal" href="#address-objects">Address objects</a></li>
  89. <li><a class="reference internal" href="#conversion-to-strings-and-integers">Conversion to Strings and Integers</a></li>
  90. <li><a class="reference internal" href="#operators">Operators</a><ul>
  91. <li><a class="reference internal" href="#comparison-operators">Comparison operators</a></li>
  92. <li><a class="reference internal" href="#arithmetic-operators">Arithmetic operators</a></li>
  93. </ul>
  94. </li>
  95. </ul>
  96. </li>
  97. <li><a class="reference internal" href="#ip-network-definitions">IP Network definitions</a><ul>
  98. <li><a class="reference internal" href="#prefix-net-mask-and-host-mask">Prefix, net mask and host mask</a></li>
  99. <li><a class="reference internal" href="#network-objects">Network objects</a></li>
  100. <li><a class="reference internal" href="#id1">Operators</a><ul>
  101. <li><a class="reference internal" href="#logical-operators">Logical operators</a></li>
  102. <li><a class="reference internal" href="#iteration">Iteration</a></li>
  103. <li><a class="reference internal" href="#networks-as-containers-of-addresses">Networks as containers of addresses</a></li>
  104. </ul>
  105. </li>
  106. </ul>
  107. </li>
  108. <li><a class="reference internal" href="#interface-objects">Interface objects</a><ul>
  109. <li><a class="reference internal" href="#id2">Operators</a><ul>
  110. <li><a class="reference internal" href="#id3">Logical operators</a></li>
  111. </ul>
  112. </li>
  113. </ul>
  114. </li>
  115. <li><a class="reference internal" href="#other-module-level-functions">Other Module Level Functions</a></li>
  116. <li><a class="reference internal" href="#custom-exceptions">Custom Exceptions</a></li>
  117. </ul>
  118. </li>
  119. </ul>
  120. </div>
  121. <div>
  122. <h4>Previous topic</h4>
  123. <p class="topless"><a href="xmlrpc.server.html"
  124. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xmlrpc.server</span></code> — Basic XML-RPC servers</a></p>
  125. </div>
  126. <div>
  127. <h4>Next topic</h4>
  128. <p class="topless"><a href="mm.html"
  129. title="next chapter">Multimedia Services</a></p>
  130. </div>
  131. <div role="note" aria-label="source link">
  132. <h3>This Page</h3>
  133. <ul class="this-page-menu">
  134. <li><a href="../bugs.html">Report a Bug</a></li>
  135. <li>
  136. <a href="https://github.com/python/cpython/blob/main/Doc/library/ipaddress.rst"
  137. rel="nofollow">Show Source
  138. </a>
  139. </li>
  140. </ul>
  141. </div>
  142. </nav>
  143. </div>
  144. </div>
  145. <div class="related" role="navigation" aria-label="related navigation">
  146. <h3>Navigation</h3>
  147. <ul>
  148. <li class="right" style="margin-right: 10px">
  149. <a href="../genindex.html" title="General Index"
  150. accesskey="I">index</a></li>
  151. <li class="right" >
  152. <a href="../py-modindex.html" title="Python Module Index"
  153. >modules</a> |</li>
  154. <li class="right" >
  155. <a href="mm.html" title="Multimedia Services"
  156. accesskey="N">next</a> |</li>
  157. <li class="right" >
  158. <a href="xmlrpc.server.html" title="xmlrpc.server — Basic XML-RPC servers"
  159. accesskey="P">previous</a> |</li>
  160. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  161. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  162. <li class="switchers">
  163. <div class="language_switcher_placeholder"></div>
  164. <div class="version_switcher_placeholder"></div>
  165. </li>
  166. <li>
  167. </li>
  168. <li id="cpython-language-and-version">
  169. <a href="../index.html">3.12.3 Documentation</a> &#187;
  170. </li>
  171. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  172. <li class="nav-item nav-item-2"><a href="internet.html" accesskey="U">Internet Protocols and Support</a> &#187;</li>
  173. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">ipaddress</span></code> — IPv4/IPv6 manipulation library</a></li>
  174. <li class="right">
  175. <div class="inline-search" role="search">
  176. <form class="inline-search" action="../search.html" method="get">
  177. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  178. <input type="submit" value="Go" />
  179. </form>
  180. </div>
  181. |
  182. </li>
  183. <li class="right">
  184. <label class="theme-selector-label">
  185. Theme
  186. <select class="theme-selector" oninput="activateTheme(this.value)">
  187. <option value="auto" selected>Auto</option>
  188. <option value="light">Light</option>
  189. <option value="dark">Dark</option>
  190. </select>
  191. </label> |</li>
  192. </ul>
  193. </div>
  194. <div class="document">
  195. <div class="documentwrapper">
  196. <div class="bodywrapper">
  197. <div class="body" role="main">
  198. <section id="module-ipaddress">
  199. <span id="ipaddress-ipv4-ipv6-manipulation-library"></span><h1><a class="reference internal" href="#module-ipaddress" title="ipaddress: IPv4/IPv6 manipulation library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ipaddress</span></code></a> — IPv4/IPv6 manipulation library<a class="headerlink" href="#module-ipaddress" title="Link to this heading">¶</a></h1>
  200. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/ipaddress.py">Lib/ipaddress.py</a></p>
  201. <hr class="docutils" />
  202. <p><a class="reference internal" href="#module-ipaddress" title="ipaddress: IPv4/IPv6 manipulation library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ipaddress</span></code></a> provides the capabilities to create, manipulate and
  203. operate on IPv4 and IPv6 addresses and networks.</p>
  204. <p>The functions and classes in this module make it straightforward to handle
  205. various tasks related to IP addresses, including checking whether or not two
  206. hosts are on the same subnet, iterating over all hosts in a particular
  207. subnet, checking whether or not a string represents a valid IP address or
  208. network definition, and so on.</p>
  209. <p>This is the full module API reference—for an overview and introduction, see
  210. <a class="reference internal" href="../howto/ipaddress.html#ipaddress-howto"><span class="std std-ref">An introduction to the ipaddress module</span></a>.</p>
  211. <div class="versionadded">
  212. <p><span class="versionmodified added">New in version 3.3.</span></p>
  213. </div>
  214. <section id="convenience-factory-functions">
  215. <h2>Convenience factory functions<a class="headerlink" href="#convenience-factory-functions" title="Link to this heading">¶</a></h2>
  216. <p>The <a class="reference internal" href="#module-ipaddress" title="ipaddress: IPv4/IPv6 manipulation library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ipaddress</span></code></a> module provides factory functions to conveniently create
  217. IP addresses, networks and interfaces:</p>
  218. <dl class="py function">
  219. <dt class="sig sig-object py" id="ipaddress.ip_address">
  220. <span class="sig-prename descclassname"><span class="pre">ipaddress.</span></span><span class="sig-name descname"><span class="pre">ip_address</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="#ipaddress.ip_address" title="Link to this definition">¶</a></dt>
  221. <dd><p>Return an <a class="reference internal" href="#ipaddress.IPv4Address" title="ipaddress.IPv4Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Address</span></code></a> or <a class="reference internal" href="#ipaddress.IPv6Address" title="ipaddress.IPv6Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv6Address</span></code></a> object depending on
  222. the IP address passed as argument. Either IPv4 or IPv6 addresses may be
  223. supplied; integers less than <code class="docutils literal notranslate"><span class="pre">2**32</span></code> will be considered to be IPv4 by default.
  224. A <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised if <em>address</em> does not represent a valid IPv4
  225. or IPv6 address.</p>
  226. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ipaddress</span><span class="o">.</span><span class="n">ip_address</span><span class="p">(</span><span class="s1">&#39;192.168.0.1&#39;</span><span class="p">)</span>
  227. <span class="go">IPv4Address(&#39;192.168.0.1&#39;)</span>
  228. <span class="gp">&gt;&gt;&gt; </span><span class="n">ipaddress</span><span class="o">.</span><span class="n">ip_address</span><span class="p">(</span><span class="s1">&#39;2001:db8::&#39;</span><span class="p">)</span>
  229. <span class="go">IPv6Address(&#39;2001:db8::&#39;)</span>
  230. </pre></div>
  231. </div>
  232. </dd></dl>
  233. <dl class="py function">
  234. <dt class="sig sig-object py" id="ipaddress.ip_network">
  235. <span class="sig-prename descclassname"><span class="pre">ipaddress.</span></span><span class="sig-name descname"><span class="pre">ip_network</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">address</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">strict</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.ip_network" title="Link to this definition">¶</a></dt>
  236. <dd><p>Return an <a class="reference internal" href="#ipaddress.IPv4Network" title="ipaddress.IPv4Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Network</span></code></a> or <a class="reference internal" href="#ipaddress.IPv6Network" title="ipaddress.IPv6Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv6Network</span></code></a> object depending on
  237. the IP address passed as argument. <em>address</em> is a string or integer
  238. representing the IP network. Either IPv4 or IPv6 networks may be supplied;
  239. integers less than <code class="docutils literal notranslate"><span class="pre">2**32</span></code> will be considered to be IPv4 by default. <em>strict</em>
  240. is passed to <a class="reference internal" href="#ipaddress.IPv4Network" title="ipaddress.IPv4Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Network</span></code></a> or <a class="reference internal" href="#ipaddress.IPv6Network" title="ipaddress.IPv6Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv6Network</span></code></a> constructor. A
  241. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised if <em>address</em> does not represent a valid IPv4 or
  242. IPv6 address, or if the network has host bits set.</p>
  243. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ipaddress</span><span class="o">.</span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.168.0.0/28&#39;</span><span class="p">)</span>
  244. <span class="go">IPv4Network(&#39;192.168.0.0/28&#39;)</span>
  245. </pre></div>
  246. </div>
  247. </dd></dl>
  248. <dl class="py function">
  249. <dt class="sig sig-object py" id="ipaddress.ip_interface">
  250. <span class="sig-prename descclassname"><span class="pre">ipaddress.</span></span><span class="sig-name descname"><span class="pre">ip_interface</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="#ipaddress.ip_interface" title="Link to this definition">¶</a></dt>
  251. <dd><p>Return an <a class="reference internal" href="#ipaddress.IPv4Interface" title="ipaddress.IPv4Interface"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Interface</span></code></a> or <a class="reference internal" href="#ipaddress.IPv6Interface" title="ipaddress.IPv6Interface"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv6Interface</span></code></a> object depending
  252. on the IP address passed as argument. <em>address</em> is a string or integer
  253. representing the IP address. Either IPv4 or IPv6 addresses may be supplied;
  254. integers less than <code class="docutils literal notranslate"><span class="pre">2**32</span></code> will be considered to be IPv4 by default. A
  255. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised if <em>address</em> does not represent a valid IPv4 or
  256. IPv6 address.</p>
  257. </dd></dl>
  258. <p>One downside of these convenience functions is that the need to handle both
  259. IPv4 and IPv6 formats means that error messages provide minimal
  260. information on the precise error, as the functions don’t know whether the
  261. IPv4 or IPv6 format was intended. More detailed error reporting can be
  262. obtained by calling the appropriate version specific class constructors
  263. directly.</p>
  264. </section>
  265. <section id="ip-addresses">
  266. <h2>IP Addresses<a class="headerlink" href="#ip-addresses" title="Link to this heading">¶</a></h2>
  267. <section id="address-objects">
  268. <h3>Address objects<a class="headerlink" href="#address-objects" title="Link to this heading">¶</a></h3>
  269. <p>The <a class="reference internal" href="#ipaddress.IPv4Address" title="ipaddress.IPv4Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Address</span></code></a> and <a class="reference internal" href="#ipaddress.IPv6Address" title="ipaddress.IPv6Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv6Address</span></code></a> objects share a lot of common
  270. attributes. Some attributes that are only meaningful for IPv6 addresses are
  271. also implemented by <a class="reference internal" href="#ipaddress.IPv4Address" title="ipaddress.IPv4Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Address</span></code></a> objects, in order to make it easier to
  272. write code that handles both IP versions correctly. Address objects are
  273. <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a>, so they can be used as keys in dictionaries.</p>
  274. <dl class="py class">
  275. <dt class="sig sig-object py" id="ipaddress.IPv4Address">
  276. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ipaddress.</span></span><span class="sig-name descname"><span class="pre">IPv4Address</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="#ipaddress.IPv4Address" title="Link to this definition">¶</a></dt>
  277. <dd><p>Construct an IPv4 address. An <a class="reference internal" href="#ipaddress.AddressValueError" title="ipaddress.AddressValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AddressValueError</span></code></a> is raised if
  278. <em>address</em> is not a valid IPv4 address.</p>
  279. <p>The following constitutes a valid IPv4 address:</p>
  280. <ol class="arabic simple">
  281. <li><p>A string in decimal-dot notation, consisting of four decimal integers in
  282. the inclusive range 0–255, separated by dots (e.g. <code class="docutils literal notranslate"><span class="pre">192.168.0.1</span></code>). Each
  283. integer represents an octet (byte) in the address. Leading zeroes are
  284. not tolerated to prevent confusion with octal notation.</p></li>
  285. <li><p>An integer that fits into 32 bits.</p></li>
  286. <li><p>An integer packed into 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 of length 4 (most
  287. significant octet first).</p></li>
  288. </ol>
  289. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;192.168.0.1&#39;</span><span class="p">)</span>
  290. <span class="go">IPv4Address(&#39;192.168.0.1&#39;)</span>
  291. <span class="gp">&gt;&gt;&gt; </span><span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv4Address</span><span class="p">(</span><span class="mi">3232235521</span><span class="p">)</span>
  292. <span class="go">IPv4Address(&#39;192.168.0.1&#39;)</span>
  293. <span class="gp">&gt;&gt;&gt; </span><span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv4Address</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;</span><span class="se">\xC0\xA8\x00\x01</span><span class="s1">&#39;</span><span class="p">)</span>
  294. <span class="go">IPv4Address(&#39;192.168.0.1&#39;)</span>
  295. </pre></div>
  296. </div>
  297. <div class="versionchanged">
  298. <p><span class="versionmodified changed">Changed in version 3.8: </span>Leading zeros are tolerated, even in ambiguous cases that look like
  299. octal notation.</p>
  300. </div>
  301. <div class="versionchanged">
  302. <p><span class="versionmodified changed">Changed in version 3.9.5: </span>Leading zeros are no longer tolerated and are treated as an error.
  303. IPv4 address strings are now parsed as strict as glibc
  304. <a class="reference internal" href="socket.html#socket.inet_pton" title="socket.inet_pton"><code class="xref py py-func docutils literal notranslate"><span class="pre">inet_pton()</span></code></a>.</p>
  305. </div>
  306. <dl class="py attribute">
  307. <dt class="sig sig-object py" id="ipaddress.IPv4Address.version">
  308. <span class="sig-name descname"><span class="pre">version</span></span><a class="headerlink" href="#ipaddress.IPv4Address.version" title="Link to this definition">¶</a></dt>
  309. <dd><p>The appropriate version number: <code class="docutils literal notranslate"><span class="pre">4</span></code> for IPv4, <code class="docutils literal notranslate"><span class="pre">6</span></code> for IPv6.</p>
  310. </dd></dl>
  311. <dl class="py attribute">
  312. <dt class="sig sig-object py" id="ipaddress.IPv4Address.max_prefixlen">
  313. <span class="sig-name descname"><span class="pre">max_prefixlen</span></span><a class="headerlink" href="#ipaddress.IPv4Address.max_prefixlen" title="Link to this definition">¶</a></dt>
  314. <dd><p>The total number of bits in the address representation for this
  315. version: <code class="docutils literal notranslate"><span class="pre">32</span></code> for IPv4, <code class="docutils literal notranslate"><span class="pre">128</span></code> for IPv6.</p>
  316. <p>The prefix defines the number of leading bits in an address that
  317. are compared to determine whether or not an address is part of a
  318. network.</p>
  319. </dd></dl>
  320. <dl class="py attribute">
  321. <dt class="sig sig-object py" id="ipaddress.IPv4Address.compressed">
  322. <span class="sig-name descname"><span class="pre">compressed</span></span><a class="headerlink" href="#ipaddress.IPv4Address.compressed" title="Link to this definition">¶</a></dt>
  323. <dd></dd></dl>
  324. <dl class="py attribute">
  325. <dt class="sig sig-object py" id="ipaddress.IPv4Address.exploded">
  326. <span class="sig-name descname"><span class="pre">exploded</span></span><a class="headerlink" href="#ipaddress.IPv4Address.exploded" title="Link to this definition">¶</a></dt>
  327. <dd><p>The string representation in dotted decimal notation. Leading zeroes
  328. are never included in the representation.</p>
  329. <p>As IPv4 does not define a shorthand notation for addresses with octets
  330. set to zero, these two attributes are always the same as <code class="docutils literal notranslate"><span class="pre">str(addr)</span></code>
  331. for IPv4 addresses. Exposing these attributes makes it easier to
  332. write display code that can handle both IPv4 and IPv6 addresses.</p>
  333. </dd></dl>
  334. <dl class="py attribute">
  335. <dt class="sig sig-object py" id="ipaddress.IPv4Address.packed">
  336. <span class="sig-name descname"><span class="pre">packed</span></span><a class="headerlink" href="#ipaddress.IPv4Address.packed" title="Link to this definition">¶</a></dt>
  337. <dd><p>The binary representation of this address - 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 of
  338. the appropriate length (most significant octet first). This is 4 bytes
  339. for IPv4 and 16 bytes for IPv6.</p>
  340. </dd></dl>
  341. <dl class="py attribute">
  342. <dt class="sig sig-object py" id="ipaddress.IPv4Address.reverse_pointer">
  343. <span class="sig-name descname"><span class="pre">reverse_pointer</span></span><a class="headerlink" href="#ipaddress.IPv4Address.reverse_pointer" title="Link to this definition">¶</a></dt>
  344. <dd><p>The name of the reverse DNS PTR record for the IP address, e.g.:</p>
  345. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ipaddress</span><span class="o">.</span><span class="n">ip_address</span><span class="p">(</span><span class="s2">&quot;127.0.0.1&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">reverse_pointer</span>
  346. <span class="go">&#39;1.0.0.127.in-addr.arpa&#39;</span>
  347. <span class="gp">&gt;&gt;&gt; </span><span class="n">ipaddress</span><span class="o">.</span><span class="n">ip_address</span><span class="p">(</span><span class="s2">&quot;2001:db8::1&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">reverse_pointer</span>
  348. <span class="go">&#39;1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa&#39;</span>
  349. </pre></div>
  350. </div>
  351. <p>This is the name that could be used for performing a PTR lookup, not the
  352. resolved hostname itself.</p>
  353. <div class="versionadded">
  354. <p><span class="versionmodified added">New in version 3.5.</span></p>
  355. </div>
  356. </dd></dl>
  357. <dl class="py attribute">
  358. <dt class="sig sig-object py" id="ipaddress.IPv4Address.is_multicast">
  359. <span class="sig-name descname"><span class="pre">is_multicast</span></span><a class="headerlink" href="#ipaddress.IPv4Address.is_multicast" title="Link to this definition">¶</a></dt>
  360. <dd><p><code class="docutils literal notranslate"><span class="pre">True</span></code> if the address is reserved for multicast use. See
  361. <span class="target" id="index-0"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3171.html"><strong>RFC 3171</strong></a> (for IPv4) or <span class="target" id="index-1"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2373.html"><strong>RFC 2373</strong></a> (for IPv6).</p>
  362. </dd></dl>
  363. <dl class="py attribute">
  364. <dt class="sig sig-object py" id="ipaddress.IPv4Address.is_private">
  365. <span class="sig-name descname"><span class="pre">is_private</span></span><a class="headerlink" href="#ipaddress.IPv4Address.is_private" title="Link to this definition">¶</a></dt>
  366. <dd><p><code class="docutils literal notranslate"><span class="pre">True</span></code> if the address is allocated for private networks. See
  367. <a class="reference external" href="https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml">iana-ipv4-special-registry</a> (for IPv4) or <a class="reference external" href="https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml">iana-ipv6-special-registry</a>
  368. (for IPv6).</p>
  369. </dd></dl>
  370. <dl class="py attribute">
  371. <dt class="sig sig-object py" id="ipaddress.IPv4Address.is_global">
  372. <span class="sig-name descname"><span class="pre">is_global</span></span><a class="headerlink" href="#ipaddress.IPv4Address.is_global" title="Link to this definition">¶</a></dt>
  373. <dd><p><code class="docutils literal notranslate"><span class="pre">True</span></code> if the address is allocated for public networks. See
  374. <a class="reference external" href="https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml">iana-ipv4-special-registry</a> (for IPv4) or <a class="reference external" href="https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml">iana-ipv6-special-registry</a>
  375. (for IPv6).</p>
  376. <div class="versionadded">
  377. <p><span class="versionmodified added">New in version 3.4.</span></p>
  378. </div>
  379. </dd></dl>
  380. <dl class="py attribute">
  381. <dt class="sig sig-object py" id="ipaddress.IPv4Address.is_unspecified">
  382. <span class="sig-name descname"><span class="pre">is_unspecified</span></span><a class="headerlink" href="#ipaddress.IPv4Address.is_unspecified" title="Link to this definition">¶</a></dt>
  383. <dd><p><code class="docutils literal notranslate"><span class="pre">True</span></code> if the address is unspecified. See <span class="target" id="index-2"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5735.html"><strong>RFC 5735</strong></a> (for IPv4)
  384. or <span class="target" id="index-3"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2373.html"><strong>RFC 2373</strong></a> (for IPv6).</p>
  385. </dd></dl>
  386. <dl class="py attribute">
  387. <dt class="sig sig-object py" id="ipaddress.IPv4Address.is_reserved">
  388. <span class="sig-name descname"><span class="pre">is_reserved</span></span><a class="headerlink" href="#ipaddress.IPv4Address.is_reserved" title="Link to this definition">¶</a></dt>
  389. <dd><p><code class="docutils literal notranslate"><span class="pre">True</span></code> if the address is otherwise IETF reserved.</p>
  390. </dd></dl>
  391. <dl class="py attribute">
  392. <dt class="sig sig-object py" id="ipaddress.IPv4Address.is_loopback">
  393. <span class="sig-name descname"><span class="pre">is_loopback</span></span><a class="headerlink" href="#ipaddress.IPv4Address.is_loopback" title="Link to this definition">¶</a></dt>
  394. <dd><p><code class="docutils literal notranslate"><span class="pre">True</span></code> if this is a loopback address. See <span class="target" id="index-4"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3330.html"><strong>RFC 3330</strong></a> (for IPv4)
  395. or <span class="target" id="index-5"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2373.html"><strong>RFC 2373</strong></a> (for IPv6).</p>
  396. </dd></dl>
  397. <dl class="py attribute">
  398. <dt class="sig sig-object py" id="ipaddress.IPv4Address.is_link_local">
  399. <span class="sig-name descname"><span class="pre">is_link_local</span></span><a class="headerlink" href="#ipaddress.IPv4Address.is_link_local" title="Link to this definition">¶</a></dt>
  400. <dd><p><code class="docutils literal notranslate"><span class="pre">True</span></code> if the address is reserved for link-local usage. See
  401. <span class="target" id="index-6"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3927.html"><strong>RFC 3927</strong></a>.</p>
  402. </dd></dl>
  403. </dd></dl>
  404. <dl class="py method">
  405. <dt class="sig sig-object py" id="ipaddress.IPv4Address.__format__">
  406. <span class="sig-prename descclassname"><span class="pre">IPv4Address.</span></span><span class="sig-name descname"><span class="pre">__format__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fmt</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.IPv4Address.__format__" title="Link to this definition">¶</a></dt>
  407. <dd><p>Returns a string representation of the IP address, controlled by
  408. an explicit format string.
  409. <em>fmt</em> can be one of the following: <code class="docutils literal notranslate"><span class="pre">'s'</span></code>, the default option,
  410. equivalent to <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a>, <code class="docutils literal notranslate"><span class="pre">'b'</span></code> for a zero-padded binary string,
  411. <code class="docutils literal notranslate"><span class="pre">'X'</span></code> or <code class="docutils literal notranslate"><span class="pre">'x'</span></code> for an uppercase or lowercase hexadecimal
  412. representation, or <code class="docutils literal notranslate"><span class="pre">'n'</span></code>, which is equivalent to <code class="docutils literal notranslate"><span class="pre">'b'</span></code> for IPv4
  413. addresses and <code class="docutils literal notranslate"><span class="pre">'x'</span></code> for IPv6. For binary and hexadecimal
  414. representations, the form specifier <code class="docutils literal notranslate"><span class="pre">'#'</span></code> and the grouping option
  415. <code class="docutils literal notranslate"><span class="pre">'_'</span></code> are available. <code class="docutils literal notranslate"><span class="pre">__format__</span></code> is used by <code class="docutils literal notranslate"><span class="pre">format</span></code>, <code class="docutils literal notranslate"><span class="pre">str.format</span></code>
  416. and f-strings.</p>
  417. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">format</span><span class="p">(</span><span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;192.168.0.1&#39;</span><span class="p">))</span>
  418. <span class="go">&#39;192.168.0.1&#39;</span>
  419. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{:#b}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;192.168.0.1&#39;</span><span class="p">))</span>
  420. <span class="go">&#39;0b11000000101010000000000000000001&#39;</span>
  421. <span class="gp">&gt;&gt;&gt; </span><span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv6Address</span><span class="p">(</span><span class="s2">&quot;2001:db8::1000&quot;</span><span class="p">)</span><span class="si">:</span><span class="s1">s</span><span class="si">}</span><span class="s1">&#39;</span>
  422. <span class="go">&#39;2001:db8::1000&#39;</span>
  423. <span class="gp">&gt;&gt;&gt; </span><span class="nb">format</span><span class="p">(</span><span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv6Address</span><span class="p">(</span><span class="s1">&#39;2001:db8::1000&#39;</span><span class="p">),</span> <span class="s1">&#39;_X&#39;</span><span class="p">)</span>
  424. <span class="go">&#39;2001_0DB8_0000_0000_0000_0000_0000_1000&#39;</span>
  425. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;{:#_n}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv6Address</span><span class="p">(</span><span class="s1">&#39;2001:db8::1000&#39;</span><span class="p">))</span>
  426. <span class="go">&#39;0x2001_0db8_0000_0000_0000_0000_0000_1000&#39;</span>
  427. </pre></div>
  428. </div>
  429. <div class="versionadded">
  430. <p><span class="versionmodified added">New in version 3.9.</span></p>
  431. </div>
  432. </dd></dl>
  433. <dl class="py class">
  434. <dt class="sig sig-object py" id="ipaddress.IPv6Address">
  435. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ipaddress.</span></span><span class="sig-name descname"><span class="pre">IPv6Address</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="#ipaddress.IPv6Address" title="Link to this definition">¶</a></dt>
  436. <dd><p>Construct an IPv6 address. An <a class="reference internal" href="#ipaddress.AddressValueError" title="ipaddress.AddressValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AddressValueError</span></code></a> is raised if
  437. <em>address</em> is not a valid IPv6 address.</p>
  438. <p>The following constitutes a valid IPv6 address:</p>
  439. <ol class="arabic">
  440. <li><p>A string consisting of eight groups of four hexadecimal digits, each
  441. group representing 16 bits. The groups are separated by colons.
  442. This describes an <em>exploded</em> (longhand) notation. The string can
  443. also be <em>compressed</em> (shorthand notation) by various means. See
  444. <span class="target" id="index-7"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4291.html"><strong>RFC 4291</strong></a> for details. For example,
  445. <code class="docutils literal notranslate"><span class="pre">&quot;0000:0000:0000:0000:0000:0abc:0007:0def&quot;</span></code> can be compressed to
  446. <code class="docutils literal notranslate"><span class="pre">&quot;::abc:7:def&quot;</span></code>.</p>
  447. <p>Optionally, the string may also have a scope zone ID, expressed
  448. with a suffix <code class="docutils literal notranslate"><span class="pre">%scope_id</span></code>. If present, the scope ID must be non-empty,
  449. and may not contain <code class="docutils literal notranslate"><span class="pre">%</span></code>.
  450. See <span class="target" id="index-8"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4007.html"><strong>RFC 4007</strong></a> for details.
  451. For example, <code class="docutils literal notranslate"><span class="pre">fe80::1234%1</span></code> might identify address <code class="docutils literal notranslate"><span class="pre">fe80::1234</span></code> on the first link of the node.</p>
  452. </li>
  453. <li><p>An integer that fits into 128 bits.</p></li>
  454. <li><p>An integer packed into 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 of length 16, big-endian.</p></li>
  455. </ol>
  456. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv6Address</span><span class="p">(</span><span class="s1">&#39;2001:db8::1000&#39;</span><span class="p">)</span>
  457. <span class="go">IPv6Address(&#39;2001:db8::1000&#39;)</span>
  458. <span class="gp">&gt;&gt;&gt; </span><span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv6Address</span><span class="p">(</span><span class="s1">&#39;ff02::5678%1&#39;</span><span class="p">)</span>
  459. <span class="go">IPv6Address(&#39;ff02::5678%1&#39;)</span>
  460. </pre></div>
  461. </div>
  462. <dl class="py attribute">
  463. <dt class="sig sig-object py" id="ipaddress.IPv6Address.compressed">
  464. <span class="sig-name descname"><span class="pre">compressed</span></span><a class="headerlink" href="#ipaddress.IPv6Address.compressed" title="Link to this definition">¶</a></dt>
  465. <dd></dd></dl>
  466. <p>The short form of the address representation, with leading zeroes in
  467. groups omitted and the longest sequence of groups consisting entirely of
  468. zeroes collapsed to a single empty group.</p>
  469. <p>This is also the value returned by <code class="docutils literal notranslate"><span class="pre">str(addr)</span></code> for IPv6 addresses.</p>
  470. <dl class="py attribute">
  471. <dt class="sig sig-object py" id="ipaddress.IPv6Address.exploded">
  472. <span class="sig-name descname"><span class="pre">exploded</span></span><a class="headerlink" href="#ipaddress.IPv6Address.exploded" title="Link to this definition">¶</a></dt>
  473. <dd></dd></dl>
  474. <p>The long form of the address representation, with all leading zeroes and
  475. groups consisting entirely of zeroes included.</p>
  476. <p>For the following attributes and methods, see the corresponding
  477. documentation of the <a class="reference internal" href="#ipaddress.IPv4Address" title="ipaddress.IPv4Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Address</span></code></a> class:</p>
  478. <dl class="py attribute">
  479. <dt class="sig sig-object py" id="ipaddress.IPv6Address.packed">
  480. <span class="sig-name descname"><span class="pre">packed</span></span><a class="headerlink" href="#ipaddress.IPv6Address.packed" title="Link to this definition">¶</a></dt>
  481. <dd></dd></dl>
  482. <dl class="py attribute">
  483. <dt class="sig sig-object py" id="ipaddress.IPv6Address.reverse_pointer">
  484. <span class="sig-name descname"><span class="pre">reverse_pointer</span></span><a class="headerlink" href="#ipaddress.IPv6Address.reverse_pointer" title="Link to this definition">¶</a></dt>
  485. <dd></dd></dl>
  486. <dl class="py attribute">
  487. <dt class="sig sig-object py" id="ipaddress.IPv6Address.version">
  488. <span class="sig-name descname"><span class="pre">version</span></span><a class="headerlink" href="#ipaddress.IPv6Address.version" title="Link to this definition">¶</a></dt>
  489. <dd></dd></dl>
  490. <dl class="py attribute">
  491. <dt class="sig sig-object py" id="ipaddress.IPv6Address.max_prefixlen">
  492. <span class="sig-name descname"><span class="pre">max_prefixlen</span></span><a class="headerlink" href="#ipaddress.IPv6Address.max_prefixlen" title="Link to this definition">¶</a></dt>
  493. <dd></dd></dl>
  494. <dl class="py attribute">
  495. <dt class="sig sig-object py" id="ipaddress.IPv6Address.is_multicast">
  496. <span class="sig-name descname"><span class="pre">is_multicast</span></span><a class="headerlink" href="#ipaddress.IPv6Address.is_multicast" title="Link to this definition">¶</a></dt>
  497. <dd></dd></dl>
  498. <dl class="py attribute">
  499. <dt class="sig sig-object py" id="ipaddress.IPv6Address.is_private">
  500. <span class="sig-name descname"><span class="pre">is_private</span></span><a class="headerlink" href="#ipaddress.IPv6Address.is_private" title="Link to this definition">¶</a></dt>
  501. <dd></dd></dl>
  502. <dl class="py attribute">
  503. <dt class="sig sig-object py" id="ipaddress.IPv6Address.is_global">
  504. <span class="sig-name descname"><span class="pre">is_global</span></span><a class="headerlink" href="#ipaddress.IPv6Address.is_global" title="Link to this definition">¶</a></dt>
  505. <dd></dd></dl>
  506. <dl class="py attribute">
  507. <dt class="sig sig-object py" id="ipaddress.IPv6Address.is_unspecified">
  508. <span class="sig-name descname"><span class="pre">is_unspecified</span></span><a class="headerlink" href="#ipaddress.IPv6Address.is_unspecified" title="Link to this definition">¶</a></dt>
  509. <dd></dd></dl>
  510. <dl class="py attribute">
  511. <dt class="sig sig-object py" id="ipaddress.IPv6Address.is_reserved">
  512. <span class="sig-name descname"><span class="pre">is_reserved</span></span><a class="headerlink" href="#ipaddress.IPv6Address.is_reserved" title="Link to this definition">¶</a></dt>
  513. <dd></dd></dl>
  514. <dl class="py attribute">
  515. <dt class="sig sig-object py" id="ipaddress.IPv6Address.is_loopback">
  516. <span class="sig-name descname"><span class="pre">is_loopback</span></span><a class="headerlink" href="#ipaddress.IPv6Address.is_loopback" title="Link to this definition">¶</a></dt>
  517. <dd></dd></dl>
  518. <dl class="py attribute">
  519. <dt class="sig sig-object py" id="ipaddress.IPv6Address.is_link_local">
  520. <span class="sig-name descname"><span class="pre">is_link_local</span></span><a class="headerlink" href="#ipaddress.IPv6Address.is_link_local" title="Link to this definition">¶</a></dt>
  521. <dd><div class="versionadded">
  522. <p><span class="versionmodified added">New in version 3.4: </span>is_global</p>
  523. </div>
  524. </dd></dl>
  525. <dl class="py attribute">
  526. <dt class="sig sig-object py" id="ipaddress.IPv6Address.is_site_local">
  527. <span class="sig-name descname"><span class="pre">is_site_local</span></span><a class="headerlink" href="#ipaddress.IPv6Address.is_site_local" title="Link to this definition">¶</a></dt>
  528. <dd><p><code class="docutils literal notranslate"><span class="pre">True</span></code> if the address is reserved for site-local usage. Note that
  529. the site-local address space has been deprecated by <span class="target" id="index-9"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3879.html"><strong>RFC 3879</strong></a>. Use
  530. <a class="reference internal" href="#ipaddress.IPv4Address.is_private" title="ipaddress.IPv4Address.is_private"><code class="xref py py-attr docutils literal notranslate"><span class="pre">is_private</span></code></a> to test if this address is in the
  531. space of unique local addresses as defined by <span class="target" id="index-10"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4193.html"><strong>RFC 4193</strong></a>.</p>
  532. </dd></dl>
  533. <dl class="py attribute">
  534. <dt class="sig sig-object py" id="ipaddress.IPv6Address.ipv4_mapped">
  535. <span class="sig-name descname"><span class="pre">ipv4_mapped</span></span><a class="headerlink" href="#ipaddress.IPv6Address.ipv4_mapped" title="Link to this definition">¶</a></dt>
  536. <dd><p>For addresses that appear to be IPv4 mapped addresses (starting with
  537. <code class="docutils literal notranslate"><span class="pre">::FFFF/96</span></code>), this property will report the embedded IPv4 address.
  538. For any other address, this property will be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  539. </dd></dl>
  540. <dl class="py attribute">
  541. <dt class="sig sig-object py" id="ipaddress.IPv6Address.scope_id">
  542. <span class="sig-name descname"><span class="pre">scope_id</span></span><a class="headerlink" href="#ipaddress.IPv6Address.scope_id" title="Link to this definition">¶</a></dt>
  543. <dd><p>For scoped addresses as defined by <span class="target" id="index-11"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4007.html"><strong>RFC 4007</strong></a>, this property identifies
  544. the particular zone of the address’s scope that the address belongs to,
  545. as a string. When no scope zone is specified, this property will be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  546. </dd></dl>
  547. <dl class="py attribute">
  548. <dt class="sig sig-object py" id="ipaddress.IPv6Address.sixtofour">
  549. <span class="sig-name descname"><span class="pre">sixtofour</span></span><a class="headerlink" href="#ipaddress.IPv6Address.sixtofour" title="Link to this definition">¶</a></dt>
  550. <dd><p>For addresses that appear to be 6to4 addresses (starting with
  551. <code class="docutils literal notranslate"><span class="pre">2002::/16</span></code>) as defined by <span class="target" id="index-12"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3056.html"><strong>RFC 3056</strong></a>, this property will report
  552. the embedded IPv4 address. For any other address, this property will
  553. be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  554. </dd></dl>
  555. <dl class="py attribute">
  556. <dt class="sig sig-object py" id="ipaddress.IPv6Address.teredo">
  557. <span class="sig-name descname"><span class="pre">teredo</span></span><a class="headerlink" href="#ipaddress.IPv6Address.teredo" title="Link to this definition">¶</a></dt>
  558. <dd><p>For addresses that appear to be Teredo addresses (starting with
  559. <code class="docutils literal notranslate"><span class="pre">2001::/32</span></code>) as defined by <span class="target" id="index-13"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4380.html"><strong>RFC 4380</strong></a>, this property will report
  560. the embedded <code class="docutils literal notranslate"><span class="pre">(server,</span> <span class="pre">client)</span></code> IP address pair. For any other
  561. address, this property will be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  562. </dd></dl>
  563. </dd></dl>
  564. <dl class="py method">
  565. <dt class="sig sig-object py" id="ipaddress.IPv6Address.__format__">
  566. <span class="sig-prename descclassname"><span class="pre">IPv6Address.</span></span><span class="sig-name descname"><span class="pre">__format__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fmt</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.IPv6Address.__format__" title="Link to this definition">¶</a></dt>
  567. <dd><p>Refer to the corresponding method documentation in
  568. <a class="reference internal" href="#ipaddress.IPv4Address" title="ipaddress.IPv4Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Address</span></code></a>.</p>
  569. <div class="versionadded">
  570. <p><span class="versionmodified added">New in version 3.9.</span></p>
  571. </div>
  572. </dd></dl>
  573. </section>
  574. <section id="conversion-to-strings-and-integers">
  575. <h3>Conversion to Strings and Integers<a class="headerlink" href="#conversion-to-strings-and-integers" title="Link to this heading">¶</a></h3>
  576. <p>To interoperate with networking interfaces such as the socket module,
  577. addresses must be converted to strings or integers. This is handled using
  578. the <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> and <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a> builtin functions:</p>
  579. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;192.168.0.1&#39;</span><span class="p">))</span>
  580. <span class="go">&#39;192.168.0.1&#39;</span>
  581. <span class="gp">&gt;&gt;&gt; </span><span class="nb">int</span><span class="p">(</span><span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;192.168.0.1&#39;</span><span class="p">))</span>
  582. <span class="go">3232235521</span>
  583. <span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv6Address</span><span class="p">(</span><span class="s1">&#39;::1&#39;</span><span class="p">))</span>
  584. <span class="go">&#39;::1&#39;</span>
  585. <span class="gp">&gt;&gt;&gt; </span><span class="nb">int</span><span class="p">(</span><span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv6Address</span><span class="p">(</span><span class="s1">&#39;::1&#39;</span><span class="p">))</span>
  586. <span class="go">1</span>
  587. </pre></div>
  588. </div>
  589. <p>Note that IPv6 scoped addresses are converted to integers without scope zone ID.</p>
  590. </section>
  591. <section id="operators">
  592. <h3>Operators<a class="headerlink" href="#operators" title="Link to this heading">¶</a></h3>
  593. <p>Address objects support some operators. Unless stated otherwise, operators can
  594. only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with
  595. IPv6).</p>
  596. <section id="comparison-operators">
  597. <h4>Comparison operators<a class="headerlink" href="#comparison-operators" title="Link to this heading">¶</a></h4>
  598. <p>Address objects can be compared with the usual set of comparison operators.
  599. Same IPv6 addresses with different scope zone IDs are not equal.
  600. Some examples:</p>
  601. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;127.0.0.2&#39;</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">)</span>
  602. <span class="go">True</span>
  603. <span class="gp">&gt;&gt;&gt; </span><span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;127.0.0.2&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">)</span>
  604. <span class="go">False</span>
  605. <span class="gp">&gt;&gt;&gt; </span><span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;127.0.0.2&#39;</span><span class="p">)</span> <span class="o">!=</span> <span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">)</span>
  606. <span class="go">True</span>
  607. <span class="gp">&gt;&gt;&gt; </span><span class="n">IPv6Address</span><span class="p">(</span><span class="s1">&#39;fe80::1234&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="n">IPv6Address</span><span class="p">(</span><span class="s1">&#39;fe80::1234%1&#39;</span><span class="p">)</span>
  608. <span class="go">False</span>
  609. <span class="gp">&gt;&gt;&gt; </span><span class="n">IPv6Address</span><span class="p">(</span><span class="s1">&#39;fe80::1234%1&#39;</span><span class="p">)</span> <span class="o">!=</span> <span class="n">IPv6Address</span><span class="p">(</span><span class="s1">&#39;fe80::1234%2&#39;</span><span class="p">)</span>
  610. <span class="go">True</span>
  611. </pre></div>
  612. </div>
  613. </section>
  614. <section id="arithmetic-operators">
  615. <h4>Arithmetic operators<a class="headerlink" href="#arithmetic-operators" title="Link to this heading">¶</a></h4>
  616. <p>Integers can be added to or subtracted from address objects. Some examples:</p>
  617. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;127.0.0.2&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="mi">3</span>
  618. <span class="go">IPv4Address(&#39;127.0.0.5&#39;)</span>
  619. <span class="gp">&gt;&gt;&gt; </span><span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;127.0.0.2&#39;</span><span class="p">)</span> <span class="o">-</span> <span class="mi">3</span>
  620. <span class="go">IPv4Address(&#39;126.255.255.255&#39;)</span>
  621. <span class="gp">&gt;&gt;&gt; </span><span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;255.255.255.255&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
  622. <span class="gt">Traceback (most recent call last):</span>
  623. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
  624. <span class="gr">ipaddress.AddressValueError</span>: <span class="n">4294967296 (&gt;= 2**32) is not permitted as an IPv4 address</span>
  625. </pre></div>
  626. </div>
  627. </section>
  628. </section>
  629. </section>
  630. <section id="ip-network-definitions">
  631. <h2>IP Network definitions<a class="headerlink" href="#ip-network-definitions" title="Link to this heading">¶</a></h2>
  632. <p>The <a class="reference internal" href="#ipaddress.IPv4Network" title="ipaddress.IPv4Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Network</span></code></a> and <a class="reference internal" href="#ipaddress.IPv6Network" title="ipaddress.IPv6Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv6Network</span></code></a> objects provide a mechanism
  633. for defining and inspecting IP network definitions. A network definition
  634. consists of a <em>mask</em> and a <em>network address</em>, and as such defines a range of
  635. IP addresses that equal the network address when masked (binary AND) with the
  636. mask. For example, a network definition with the mask <code class="docutils literal notranslate"><span class="pre">255.255.255.0</span></code> and
  637. the network address <code class="docutils literal notranslate"><span class="pre">192.168.1.0</span></code> consists of IP addresses in the inclusive
  638. range <code class="docutils literal notranslate"><span class="pre">192.168.1.0</span></code> to <code class="docutils literal notranslate"><span class="pre">192.168.1.255</span></code>.</p>
  639. <section id="prefix-net-mask-and-host-mask">
  640. <h3>Prefix, net mask and host mask<a class="headerlink" href="#prefix-net-mask-and-host-mask" title="Link to this heading">¶</a></h3>
  641. <p>There are several equivalent ways to specify IP network masks. A <em>prefix</em>
  642. <code class="docutils literal notranslate"><span class="pre">/&lt;nbits&gt;</span></code> is a notation that denotes how many high-order bits are set in
  643. the network mask. A <em>net mask</em> is an IP address with some number of
  644. high-order bits set. Thus the prefix <code class="docutils literal notranslate"><span class="pre">/24</span></code> is equivalent to the net mask
  645. <code class="docutils literal notranslate"><span class="pre">255.255.255.0</span></code> in IPv4, or <code class="docutils literal notranslate"><span class="pre">ffff:ff00::</span></code> in IPv6. In addition, a
  646. <em>host mask</em> is the logical inverse of a <em>net mask</em>, and is sometimes used
  647. (for example in Cisco access control lists) to denote a network mask. The
  648. host mask equivalent to <code class="docutils literal notranslate"><span class="pre">/24</span></code> in IPv4 is <code class="docutils literal notranslate"><span class="pre">0.0.0.255</span></code>.</p>
  649. </section>
  650. <section id="network-objects">
  651. <h3>Network objects<a class="headerlink" href="#network-objects" title="Link to this heading">¶</a></h3>
  652. <p>All attributes implemented by address objects are implemented by network
  653. objects as well. In addition, network objects implement additional attributes.
  654. All of these are common between <a class="reference internal" href="#ipaddress.IPv4Network" title="ipaddress.IPv4Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Network</span></code></a> and <a class="reference internal" href="#ipaddress.IPv6Network" title="ipaddress.IPv6Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv6Network</span></code></a>,
  655. so to avoid duplication they are only documented for <a class="reference internal" href="#ipaddress.IPv4Network" title="ipaddress.IPv4Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Network</span></code></a>.
  656. Network objects are <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a>, so they can be used as keys in
  657. dictionaries.</p>
  658. <dl class="py class">
  659. <dt class="sig sig-object py" id="ipaddress.IPv4Network">
  660. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ipaddress.</span></span><span class="sig-name descname"><span class="pre">IPv4Network</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">address</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">strict</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.IPv4Network" title="Link to this definition">¶</a></dt>
  661. <dd><p>Construct an IPv4 network definition. <em>address</em> can be one of the following:</p>
  662. <ol class="arabic">
  663. <li><p>A string consisting of an IP address and an optional mask, separated by
  664. a slash (<code class="docutils literal notranslate"><span class="pre">/</span></code>). The IP address is the network address, and the mask
  665. can be either a single number, which means it’s a <em>prefix</em>, or a string
  666. representation of an IPv4 address. If it’s the latter, the mask is
  667. interpreted as a <em>net mask</em> if it starts with a non-zero field, or as a
  668. <em>host mask</em> if it starts with a zero field, with the single exception of
  669. an all-zero mask which is treated as a <em>net mask</em>. If no mask is provided,
  670. it’s considered to be <code class="docutils literal notranslate"><span class="pre">/32</span></code>.</p>
  671. <p>For example, the following <em>address</em> specifications are equivalent:
  672. <code class="docutils literal notranslate"><span class="pre">192.168.1.0/24</span></code>, <code class="docutils literal notranslate"><span class="pre">192.168.1.0/255.255.255.0</span></code> and
  673. <code class="docutils literal notranslate"><span class="pre">192.168.1.0/0.0.0.255</span></code>.</p>
  674. </li>
  675. <li><p>An integer that fits into 32 bits. This is equivalent to a
  676. single-address network, with the network address being <em>address</em> and
  677. the mask being <code class="docutils literal notranslate"><span class="pre">/32</span></code>.</p></li>
  678. <li><p>An integer packed into 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 of length 4, big-endian.
  679. The interpretation is similar to an integer <em>address</em>.</p></li>
  680. <li><p>A two-tuple of an address description and a netmask, where the address
  681. description is either a string, a 32-bits integer, a 4-bytes packed
  682. integer, or an existing IPv4Address object; and the netmask is either
  683. an integer representing the prefix length (e.g. <code class="docutils literal notranslate"><span class="pre">24</span></code>) or a string
  684. representing the prefix mask (e.g. <code class="docutils literal notranslate"><span class="pre">255.255.255.0</span></code>).</p></li>
  685. </ol>
  686. <p>An <a class="reference internal" href="#ipaddress.AddressValueError" title="ipaddress.AddressValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AddressValueError</span></code></a> is raised if <em>address</em> is not a valid IPv4
  687. address. A <a class="reference internal" href="#ipaddress.NetmaskValueError" title="ipaddress.NetmaskValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NetmaskValueError</span></code></a> is raised if the mask is not valid for
  688. an IPv4 address.</p>
  689. <p>If <em>strict</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> and host bits are set in the supplied address,
  690. then <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised. Otherwise, the host bits are masked out
  691. to determine the appropriate network address.</p>
  692. <p>Unless stated otherwise, all network methods accepting other network/address
  693. objects will raise <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> if the argument’s IP version is
  694. incompatible to <code class="docutils literal notranslate"><span class="pre">self</span></code>.</p>
  695. <div class="versionchanged">
  696. <p><span class="versionmodified changed">Changed in version 3.5: </span>Added the two-tuple form for the <em>address</em> constructor parameter.</p>
  697. </div>
  698. <dl class="py attribute">
  699. <dt class="sig sig-object py" id="ipaddress.IPv4Network.version">
  700. <span class="sig-name descname"><span class="pre">version</span></span><a class="headerlink" href="#ipaddress.IPv4Network.version" title="Link to this definition">¶</a></dt>
  701. <dd></dd></dl>
  702. <dl class="py attribute">
  703. <dt class="sig sig-object py" id="ipaddress.IPv4Network.max_prefixlen">
  704. <span class="sig-name descname"><span class="pre">max_prefixlen</span></span><a class="headerlink" href="#ipaddress.IPv4Network.max_prefixlen" title="Link to this definition">¶</a></dt>
  705. <dd><p>Refer to the corresponding attribute documentation in
  706. <a class="reference internal" href="#ipaddress.IPv4Address" title="ipaddress.IPv4Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Address</span></code></a>.</p>
  707. </dd></dl>
  708. <dl class="py attribute">
  709. <dt class="sig sig-object py" id="ipaddress.IPv4Network.is_multicast">
  710. <span class="sig-name descname"><span class="pre">is_multicast</span></span><a class="headerlink" href="#ipaddress.IPv4Network.is_multicast" title="Link to this definition">¶</a></dt>
  711. <dd></dd></dl>
  712. <dl class="py attribute">
  713. <dt class="sig sig-object py" id="ipaddress.IPv4Network.is_private">
  714. <span class="sig-name descname"><span class="pre">is_private</span></span><a class="headerlink" href="#ipaddress.IPv4Network.is_private" title="Link to this definition">¶</a></dt>
  715. <dd></dd></dl>
  716. <dl class="py attribute">
  717. <dt class="sig sig-object py" id="ipaddress.IPv4Network.is_unspecified">
  718. <span class="sig-name descname"><span class="pre">is_unspecified</span></span><a class="headerlink" href="#ipaddress.IPv4Network.is_unspecified" title="Link to this definition">¶</a></dt>
  719. <dd></dd></dl>
  720. <dl class="py attribute">
  721. <dt class="sig sig-object py" id="ipaddress.IPv4Network.is_reserved">
  722. <span class="sig-name descname"><span class="pre">is_reserved</span></span><a class="headerlink" href="#ipaddress.IPv4Network.is_reserved" title="Link to this definition">¶</a></dt>
  723. <dd></dd></dl>
  724. <dl class="py attribute">
  725. <dt class="sig sig-object py" id="ipaddress.IPv4Network.is_loopback">
  726. <span class="sig-name descname"><span class="pre">is_loopback</span></span><a class="headerlink" href="#ipaddress.IPv4Network.is_loopback" title="Link to this definition">¶</a></dt>
  727. <dd></dd></dl>
  728. <dl class="py attribute">
  729. <dt class="sig sig-object py" id="ipaddress.IPv4Network.is_link_local">
  730. <span class="sig-name descname"><span class="pre">is_link_local</span></span><a class="headerlink" href="#ipaddress.IPv4Network.is_link_local" title="Link to this definition">¶</a></dt>
  731. <dd><p>These attributes are true for the network as a whole if they are true
  732. for both the network address and the broadcast address.</p>
  733. </dd></dl>
  734. <dl class="py attribute">
  735. <dt class="sig sig-object py" id="ipaddress.IPv4Network.network_address">
  736. <span class="sig-name descname"><span class="pre">network_address</span></span><a class="headerlink" href="#ipaddress.IPv4Network.network_address" title="Link to this definition">¶</a></dt>
  737. <dd><p>The network address for the network. The network address and the
  738. prefix length together uniquely define a network.</p>
  739. </dd></dl>
  740. <dl class="py attribute">
  741. <dt class="sig sig-object py" id="ipaddress.IPv4Network.broadcast_address">
  742. <span class="sig-name descname"><span class="pre">broadcast_address</span></span><a class="headerlink" href="#ipaddress.IPv4Network.broadcast_address" title="Link to this definition">¶</a></dt>
  743. <dd><p>The broadcast address for the network. Packets sent to the broadcast
  744. address should be received by every host on the network.</p>
  745. </dd></dl>
  746. <dl class="py attribute">
  747. <dt class="sig sig-object py" id="ipaddress.IPv4Network.hostmask">
  748. <span class="sig-name descname"><span class="pre">hostmask</span></span><a class="headerlink" href="#ipaddress.IPv4Network.hostmask" title="Link to this definition">¶</a></dt>
  749. <dd><p>The host mask, as an <a class="reference internal" href="#ipaddress.IPv4Address" title="ipaddress.IPv4Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Address</span></code></a> object.</p>
  750. </dd></dl>
  751. <dl class="py attribute">
  752. <dt class="sig sig-object py" id="ipaddress.IPv4Network.netmask">
  753. <span class="sig-name descname"><span class="pre">netmask</span></span><a class="headerlink" href="#ipaddress.IPv4Network.netmask" title="Link to this definition">¶</a></dt>
  754. <dd><p>The net mask, as an <a class="reference internal" href="#ipaddress.IPv4Address" title="ipaddress.IPv4Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Address</span></code></a> object.</p>
  755. </dd></dl>
  756. <dl class="py attribute">
  757. <dt class="sig sig-object py" id="ipaddress.IPv4Network.with_prefixlen">
  758. <span class="sig-name descname"><span class="pre">with_prefixlen</span></span><a class="headerlink" href="#ipaddress.IPv4Network.with_prefixlen" title="Link to this definition">¶</a></dt>
  759. <dd></dd></dl>
  760. <dl class="py attribute">
  761. <dt class="sig sig-object py" id="ipaddress.IPv4Network.compressed">
  762. <span class="sig-name descname"><span class="pre">compressed</span></span><a class="headerlink" href="#ipaddress.IPv4Network.compressed" title="Link to this definition">¶</a></dt>
  763. <dd></dd></dl>
  764. <dl class="py attribute">
  765. <dt class="sig sig-object py" id="ipaddress.IPv4Network.exploded">
  766. <span class="sig-name descname"><span class="pre">exploded</span></span><a class="headerlink" href="#ipaddress.IPv4Network.exploded" title="Link to this definition">¶</a></dt>
  767. <dd><p>A string representation of the network, with the mask in prefix
  768. notation.</p>
  769. <p><code class="docutils literal notranslate"><span class="pre">with_prefixlen</span></code> and <code class="docutils literal notranslate"><span class="pre">compressed</span></code> are always the same as
  770. <code class="docutils literal notranslate"><span class="pre">str(network)</span></code>.
  771. <code class="docutils literal notranslate"><span class="pre">exploded</span></code> uses the exploded form the network address.</p>
  772. </dd></dl>
  773. <dl class="py attribute">
  774. <dt class="sig sig-object py" id="ipaddress.IPv4Network.with_netmask">
  775. <span class="sig-name descname"><span class="pre">with_netmask</span></span><a class="headerlink" href="#ipaddress.IPv4Network.with_netmask" title="Link to this definition">¶</a></dt>
  776. <dd><p>A string representation of the network, with the mask in net mask
  777. notation.</p>
  778. </dd></dl>
  779. <dl class="py attribute">
  780. <dt class="sig sig-object py" id="ipaddress.IPv4Network.with_hostmask">
  781. <span class="sig-name descname"><span class="pre">with_hostmask</span></span><a class="headerlink" href="#ipaddress.IPv4Network.with_hostmask" title="Link to this definition">¶</a></dt>
  782. <dd><p>A string representation of the network, with the mask in host mask
  783. notation.</p>
  784. </dd></dl>
  785. <dl class="py attribute">
  786. <dt class="sig sig-object py" id="ipaddress.IPv4Network.num_addresses">
  787. <span class="sig-name descname"><span class="pre">num_addresses</span></span><a class="headerlink" href="#ipaddress.IPv4Network.num_addresses" title="Link to this definition">¶</a></dt>
  788. <dd><p>The total number of addresses in the network.</p>
  789. </dd></dl>
  790. <dl class="py attribute">
  791. <dt class="sig sig-object py" id="ipaddress.IPv4Network.prefixlen">
  792. <span class="sig-name descname"><span class="pre">prefixlen</span></span><a class="headerlink" href="#ipaddress.IPv4Network.prefixlen" title="Link to this definition">¶</a></dt>
  793. <dd><p>Length of the network prefix, in bits.</p>
  794. </dd></dl>
  795. <dl class="py method">
  796. <dt class="sig sig-object py" id="ipaddress.IPv4Network.hosts">
  797. <span class="sig-name descname"><span class="pre">hosts</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.IPv4Network.hosts" title="Link to this definition">¶</a></dt>
  798. <dd><p>Returns an iterator over the usable hosts in the network. The usable
  799. hosts are all the IP addresses that belong to the network, except the
  800. network address itself and the network broadcast address. For networks
  801. with a mask length of 31, the network address and network broadcast
  802. address are also included in the result. Networks with a mask of 32
  803. will return a list containing the single host address.</p>
  804. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/29&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">hosts</span><span class="p">())</span>
  805. <span class="go">[IPv4Address(&#39;192.0.2.1&#39;), IPv4Address(&#39;192.0.2.2&#39;),</span>
  806. <span class="go"> IPv4Address(&#39;192.0.2.3&#39;), IPv4Address(&#39;192.0.2.4&#39;),</span>
  807. <span class="go"> IPv4Address(&#39;192.0.2.5&#39;), IPv4Address(&#39;192.0.2.6&#39;)]</span>
  808. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/31&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">hosts</span><span class="p">())</span>
  809. <span class="go">[IPv4Address(&#39;192.0.2.0&#39;), IPv4Address(&#39;192.0.2.1&#39;)]</span>
  810. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.1/32&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">hosts</span><span class="p">())</span>
  811. <span class="go">[IPv4Address(&#39;192.0.2.1&#39;)]</span>
  812. </pre></div>
  813. </div>
  814. </dd></dl>
  815. <dl class="py method">
  816. <dt class="sig sig-object py" id="ipaddress.IPv4Network.overlaps">
  817. <span class="sig-name descname"><span class="pre">overlaps</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">other</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.IPv4Network.overlaps" title="Link to this definition">¶</a></dt>
  818. <dd><p><code class="docutils literal notranslate"><span class="pre">True</span></code> if this network is partly or wholly contained in <em>other</em> or
  819. <em>other</em> is wholly contained in this network.</p>
  820. </dd></dl>
  821. <dl class="py method">
  822. <dt class="sig sig-object py" id="ipaddress.IPv4Network.address_exclude">
  823. <span class="sig-name descname"><span class="pre">address_exclude</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">network</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.IPv4Network.address_exclude" title="Link to this definition">¶</a></dt>
  824. <dd><p>Computes the network definitions resulting from removing the given
  825. <em>network</em> from this one. Returns an iterator of network objects.
  826. Raises <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> if <em>network</em> is not completely contained in
  827. this network.</p>
  828. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">n1</span> <span class="o">=</span> <span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/28&#39;</span><span class="p">)</span>
  829. <span class="gp">&gt;&gt;&gt; </span><span class="n">n2</span> <span class="o">=</span> <span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.1/32&#39;</span><span class="p">)</span>
  830. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">n1</span><span class="o">.</span><span class="n">address_exclude</span><span class="p">(</span><span class="n">n2</span><span class="p">))</span>
  831. <span class="go">[IPv4Network(&#39;192.0.2.8/29&#39;), IPv4Network(&#39;192.0.2.4/30&#39;),</span>
  832. <span class="go"> IPv4Network(&#39;192.0.2.2/31&#39;), IPv4Network(&#39;192.0.2.0/32&#39;)]</span>
  833. </pre></div>
  834. </div>
  835. </dd></dl>
  836. <dl class="py method">
  837. <dt class="sig sig-object py" id="ipaddress.IPv4Network.subnets">
  838. <span class="sig-name descname"><span class="pre">subnets</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">prefixlen_diff</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">new_prefix</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="#ipaddress.IPv4Network.subnets" title="Link to this definition">¶</a></dt>
  839. <dd><p>The subnets that join to make the current network definition, depending
  840. on the argument values. <em>prefixlen_diff</em> is the amount our prefix
  841. length should be increased by. <em>new_prefix</em> is the desired new
  842. prefix of the subnets; it must be larger than our prefix. One and
  843. only one of <em>prefixlen_diff</em> and <em>new_prefix</em> must be set. Returns an
  844. iterator of network objects.</p>
  845. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/24&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">subnets</span><span class="p">())</span>
  846. <span class="go">[IPv4Network(&#39;192.0.2.0/25&#39;), IPv4Network(&#39;192.0.2.128/25&#39;)]</span>
  847. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/24&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">subnets</span><span class="p">(</span><span class="n">prefixlen_diff</span><span class="o">=</span><span class="mi">2</span><span class="p">))</span>
  848. <span class="go">[IPv4Network(&#39;192.0.2.0/26&#39;), IPv4Network(&#39;192.0.2.64/26&#39;),</span>
  849. <span class="go"> IPv4Network(&#39;192.0.2.128/26&#39;), IPv4Network(&#39;192.0.2.192/26&#39;)]</span>
  850. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/24&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">subnets</span><span class="p">(</span><span class="n">new_prefix</span><span class="o">=</span><span class="mi">26</span><span class="p">))</span>
  851. <span class="go">[IPv4Network(&#39;192.0.2.0/26&#39;), IPv4Network(&#39;192.0.2.64/26&#39;),</span>
  852. <span class="go"> IPv4Network(&#39;192.0.2.128/26&#39;), IPv4Network(&#39;192.0.2.192/26&#39;)]</span>
  853. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/24&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">subnets</span><span class="p">(</span><span class="n">new_prefix</span><span class="o">=</span><span class="mi">23</span><span class="p">))</span>
  854. <span class="gt">Traceback (most recent call last):</span>
  855. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
  856. <span class="w"> </span><span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;new prefix must be longer&#39;</span><span class="p">)</span>
  857. <span class="gr">ValueError</span>: <span class="n">new prefix must be longer</span>
  858. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/24&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">subnets</span><span class="p">(</span><span class="n">new_prefix</span><span class="o">=</span><span class="mi">25</span><span class="p">))</span>
  859. <span class="go">[IPv4Network(&#39;192.0.2.0/25&#39;), IPv4Network(&#39;192.0.2.128/25&#39;)]</span>
  860. </pre></div>
  861. </div>
  862. </dd></dl>
  863. <dl class="py method">
  864. <dt class="sig sig-object py" id="ipaddress.IPv4Network.supernet">
  865. <span class="sig-name descname"><span class="pre">supernet</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">prefixlen_diff</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">new_prefix</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="#ipaddress.IPv4Network.supernet" title="Link to this definition">¶</a></dt>
  866. <dd><p>The supernet containing this network definition, depending on the
  867. argument values. <em>prefixlen_diff</em> is the amount our prefix length
  868. should be decreased by. <em>new_prefix</em> is the desired new prefix of
  869. the supernet; it must be smaller than our prefix. One and only one
  870. of <em>prefixlen_diff</em> and <em>new_prefix</em> must be set. Returns a single
  871. network object.</p>
  872. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/24&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">supernet</span><span class="p">()</span>
  873. <span class="go">IPv4Network(&#39;192.0.2.0/23&#39;)</span>
  874. <span class="gp">&gt;&gt;&gt; </span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/24&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">supernet</span><span class="p">(</span><span class="n">prefixlen_diff</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
  875. <span class="go">IPv4Network(&#39;192.0.0.0/22&#39;)</span>
  876. <span class="gp">&gt;&gt;&gt; </span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/24&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">supernet</span><span class="p">(</span><span class="n">new_prefix</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>
  877. <span class="go">IPv4Network(&#39;192.0.0.0/20&#39;)</span>
  878. </pre></div>
  879. </div>
  880. </dd></dl>
  881. <dl class="py method">
  882. <dt class="sig sig-object py" id="ipaddress.IPv4Network.subnet_of">
  883. <span class="sig-name descname"><span class="pre">subnet_of</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">other</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.IPv4Network.subnet_of" title="Link to this definition">¶</a></dt>
  884. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if this network is a subnet of <em>other</em>.</p>
  885. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.168.1.0/24&#39;</span><span class="p">)</span>
  886. <span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.168.1.128/30&#39;</span><span class="p">)</span>
  887. <span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">subnet_of</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
  888. <span class="go">True</span>
  889. </pre></div>
  890. </div>
  891. <div class="versionadded">
  892. <p><span class="versionmodified added">New in version 3.7.</span></p>
  893. </div>
  894. </dd></dl>
  895. <dl class="py method">
  896. <dt class="sig sig-object py" id="ipaddress.IPv4Network.supernet_of">
  897. <span class="sig-name descname"><span class="pre">supernet_of</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">other</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.IPv4Network.supernet_of" title="Link to this definition">¶</a></dt>
  898. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if this network is a supernet of <em>other</em>.</p>
  899. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.168.1.0/24&#39;</span><span class="p">)</span>
  900. <span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.168.1.128/30&#39;</span><span class="p">)</span>
  901. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">supernet_of</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
  902. <span class="go">True</span>
  903. </pre></div>
  904. </div>
  905. <div class="versionadded">
  906. <p><span class="versionmodified added">New in version 3.7.</span></p>
  907. </div>
  908. </dd></dl>
  909. <dl class="py method">
  910. <dt class="sig sig-object py" id="ipaddress.IPv4Network.compare_networks">
  911. <span class="sig-name descname"><span class="pre">compare_networks</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">other</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.IPv4Network.compare_networks" title="Link to this definition">¶</a></dt>
  912. <dd><p>Compare this network to <em>other</em>. In this comparison only the network
  913. addresses are considered; host bits aren’t. Returns either <code class="docutils literal notranslate"><span class="pre">-1</span></code>,
  914. <code class="docutils literal notranslate"><span class="pre">0</span></code> or <code class="docutils literal notranslate"><span class="pre">1</span></code>.</p>
  915. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.1/32&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">compare_networks</span><span class="p">(</span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.2/32&#39;</span><span class="p">))</span>
  916. <span class="go">-1</span>
  917. <span class="gp">&gt;&gt;&gt; </span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.1/32&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">compare_networks</span><span class="p">(</span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/32&#39;</span><span class="p">))</span>
  918. <span class="go">1</span>
  919. <span class="gp">&gt;&gt;&gt; </span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.1/32&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">compare_networks</span><span class="p">(</span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.1/32&#39;</span><span class="p">))</span>
  920. <span class="go">0</span>
  921. </pre></div>
  922. </div>
  923. <div class="deprecated">
  924. <p><span class="versionmodified deprecated">Deprecated since version 3.7: </span>It uses the same ordering and comparison algorithm as “&lt;”, “==”, and “&gt;”</p>
  925. </div>
  926. </dd></dl>
  927. </dd></dl>
  928. <dl class="py class">
  929. <dt class="sig sig-object py" id="ipaddress.IPv6Network">
  930. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ipaddress.</span></span><span class="sig-name descname"><span class="pre">IPv6Network</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">address</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">strict</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.IPv6Network" title="Link to this definition">¶</a></dt>
  931. <dd><p>Construct an IPv6 network definition. <em>address</em> can be one of the following:</p>
  932. <ol class="arabic">
  933. <li><p>A string consisting of an IP address and an optional prefix length,
  934. separated by a slash (<code class="docutils literal notranslate"><span class="pre">/</span></code>). The IP address is the network address,
  935. and the prefix length must be a single number, the <em>prefix</em>. If no
  936. prefix length is provided, it’s considered to be <code class="docutils literal notranslate"><span class="pre">/128</span></code>.</p>
  937. <p>Note that currently expanded netmasks are not supported. That means
  938. <code class="docutils literal notranslate"><span class="pre">2001:db00::0/24</span></code> is a valid argument while <code class="docutils literal notranslate"><span class="pre">2001:db00::0/ffff:ff00::</span></code>
  939. is not.</p>
  940. </li>
  941. <li><p>An integer that fits into 128 bits. This is equivalent to a
  942. single-address network, with the network address being <em>address</em> and
  943. the mask being <code class="docutils literal notranslate"><span class="pre">/128</span></code>.</p></li>
  944. <li><p>An integer packed into 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 of length 16, big-endian.
  945. The interpretation is similar to an integer <em>address</em>.</p></li>
  946. <li><p>A two-tuple of an address description and a netmask, where the address
  947. description is either a string, a 128-bits integer, a 16-bytes packed
  948. integer, or an existing IPv6Address object; and the netmask is an
  949. integer representing the prefix length.</p></li>
  950. </ol>
  951. <p>An <a class="reference internal" href="#ipaddress.AddressValueError" title="ipaddress.AddressValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AddressValueError</span></code></a> is raised if <em>address</em> is not a valid IPv6
  952. address. A <a class="reference internal" href="#ipaddress.NetmaskValueError" title="ipaddress.NetmaskValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NetmaskValueError</span></code></a> is raised if the mask is not valid for
  953. an IPv6 address.</p>
  954. <p>If <em>strict</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> and host bits are set in the supplied address,
  955. then <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised. Otherwise, the host bits are masked out
  956. to determine the appropriate network address.</p>
  957. <div class="versionchanged">
  958. <p><span class="versionmodified changed">Changed in version 3.5: </span>Added the two-tuple form for the <em>address</em> constructor parameter.</p>
  959. </div>
  960. <dl class="py attribute">
  961. <dt class="sig sig-object py" id="ipaddress.IPv6Network.version">
  962. <span class="sig-name descname"><span class="pre">version</span></span><a class="headerlink" href="#ipaddress.IPv6Network.version" title="Link to this definition">¶</a></dt>
  963. <dd></dd></dl>
  964. <dl class="py attribute">
  965. <dt class="sig sig-object py" id="ipaddress.IPv6Network.max_prefixlen">
  966. <span class="sig-name descname"><span class="pre">max_prefixlen</span></span><a class="headerlink" href="#ipaddress.IPv6Network.max_prefixlen" title="Link to this definition">¶</a></dt>
  967. <dd></dd></dl>
  968. <dl class="py attribute">
  969. <dt class="sig sig-object py" id="ipaddress.IPv6Network.is_multicast">
  970. <span class="sig-name descname"><span class="pre">is_multicast</span></span><a class="headerlink" href="#ipaddress.IPv6Network.is_multicast" title="Link to this definition">¶</a></dt>
  971. <dd></dd></dl>
  972. <dl class="py attribute">
  973. <dt class="sig sig-object py" id="ipaddress.IPv6Network.is_private">
  974. <span class="sig-name descname"><span class="pre">is_private</span></span><a class="headerlink" href="#ipaddress.IPv6Network.is_private" title="Link to this definition">¶</a></dt>
  975. <dd></dd></dl>
  976. <dl class="py attribute">
  977. <dt class="sig sig-object py" id="ipaddress.IPv6Network.is_unspecified">
  978. <span class="sig-name descname"><span class="pre">is_unspecified</span></span><a class="headerlink" href="#ipaddress.IPv6Network.is_unspecified" title="Link to this definition">¶</a></dt>
  979. <dd></dd></dl>
  980. <dl class="py attribute">
  981. <dt class="sig sig-object py" id="ipaddress.IPv6Network.is_reserved">
  982. <span class="sig-name descname"><span class="pre">is_reserved</span></span><a class="headerlink" href="#ipaddress.IPv6Network.is_reserved" title="Link to this definition">¶</a></dt>
  983. <dd></dd></dl>
  984. <dl class="py attribute">
  985. <dt class="sig sig-object py" id="ipaddress.IPv6Network.is_loopback">
  986. <span class="sig-name descname"><span class="pre">is_loopback</span></span><a class="headerlink" href="#ipaddress.IPv6Network.is_loopback" title="Link to this definition">¶</a></dt>
  987. <dd></dd></dl>
  988. <dl class="py attribute">
  989. <dt class="sig sig-object py" id="ipaddress.IPv6Network.is_link_local">
  990. <span class="sig-name descname"><span class="pre">is_link_local</span></span><a class="headerlink" href="#ipaddress.IPv6Network.is_link_local" title="Link to this definition">¶</a></dt>
  991. <dd></dd></dl>
  992. <dl class="py attribute">
  993. <dt class="sig sig-object py" id="ipaddress.IPv6Network.network_address">
  994. <span class="sig-name descname"><span class="pre">network_address</span></span><a class="headerlink" href="#ipaddress.IPv6Network.network_address" title="Link to this definition">¶</a></dt>
  995. <dd></dd></dl>
  996. <dl class="py attribute">
  997. <dt class="sig sig-object py" id="ipaddress.IPv6Network.broadcast_address">
  998. <span class="sig-name descname"><span class="pre">broadcast_address</span></span><a class="headerlink" href="#ipaddress.IPv6Network.broadcast_address" title="Link to this definition">¶</a></dt>
  999. <dd></dd></dl>
  1000. <dl class="py attribute">
  1001. <dt class="sig sig-object py" id="ipaddress.IPv6Network.hostmask">
  1002. <span class="sig-name descname"><span class="pre">hostmask</span></span><a class="headerlink" href="#ipaddress.IPv6Network.hostmask" title="Link to this definition">¶</a></dt>
  1003. <dd></dd></dl>
  1004. <dl class="py attribute">
  1005. <dt class="sig sig-object py" id="ipaddress.IPv6Network.netmask">
  1006. <span class="sig-name descname"><span class="pre">netmask</span></span><a class="headerlink" href="#ipaddress.IPv6Network.netmask" title="Link to this definition">¶</a></dt>
  1007. <dd></dd></dl>
  1008. <dl class="py attribute">
  1009. <dt class="sig sig-object py" id="ipaddress.IPv6Network.with_prefixlen">
  1010. <span class="sig-name descname"><span class="pre">with_prefixlen</span></span><a class="headerlink" href="#ipaddress.IPv6Network.with_prefixlen" title="Link to this definition">¶</a></dt>
  1011. <dd></dd></dl>
  1012. <dl class="py attribute">
  1013. <dt class="sig sig-object py" id="ipaddress.IPv6Network.compressed">
  1014. <span class="sig-name descname"><span class="pre">compressed</span></span><a class="headerlink" href="#ipaddress.IPv6Network.compressed" title="Link to this definition">¶</a></dt>
  1015. <dd></dd></dl>
  1016. <dl class="py attribute">
  1017. <dt class="sig sig-object py" id="ipaddress.IPv6Network.exploded">
  1018. <span class="sig-name descname"><span class="pre">exploded</span></span><a class="headerlink" href="#ipaddress.IPv6Network.exploded" title="Link to this definition">¶</a></dt>
  1019. <dd></dd></dl>
  1020. <dl class="py attribute">
  1021. <dt class="sig sig-object py" id="ipaddress.IPv6Network.with_netmask">
  1022. <span class="sig-name descname"><span class="pre">with_netmask</span></span><a class="headerlink" href="#ipaddress.IPv6Network.with_netmask" title="Link to this definition">¶</a></dt>
  1023. <dd></dd></dl>
  1024. <dl class="py attribute">
  1025. <dt class="sig sig-object py" id="ipaddress.IPv6Network.with_hostmask">
  1026. <span class="sig-name descname"><span class="pre">with_hostmask</span></span><a class="headerlink" href="#ipaddress.IPv6Network.with_hostmask" title="Link to this definition">¶</a></dt>
  1027. <dd></dd></dl>
  1028. <dl class="py attribute">
  1029. <dt class="sig sig-object py" id="ipaddress.IPv6Network.num_addresses">
  1030. <span class="sig-name descname"><span class="pre">num_addresses</span></span><a class="headerlink" href="#ipaddress.IPv6Network.num_addresses" title="Link to this definition">¶</a></dt>
  1031. <dd></dd></dl>
  1032. <dl class="py attribute">
  1033. <dt class="sig sig-object py" id="ipaddress.IPv6Network.prefixlen">
  1034. <span class="sig-name descname"><span class="pre">prefixlen</span></span><a class="headerlink" href="#ipaddress.IPv6Network.prefixlen" title="Link to this definition">¶</a></dt>
  1035. <dd></dd></dl>
  1036. <dl class="py method">
  1037. <dt class="sig sig-object py" id="ipaddress.IPv6Network.hosts">
  1038. <span class="sig-name descname"><span class="pre">hosts</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.IPv6Network.hosts" title="Link to this definition">¶</a></dt>
  1039. <dd><p>Returns an iterator over the usable hosts in the network. The usable
  1040. hosts are all the IP addresses that belong to the network, except the
  1041. Subnet-Router anycast address. For networks with a mask length of 127,
  1042. the Subnet-Router anycast address is also included in the result.
  1043. Networks with a mask of 128 will return a list containing the
  1044. single host address.</p>
  1045. </dd></dl>
  1046. <dl class="py method">
  1047. <dt class="sig sig-object py" id="ipaddress.IPv6Network.overlaps">
  1048. <span class="sig-name descname"><span class="pre">overlaps</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">other</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.IPv6Network.overlaps" title="Link to this definition">¶</a></dt>
  1049. <dd></dd></dl>
  1050. <dl class="py method">
  1051. <dt class="sig sig-object py" id="ipaddress.IPv6Network.address_exclude">
  1052. <span class="sig-name descname"><span class="pre">address_exclude</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">network</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.IPv6Network.address_exclude" title="Link to this definition">¶</a></dt>
  1053. <dd></dd></dl>
  1054. <dl class="py method">
  1055. <dt class="sig sig-object py" id="ipaddress.IPv6Network.subnets">
  1056. <span class="sig-name descname"><span class="pre">subnets</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">prefixlen_diff</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">new_prefix</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="#ipaddress.IPv6Network.subnets" title="Link to this definition">¶</a></dt>
  1057. <dd></dd></dl>
  1058. <dl class="py method">
  1059. <dt class="sig sig-object py" id="ipaddress.IPv6Network.supernet">
  1060. <span class="sig-name descname"><span class="pre">supernet</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">prefixlen_diff</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">new_prefix</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="#ipaddress.IPv6Network.supernet" title="Link to this definition">¶</a></dt>
  1061. <dd></dd></dl>
  1062. <dl class="py method">
  1063. <dt class="sig sig-object py" id="ipaddress.IPv6Network.subnet_of">
  1064. <span class="sig-name descname"><span class="pre">subnet_of</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">other</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.IPv6Network.subnet_of" title="Link to this definition">¶</a></dt>
  1065. <dd></dd></dl>
  1066. <dl class="py method">
  1067. <dt class="sig sig-object py" id="ipaddress.IPv6Network.supernet_of">
  1068. <span class="sig-name descname"><span class="pre">supernet_of</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">other</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.IPv6Network.supernet_of" title="Link to this definition">¶</a></dt>
  1069. <dd></dd></dl>
  1070. <dl class="py method">
  1071. <dt class="sig sig-object py" id="ipaddress.IPv6Network.compare_networks">
  1072. <span class="sig-name descname"><span class="pre">compare_networks</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">other</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.IPv6Network.compare_networks" title="Link to this definition">¶</a></dt>
  1073. <dd><p>Refer to the corresponding attribute documentation in
  1074. <a class="reference internal" href="#ipaddress.IPv4Network" title="ipaddress.IPv4Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Network</span></code></a>.</p>
  1075. </dd></dl>
  1076. <dl class="py attribute">
  1077. <dt class="sig sig-object py" id="ipaddress.IPv6Network.is_site_local">
  1078. <span class="sig-name descname"><span class="pre">is_site_local</span></span><a class="headerlink" href="#ipaddress.IPv6Network.is_site_local" title="Link to this definition">¶</a></dt>
  1079. <dd><p>These attribute is true for the network as a whole if it is true
  1080. for both the network address and the broadcast address.</p>
  1081. </dd></dl>
  1082. </dd></dl>
  1083. </section>
  1084. <section id="id1">
  1085. <h3>Operators<a class="headerlink" href="#id1" title="Link to this heading">¶</a></h3>
  1086. <p>Network objects support some operators. Unless stated otherwise, operators can
  1087. only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with
  1088. IPv6).</p>
  1089. <section id="logical-operators">
  1090. <h4>Logical operators<a class="headerlink" href="#logical-operators" title="Link to this heading">¶</a></h4>
  1091. <p>Network objects can be compared with the usual set of logical operators.
  1092. Network objects are ordered first by network address, then by net mask.</p>
  1093. </section>
  1094. <section id="iteration">
  1095. <h4>Iteration<a class="headerlink" href="#iteration" title="Link to this heading">¶</a></h4>
  1096. <p>Network objects can be iterated to list all the addresses belonging to the
  1097. network. For iteration, <em>all</em> hosts are returned, including unusable hosts
  1098. (for usable hosts, use the <a class="reference internal" href="#ipaddress.IPv4Network.hosts" title="ipaddress.IPv4Network.hosts"><code class="xref py py-meth docutils literal notranslate"><span class="pre">hosts()</span></code></a> method). An
  1099. example:</p>
  1100. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">addr</span> <span class="ow">in</span> <span class="n">IPv4Network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/28&#39;</span><span class="p">):</span>
  1101. <span class="gp">... </span> <span class="n">addr</span>
  1102. <span class="gp">...</span>
  1103. <span class="go">IPv4Address(&#39;192.0.2.0&#39;)</span>
  1104. <span class="go">IPv4Address(&#39;192.0.2.1&#39;)</span>
  1105. <span class="go">IPv4Address(&#39;192.0.2.2&#39;)</span>
  1106. <span class="go">IPv4Address(&#39;192.0.2.3&#39;)</span>
  1107. <span class="go">IPv4Address(&#39;192.0.2.4&#39;)</span>
  1108. <span class="go">IPv4Address(&#39;192.0.2.5&#39;)</span>
  1109. <span class="go">IPv4Address(&#39;192.0.2.6&#39;)</span>
  1110. <span class="go">IPv4Address(&#39;192.0.2.7&#39;)</span>
  1111. <span class="go">IPv4Address(&#39;192.0.2.8&#39;)</span>
  1112. <span class="go">IPv4Address(&#39;192.0.2.9&#39;)</span>
  1113. <span class="go">IPv4Address(&#39;192.0.2.10&#39;)</span>
  1114. <span class="go">IPv4Address(&#39;192.0.2.11&#39;)</span>
  1115. <span class="go">IPv4Address(&#39;192.0.2.12&#39;)</span>
  1116. <span class="go">IPv4Address(&#39;192.0.2.13&#39;)</span>
  1117. <span class="go">IPv4Address(&#39;192.0.2.14&#39;)</span>
  1118. <span class="go">IPv4Address(&#39;192.0.2.15&#39;)</span>
  1119. </pre></div>
  1120. </div>
  1121. </section>
  1122. <section id="networks-as-containers-of-addresses">
  1123. <h4>Networks as containers of addresses<a class="headerlink" href="#networks-as-containers-of-addresses" title="Link to this heading">¶</a></h4>
  1124. <p>Network objects can act as containers of addresses. Some examples:</p>
  1125. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">IPv4Network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/28&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
  1126. <span class="go">IPv4Address(&#39;192.0.2.0&#39;)</span>
  1127. <span class="gp">&gt;&gt;&gt; </span><span class="n">IPv4Network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/28&#39;</span><span class="p">)[</span><span class="mi">15</span><span class="p">]</span>
  1128. <span class="go">IPv4Address(&#39;192.0.2.15&#39;)</span>
  1129. <span class="gp">&gt;&gt;&gt; </span><span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;192.0.2.6&#39;</span><span class="p">)</span> <span class="ow">in</span> <span class="n">IPv4Network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/28&#39;</span><span class="p">)</span>
  1130. <span class="go">True</span>
  1131. <span class="gp">&gt;&gt;&gt; </span><span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;192.0.3.6&#39;</span><span class="p">)</span> <span class="ow">in</span> <span class="n">IPv4Network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/28&#39;</span><span class="p">)</span>
  1132. <span class="go">False</span>
  1133. </pre></div>
  1134. </div>
  1135. </section>
  1136. </section>
  1137. </section>
  1138. <section id="interface-objects">
  1139. <h2>Interface objects<a class="headerlink" href="#interface-objects" title="Link to this heading">¶</a></h2>
  1140. <p>Interface objects are <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a>, so they can be used as keys in
  1141. dictionaries.</p>
  1142. <dl class="py class">
  1143. <dt class="sig sig-object py" id="ipaddress.IPv4Interface">
  1144. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ipaddress.</span></span><span class="sig-name descname"><span class="pre">IPv4Interface</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="#ipaddress.IPv4Interface" title="Link to this definition">¶</a></dt>
  1145. <dd><p>Construct an IPv4 interface. The meaning of <em>address</em> is as in the
  1146. constructor of <a class="reference internal" href="#ipaddress.IPv4Network" title="ipaddress.IPv4Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Network</span></code></a>, except that arbitrary host addresses
  1147. are always accepted.</p>
  1148. <p><a class="reference internal" href="#ipaddress.IPv4Interface" title="ipaddress.IPv4Interface"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Interface</span></code></a> is a subclass of <a class="reference internal" href="#ipaddress.IPv4Address" title="ipaddress.IPv4Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Address</span></code></a>, so it inherits
  1149. all the attributes from that class. In addition, the following attributes
  1150. are available:</p>
  1151. <dl class="py attribute">
  1152. <dt class="sig sig-object py" id="ipaddress.IPv4Interface.ip">
  1153. <span class="sig-name descname"><span class="pre">ip</span></span><a class="headerlink" href="#ipaddress.IPv4Interface.ip" title="Link to this definition">¶</a></dt>
  1154. <dd><p>The address (<a class="reference internal" href="#ipaddress.IPv4Address" title="ipaddress.IPv4Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Address</span></code></a>) without network information.</p>
  1155. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">interface</span> <span class="o">=</span> <span class="n">IPv4Interface</span><span class="p">(</span><span class="s1">&#39;192.0.2.5/24&#39;</span><span class="p">)</span>
  1156. <span class="gp">&gt;&gt;&gt; </span><span class="n">interface</span><span class="o">.</span><span class="n">ip</span>
  1157. <span class="go">IPv4Address(&#39;192.0.2.5&#39;)</span>
  1158. </pre></div>
  1159. </div>
  1160. </dd></dl>
  1161. <dl class="py attribute">
  1162. <dt class="sig sig-object py" id="ipaddress.IPv4Interface.network">
  1163. <span class="sig-name descname"><span class="pre">network</span></span><a class="headerlink" href="#ipaddress.IPv4Interface.network" title="Link to this definition">¶</a></dt>
  1164. <dd><p>The network (<a class="reference internal" href="#ipaddress.IPv4Network" title="ipaddress.IPv4Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Network</span></code></a>) this interface belongs to.</p>
  1165. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">interface</span> <span class="o">=</span> <span class="n">IPv4Interface</span><span class="p">(</span><span class="s1">&#39;192.0.2.5/24&#39;</span><span class="p">)</span>
  1166. <span class="gp">&gt;&gt;&gt; </span><span class="n">interface</span><span class="o">.</span><span class="n">network</span>
  1167. <span class="go">IPv4Network(&#39;192.0.2.0/24&#39;)</span>
  1168. </pre></div>
  1169. </div>
  1170. </dd></dl>
  1171. <dl class="py attribute">
  1172. <dt class="sig sig-object py" id="ipaddress.IPv4Interface.with_prefixlen">
  1173. <span class="sig-name descname"><span class="pre">with_prefixlen</span></span><a class="headerlink" href="#ipaddress.IPv4Interface.with_prefixlen" title="Link to this definition">¶</a></dt>
  1174. <dd><p>A string representation of the interface with the mask in prefix notation.</p>
  1175. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">interface</span> <span class="o">=</span> <span class="n">IPv4Interface</span><span class="p">(</span><span class="s1">&#39;192.0.2.5/24&#39;</span><span class="p">)</span>
  1176. <span class="gp">&gt;&gt;&gt; </span><span class="n">interface</span><span class="o">.</span><span class="n">with_prefixlen</span>
  1177. <span class="go">&#39;192.0.2.5/24&#39;</span>
  1178. </pre></div>
  1179. </div>
  1180. </dd></dl>
  1181. <dl class="py attribute">
  1182. <dt class="sig sig-object py" id="ipaddress.IPv4Interface.with_netmask">
  1183. <span class="sig-name descname"><span class="pre">with_netmask</span></span><a class="headerlink" href="#ipaddress.IPv4Interface.with_netmask" title="Link to this definition">¶</a></dt>
  1184. <dd><p>A string representation of the interface with the network as a net mask.</p>
  1185. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">interface</span> <span class="o">=</span> <span class="n">IPv4Interface</span><span class="p">(</span><span class="s1">&#39;192.0.2.5/24&#39;</span><span class="p">)</span>
  1186. <span class="gp">&gt;&gt;&gt; </span><span class="n">interface</span><span class="o">.</span><span class="n">with_netmask</span>
  1187. <span class="go">&#39;192.0.2.5/255.255.255.0&#39;</span>
  1188. </pre></div>
  1189. </div>
  1190. </dd></dl>
  1191. <dl class="py attribute">
  1192. <dt class="sig sig-object py" id="ipaddress.IPv4Interface.with_hostmask">
  1193. <span class="sig-name descname"><span class="pre">with_hostmask</span></span><a class="headerlink" href="#ipaddress.IPv4Interface.with_hostmask" title="Link to this definition">¶</a></dt>
  1194. <dd><p>A string representation of the interface with the network as a host mask.</p>
  1195. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">interface</span> <span class="o">=</span> <span class="n">IPv4Interface</span><span class="p">(</span><span class="s1">&#39;192.0.2.5/24&#39;</span><span class="p">)</span>
  1196. <span class="gp">&gt;&gt;&gt; </span><span class="n">interface</span><span class="o">.</span><span class="n">with_hostmask</span>
  1197. <span class="go">&#39;192.0.2.5/0.0.0.255&#39;</span>
  1198. </pre></div>
  1199. </div>
  1200. </dd></dl>
  1201. </dd></dl>
  1202. <dl class="py class">
  1203. <dt class="sig sig-object py" id="ipaddress.IPv6Interface">
  1204. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ipaddress.</span></span><span class="sig-name descname"><span class="pre">IPv6Interface</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="#ipaddress.IPv6Interface" title="Link to this definition">¶</a></dt>
  1205. <dd><p>Construct an IPv6 interface. The meaning of <em>address</em> is as in the
  1206. constructor of <a class="reference internal" href="#ipaddress.IPv6Network" title="ipaddress.IPv6Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv6Network</span></code></a>, except that arbitrary host addresses
  1207. are always accepted.</p>
  1208. <p><a class="reference internal" href="#ipaddress.IPv6Interface" title="ipaddress.IPv6Interface"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv6Interface</span></code></a> is a subclass of <a class="reference internal" href="#ipaddress.IPv6Address" title="ipaddress.IPv6Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv6Address</span></code></a>, so it inherits
  1209. all the attributes from that class. In addition, the following attributes
  1210. are available:</p>
  1211. <dl class="py attribute">
  1212. <dt class="sig sig-object py" id="ipaddress.IPv6Interface.ip">
  1213. <span class="sig-name descname"><span class="pre">ip</span></span><a class="headerlink" href="#ipaddress.IPv6Interface.ip" title="Link to this definition">¶</a></dt>
  1214. <dd></dd></dl>
  1215. <dl class="py attribute">
  1216. <dt class="sig sig-object py" id="ipaddress.IPv6Interface.network">
  1217. <span class="sig-name descname"><span class="pre">network</span></span><a class="headerlink" href="#ipaddress.IPv6Interface.network" title="Link to this definition">¶</a></dt>
  1218. <dd></dd></dl>
  1219. <dl class="py attribute">
  1220. <dt class="sig sig-object py" id="ipaddress.IPv6Interface.with_prefixlen">
  1221. <span class="sig-name descname"><span class="pre">with_prefixlen</span></span><a class="headerlink" href="#ipaddress.IPv6Interface.with_prefixlen" title="Link to this definition">¶</a></dt>
  1222. <dd></dd></dl>
  1223. <dl class="py attribute">
  1224. <dt class="sig sig-object py" id="ipaddress.IPv6Interface.with_netmask">
  1225. <span class="sig-name descname"><span class="pre">with_netmask</span></span><a class="headerlink" href="#ipaddress.IPv6Interface.with_netmask" title="Link to this definition">¶</a></dt>
  1226. <dd></dd></dl>
  1227. <dl class="py attribute">
  1228. <dt class="sig sig-object py" id="ipaddress.IPv6Interface.with_hostmask">
  1229. <span class="sig-name descname"><span class="pre">with_hostmask</span></span><a class="headerlink" href="#ipaddress.IPv6Interface.with_hostmask" title="Link to this definition">¶</a></dt>
  1230. <dd><p>Refer to the corresponding attribute documentation in
  1231. <a class="reference internal" href="#ipaddress.IPv4Interface" title="ipaddress.IPv4Interface"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Interface</span></code></a>.</p>
  1232. </dd></dl>
  1233. </dd></dl>
  1234. <section id="id2">
  1235. <h3>Operators<a class="headerlink" href="#id2" title="Link to this heading">¶</a></h3>
  1236. <p>Interface objects support some operators. Unless stated otherwise, operators
  1237. can only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with
  1238. IPv6).</p>
  1239. <section id="id3">
  1240. <h4>Logical operators<a class="headerlink" href="#id3" title="Link to this heading">¶</a></h4>
  1241. <p>Interface objects can be compared with the usual set of logical operators.</p>
  1242. <p>For equality comparison (<code class="docutils literal notranslate"><span class="pre">==</span></code> and <code class="docutils literal notranslate"><span class="pre">!=</span></code>), both the IP address and network
  1243. must be the same for the objects to be equal. An interface will not compare
  1244. equal to any address or network object.</p>
  1245. <p>For ordering (<code class="docutils literal notranslate"><span class="pre">&lt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;</span></code>, etc) the rules are different. Interface and
  1246. address objects with the same IP version can be compared, and the address
  1247. objects will always sort before the interface objects. Two interface objects
  1248. are first compared by their networks and, if those are the same, then by their
  1249. IP addresses.</p>
  1250. </section>
  1251. </section>
  1252. </section>
  1253. <section id="other-module-level-functions">
  1254. <h2>Other Module Level Functions<a class="headerlink" href="#other-module-level-functions" title="Link to this heading">¶</a></h2>
  1255. <p>The module also provides the following module level functions:</p>
  1256. <dl class="py function">
  1257. <dt class="sig sig-object py" id="ipaddress.v4_int_to_packed">
  1258. <span class="sig-prename descclassname"><span class="pre">ipaddress.</span></span><span class="sig-name descname"><span class="pre">v4_int_to_packed</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="#ipaddress.v4_int_to_packed" title="Link to this definition">¶</a></dt>
  1259. <dd><p>Represent an address as 4 packed bytes in network (big-endian) order.
  1260. <em>address</em> is an integer representation of an IPv4 IP address. A
  1261. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised if the integer is negative or too large to be an
  1262. IPv4 IP address.</p>
  1263. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ipaddress</span><span class="o">.</span><span class="n">ip_address</span><span class="p">(</span><span class="mi">3221225985</span><span class="p">)</span>
  1264. <span class="go">IPv4Address(&#39;192.0.2.1&#39;)</span>
  1265. <span class="gp">&gt;&gt;&gt; </span><span class="n">ipaddress</span><span class="o">.</span><span class="n">v4_int_to_packed</span><span class="p">(</span><span class="mi">3221225985</span><span class="p">)</span>
  1266. <span class="go">b&#39;\xc0\x00\x02\x01&#39;</span>
  1267. </pre></div>
  1268. </div>
  1269. </dd></dl>
  1270. <dl class="py function">
  1271. <dt class="sig sig-object py" id="ipaddress.v6_int_to_packed">
  1272. <span class="sig-prename descclassname"><span class="pre">ipaddress.</span></span><span class="sig-name descname"><span class="pre">v6_int_to_packed</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="#ipaddress.v6_int_to_packed" title="Link to this definition">¶</a></dt>
  1273. <dd><p>Represent an address as 16 packed bytes in network (big-endian) order.
  1274. <em>address</em> is an integer representation of an IPv6 IP address. A
  1275. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised if the integer is negative or too large to be an
  1276. IPv6 IP address.</p>
  1277. </dd></dl>
  1278. <dl class="py function">
  1279. <dt class="sig sig-object py" id="ipaddress.summarize_address_range">
  1280. <span class="sig-prename descclassname"><span class="pre">ipaddress.</span></span><span class="sig-name descname"><span class="pre">summarize_address_range</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">first</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">last</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.summarize_address_range" title="Link to this definition">¶</a></dt>
  1281. <dd><p>Return an iterator of the summarized network range given the first and last
  1282. IP addresses. <em>first</em> is the first <a class="reference internal" href="#ipaddress.IPv4Address" title="ipaddress.IPv4Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Address</span></code></a> or
  1283. <a class="reference internal" href="#ipaddress.IPv6Address" title="ipaddress.IPv6Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv6Address</span></code></a> in the range and <em>last</em> is the last <a class="reference internal" href="#ipaddress.IPv4Address" title="ipaddress.IPv4Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Address</span></code></a>
  1284. or <a class="reference internal" href="#ipaddress.IPv6Address" title="ipaddress.IPv6Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv6Address</span></code></a> in the range. 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> is raised if
  1285. <em>first</em> or <em>last</em> are not IP addresses or are not of the same version. A
  1286. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised if <em>last</em> is not greater than <em>first</em> or if
  1287. <em>first</em> address version is not 4 or 6.</p>
  1288. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">ipaddr</span> <span class="k">for</span> <span class="n">ipaddr</span> <span class="ow">in</span> <span class="n">ipaddress</span><span class="o">.</span><span class="n">summarize_address_range</span><span class="p">(</span>
  1289. <span class="gp">... </span> <span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;192.0.2.0&#39;</span><span class="p">),</span>
  1290. <span class="gp">... </span> <span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;192.0.2.130&#39;</span><span class="p">))]</span>
  1291. <span class="go">[IPv4Network(&#39;192.0.2.0/25&#39;), IPv4Network(&#39;192.0.2.128/31&#39;), IPv4Network(&#39;192.0.2.130/32&#39;)]</span>
  1292. </pre></div>
  1293. </div>
  1294. </dd></dl>
  1295. <dl class="py function">
  1296. <dt class="sig sig-object py" id="ipaddress.collapse_addresses">
  1297. <span class="sig-prename descclassname"><span class="pre">ipaddress.</span></span><span class="sig-name descname"><span class="pre">collapse_addresses</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">addresses</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.collapse_addresses" title="Link to this definition">¶</a></dt>
  1298. <dd><p>Return an iterator of the collapsed <a class="reference internal" href="#ipaddress.IPv4Network" title="ipaddress.IPv4Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Network</span></code></a> or
  1299. <a class="reference internal" href="#ipaddress.IPv6Network" title="ipaddress.IPv6Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv6Network</span></code></a> objects. <em>addresses</em> is an iterator of
  1300. <a class="reference internal" href="#ipaddress.IPv4Network" title="ipaddress.IPv4Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Network</span></code></a> or <a class="reference internal" href="#ipaddress.IPv6Network" title="ipaddress.IPv6Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv6Network</span></code></a> objects. 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> is
  1301. raised if <em>addresses</em> contains mixed version objects.</p>
  1302. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">ipaddr</span> <span class="k">for</span> <span class="n">ipaddr</span> <span class="ow">in</span>
  1303. <span class="gp">... </span><span class="n">ipaddress</span><span class="o">.</span><span class="n">collapse_addresses</span><span class="p">([</span><span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv4Network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/25&#39;</span><span class="p">),</span>
  1304. <span class="gp">... </span><span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv4Network</span><span class="p">(</span><span class="s1">&#39;192.0.2.128/25&#39;</span><span class="p">)])]</span>
  1305. <span class="go">[IPv4Network(&#39;192.0.2.0/24&#39;)]</span>
  1306. </pre></div>
  1307. </div>
  1308. </dd></dl>
  1309. <dl class="py function">
  1310. <dt class="sig sig-object py" id="ipaddress.get_mixed_type_key">
  1311. <span class="sig-prename descclassname"><span class="pre">ipaddress.</span></span><span class="sig-name descname"><span class="pre">get_mixed_type_key</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.get_mixed_type_key" title="Link to this definition">¶</a></dt>
  1312. <dd><p>Return a key suitable for sorting between networks and addresses. Address
  1313. and Network objects are not sortable by default; they’re fundamentally
  1314. different, so the expression:</p>
  1315. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;192.0.2.0&#39;</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="n">IPv4Network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/24&#39;</span><span class="p">)</span>
  1316. </pre></div>
  1317. </div>
  1318. <p>doesn’t make sense. There are some times however, where you may wish to
  1319. have <a class="reference internal" href="#module-ipaddress" title="ipaddress: IPv4/IPv6 manipulation library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ipaddress</span></code></a> sort these anyway. If you need to do this, you can use
  1320. this function as the <em>key</em> argument to <a class="reference internal" href="functions.html#sorted" title="sorted"><code class="xref py py-func docutils literal notranslate"><span class="pre">sorted()</span></code></a>.</p>
  1321. <p><em>obj</em> is either a network or address object.</p>
  1322. </dd></dl>
  1323. </section>
  1324. <section id="custom-exceptions">
  1325. <h2>Custom Exceptions<a class="headerlink" href="#custom-exceptions" title="Link to this heading">¶</a></h2>
  1326. <p>To support more specific error reporting from class constructors, the
  1327. module defines the following exceptions:</p>
  1328. <dl class="py exception">
  1329. <dt class="sig sig-object py" id="ipaddress.AddressValueError">
  1330. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ipaddress.</span></span><span class="sig-name descname"><span class="pre">AddressValueError</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">ValueError</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.AddressValueError" title="Link to this definition">¶</a></dt>
  1331. <dd><p>Any value error related to the address.</p>
  1332. </dd></dl>
  1333. <dl class="py exception">
  1334. <dt class="sig sig-object py" id="ipaddress.NetmaskValueError">
  1335. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ipaddress.</span></span><span class="sig-name descname"><span class="pre">NetmaskValueError</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">ValueError</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ipaddress.NetmaskValueError" title="Link to this definition">¶</a></dt>
  1336. <dd><p>Any value error related to the net mask.</p>
  1337. </dd></dl>
  1338. </section>
  1339. </section>
  1340. <div class="clearer"></div>
  1341. </div>
  1342. </div>
  1343. </div>
  1344. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1345. <div class="sphinxsidebarwrapper">
  1346. <div>
  1347. <h3><a href="../contents.html">Table of Contents</a></h3>
  1348. <ul>
  1349. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ipaddress</span></code> — IPv4/IPv6 manipulation library</a><ul>
  1350. <li><a class="reference internal" href="#convenience-factory-functions">Convenience factory functions</a></li>
  1351. <li><a class="reference internal" href="#ip-addresses">IP Addresses</a><ul>
  1352. <li><a class="reference internal" href="#address-objects">Address objects</a></li>
  1353. <li><a class="reference internal" href="#conversion-to-strings-and-integers">Conversion to Strings and Integers</a></li>
  1354. <li><a class="reference internal" href="#operators">Operators</a><ul>
  1355. <li><a class="reference internal" href="#comparison-operators">Comparison operators</a></li>
  1356. <li><a class="reference internal" href="#arithmetic-operators">Arithmetic operators</a></li>
  1357. </ul>
  1358. </li>
  1359. </ul>
  1360. </li>
  1361. <li><a class="reference internal" href="#ip-network-definitions">IP Network definitions</a><ul>
  1362. <li><a class="reference internal" href="#prefix-net-mask-and-host-mask">Prefix, net mask and host mask</a></li>
  1363. <li><a class="reference internal" href="#network-objects">Network objects</a></li>
  1364. <li><a class="reference internal" href="#id1">Operators</a><ul>
  1365. <li><a class="reference internal" href="#logical-operators">Logical operators</a></li>
  1366. <li><a class="reference internal" href="#iteration">Iteration</a></li>
  1367. <li><a class="reference internal" href="#networks-as-containers-of-addresses">Networks as containers of addresses</a></li>
  1368. </ul>
  1369. </li>
  1370. </ul>
  1371. </li>
  1372. <li><a class="reference internal" href="#interface-objects">Interface objects</a><ul>
  1373. <li><a class="reference internal" href="#id2">Operators</a><ul>
  1374. <li><a class="reference internal" href="#id3">Logical operators</a></li>
  1375. </ul>
  1376. </li>
  1377. </ul>
  1378. </li>
  1379. <li><a class="reference internal" href="#other-module-level-functions">Other Module Level Functions</a></li>
  1380. <li><a class="reference internal" href="#custom-exceptions">Custom Exceptions</a></li>
  1381. </ul>
  1382. </li>
  1383. </ul>
  1384. </div>
  1385. <div>
  1386. <h4>Previous topic</h4>
  1387. <p class="topless"><a href="xmlrpc.server.html"
  1388. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xmlrpc.server</span></code> — Basic XML-RPC servers</a></p>
  1389. </div>
  1390. <div>
  1391. <h4>Next topic</h4>
  1392. <p class="topless"><a href="mm.html"
  1393. title="next chapter">Multimedia Services</a></p>
  1394. </div>
  1395. <div role="note" aria-label="source link">
  1396. <h3>This Page</h3>
  1397. <ul class="this-page-menu">
  1398. <li><a href="../bugs.html">Report a Bug</a></li>
  1399. <li>
  1400. <a href="https://github.com/python/cpython/blob/main/Doc/library/ipaddress.rst"
  1401. rel="nofollow">Show Source
  1402. </a>
  1403. </li>
  1404. </ul>
  1405. </div>
  1406. </div>
  1407. <div id="sidebarbutton" title="Collapse sidebar">
  1408. <span>«</span>
  1409. </div>
  1410. </div>
  1411. <div class="clearer"></div>
  1412. </div>
  1413. <div class="related" role="navigation" aria-label="related navigation">
  1414. <h3>Navigation</h3>
  1415. <ul>
  1416. <li class="right" style="margin-right: 10px">
  1417. <a href="../genindex.html" title="General Index"
  1418. >index</a></li>
  1419. <li class="right" >
  1420. <a href="../py-modindex.html" title="Python Module Index"
  1421. >modules</a> |</li>
  1422. <li class="right" >
  1423. <a href="mm.html" title="Multimedia Services"
  1424. >next</a> |</li>
  1425. <li class="right" >
  1426. <a href="xmlrpc.server.html" title="xmlrpc.server — Basic XML-RPC servers"
  1427. >previous</a> |</li>
  1428. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1429. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1430. <li class="switchers">
  1431. <div class="language_switcher_placeholder"></div>
  1432. <div class="version_switcher_placeholder"></div>
  1433. </li>
  1434. <li>
  1435. </li>
  1436. <li id="cpython-language-and-version">
  1437. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1438. </li>
  1439. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1440. <li class="nav-item nav-item-2"><a href="internet.html" >Internet Protocols and Support</a> &#187;</li>
  1441. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">ipaddress</span></code> — IPv4/IPv6 manipulation library</a></li>
  1442. <li class="right">
  1443. <div class="inline-search" role="search">
  1444. <form class="inline-search" action="../search.html" method="get">
  1445. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1446. <input type="submit" value="Go" />
  1447. </form>
  1448. </div>
  1449. |
  1450. </li>
  1451. <li class="right">
  1452. <label class="theme-selector-label">
  1453. Theme
  1454. <select class="theme-selector" oninput="activateTheme(this.value)">
  1455. <option value="auto" selected>Auto</option>
  1456. <option value="light">Light</option>
  1457. <option value="dark">Dark</option>
  1458. </select>
  1459. </label> |</li>
  1460. </ul>
  1461. </div>
  1462. <div class="footer">
  1463. &copy;
  1464. <a href="../copyright.html">
  1465. Copyright
  1466. </a>
  1467. 2001-2024, Python Software Foundation.
  1468. <br />
  1469. This page is licensed under the Python Software Foundation License Version 2.
  1470. <br />
  1471. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1472. <br />
  1473. See <a href="/license.html">History and License</a> for more information.<br />
  1474. <br />
  1475. The Python Software Foundation is a non-profit corporation.
  1476. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1477. <br />
  1478. <br />
  1479. Last updated on Apr 09, 2024 (13:47 UTC).
  1480. <a href="/bugs.html">Found a bug</a>?
  1481. <br />
  1482. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1483. </div>
  1484. </body>
  1485. </html>
上海开阖软件有限公司 沪ICP备12045867号-1