ipaddress.html 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
  6. <meta property="og:title" content="An introduction to the ipaddress module" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/howto/ipaddress.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="author, Peter Moody,, author, Nick Coghlan,. Overview: This document aims to provide a gentle introduction to the ipaddress module. It is aimed primarily at users that aren’t already familiar with ..." />
  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="author, Peter Moody,, author, Nick Coghlan,. Overview: This document aims to provide a gentle introduction to the ipaddress module. It is aimed primarily at users that aren’t already familiar with ..." />
  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>An introduction to the ipaddress module &#8212; Python 3.12.0 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?digest=b37c26da2f7529d09fe70b41c4b2133fe4931a90" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css" />
  21. <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
  22. <script src="../_static/jquery.js"></script>
  23. <script src="../_static/underscore.js"></script>
  24. <script src="../_static/doctools.js"></script>
  25. <script src="../_static/sidebar.js"></script>
  26. <link rel="search" type="application/opensearchdescription+xml"
  27. title="Search within Python 3.12.0 documentation"
  28. href="../_static/opensearch.xml"/>
  29. <link rel="author" title="About these documents" href="../about.html" />
  30. <link rel="index" title="Index" href="../genindex.html" />
  31. <link rel="search" title="Search" href="../search.html" />
  32. <link rel="copyright" title="Copyright" href="../copyright.html" />
  33. <link rel="next" title="Argument Clinic How-To" href="clinic.html" />
  34. <link rel="prev" title="Argparse Tutorial" href="argparse.html" />
  35. <link rel="canonical" href="https://docs.python.org/3/howto/ipaddress.html" />
  36. <style>
  37. @media only screen {
  38. table.full-width-table {
  39. width: 100%;
  40. }
  41. }
  42. </style>
  43. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  44. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  45. <script type="text/javascript" src="../_static/copybutton.js"></script>
  46. <script type="text/javascript" src="../_static/menu.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="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="#">An introduction to the ipaddress module</a><ul>
  86. <li><a class="reference internal" href="#creating-address-network-interface-objects">Creating Address/Network/Interface objects</a><ul>
  87. <li><a class="reference internal" href="#a-note-on-ip-versions">A Note on IP Versions</a></li>
  88. <li><a class="reference internal" href="#ip-host-addresses">IP Host Addresses</a></li>
  89. <li><a class="reference internal" href="#defining-networks">Defining Networks</a></li>
  90. <li><a class="reference internal" href="#host-interfaces">Host Interfaces</a></li>
  91. </ul>
  92. </li>
  93. <li><a class="reference internal" href="#inspecting-address-network-interface-objects">Inspecting Address/Network/Interface Objects</a></li>
  94. <li><a class="reference internal" href="#networks-as-lists-of-addresses">Networks as lists of Addresses</a></li>
  95. <li><a class="reference internal" href="#comparisons">Comparisons</a></li>
  96. <li><a class="reference internal" href="#using-ip-addresses-with-other-modules">Using IP Addresses with other modules</a></li>
  97. <li><a class="reference internal" href="#getting-more-detail-when-instance-creation-fails">Getting more detail when instance creation fails</a></li>
  98. </ul>
  99. </li>
  100. </ul>
  101. </div>
  102. <div>
  103. <h4>Previous topic</h4>
  104. <p class="topless"><a href="argparse.html"
  105. title="previous chapter">Argparse Tutorial</a></p>
  106. </div>
  107. <div>
  108. <h4>Next topic</h4>
  109. <p class="topless"><a href="clinic.html"
  110. title="next chapter">Argument Clinic How-To</a></p>
  111. </div>
  112. <div role="note" aria-label="source link">
  113. <h3>This Page</h3>
  114. <ul class="this-page-menu">
  115. <li><a href="../bugs.html">Report a Bug</a></li>
  116. <li>
  117. <a href="https://github.com/python/cpython/blob/main/Doc/howto/ipaddress.rst"
  118. rel="nofollow">Show Source
  119. </a>
  120. </li>
  121. </ul>
  122. </div>
  123. </nav>
  124. </div>
  125. </div>
  126. <div class="related" role="navigation" aria-label="related navigation">
  127. <h3>Navigation</h3>
  128. <ul>
  129. <li class="right" style="margin-right: 10px">
  130. <a href="../genindex.html" title="General Index"
  131. accesskey="I">index</a></li>
  132. <li class="right" >
  133. <a href="../py-modindex.html" title="Python Module Index"
  134. >modules</a> |</li>
  135. <li class="right" >
  136. <a href="clinic.html" title="Argument Clinic How-To"
  137. accesskey="N">next</a> |</li>
  138. <li class="right" >
  139. <a href="argparse.html" title="Argparse Tutorial"
  140. accesskey="P">previous</a> |</li>
  141. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  142. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  143. <li class="switchers">
  144. <div class="language_switcher_placeholder"></div>
  145. <div class="version_switcher_placeholder"></div>
  146. </li>
  147. <li>
  148. </li>
  149. <li id="cpython-language-and-version">
  150. <a href="../index.html">3.12.0 Documentation</a> &#187;
  151. </li>
  152. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python HOWTOs</a> &#187;</li>
  153. <li class="nav-item nav-item-this"><a href="">An introduction to the ipaddress module</a></li>
  154. <li class="right">
  155. <div class="inline-search" role="search">
  156. <form class="inline-search" action="../search.html" method="get">
  157. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  158. <input type="submit" value="Go" />
  159. </form>
  160. </div>
  161. |
  162. </li>
  163. <li class="right">
  164. <label class="theme-selector-label">
  165. Theme
  166. <select class="theme-selector" oninput="activateTheme(this.value)">
  167. <option value="auto" selected>Auto</option>
  168. <option value="light">Light</option>
  169. <option value="dark">Dark</option>
  170. </select>
  171. </label> |</li>
  172. </ul>
  173. </div>
  174. <div class="document">
  175. <div class="documentwrapper">
  176. <div class="bodywrapper">
  177. <div class="body" role="main">
  178. <section id="an-introduction-to-the-ipaddress-module">
  179. <span id="ipaddress-howto"></span><h1>An introduction to the ipaddress module<a class="headerlink" href="#an-introduction-to-the-ipaddress-module" title="Permalink to this headline">¶</a></h1>
  180. <dl class="field-list simple">
  181. <dt class="field-odd">author</dt>
  182. <dd class="field-odd"><p>Peter Moody</p>
  183. </dd>
  184. <dt class="field-even">author</dt>
  185. <dd class="field-even"><p>Nick Coghlan</p>
  186. </dd>
  187. </dl>
  188. <div class="topic">
  189. <p class="topic-title">Overview</p>
  190. <p>This document aims to provide a gentle introduction to the
  191. <a class="reference internal" href="../library/ipaddress.html#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. It is aimed primarily at users that aren’t
  192. already familiar with IP networking terminology, but may also be useful
  193. to network engineers wanting an overview of how <a class="reference internal" href="../library/ipaddress.html#module-ipaddress" title="ipaddress: IPv4/IPv6 manipulation library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ipaddress</span></code></a>
  194. represents IP network addressing concepts.</p>
  195. </div>
  196. <section id="creating-address-network-interface-objects">
  197. <h2>Creating Address/Network/Interface objects<a class="headerlink" href="#creating-address-network-interface-objects" title="Permalink to this headline">¶</a></h2>
  198. <p>Since <a class="reference internal" href="../library/ipaddress.html#module-ipaddress" title="ipaddress: IPv4/IPv6 manipulation library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ipaddress</span></code></a> is a module for inspecting and manipulating IP addresses,
  199. the first thing you’ll want to do is create some objects. You can use
  200. <a class="reference internal" href="../library/ipaddress.html#module-ipaddress" title="ipaddress: IPv4/IPv6 manipulation library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ipaddress</span></code></a> to create objects from strings and integers.</p>
  201. <section id="a-note-on-ip-versions">
  202. <h3>A Note on IP Versions<a class="headerlink" href="#a-note-on-ip-versions" title="Permalink to this headline">¶</a></h3>
  203. <p>For readers that aren’t particularly familiar with IP addressing, it’s
  204. important to know that the Internet Protocol (IP) is currently in the process
  205. of moving from version 4 of the protocol to version 6. This transition is
  206. occurring largely because version 4 of the protocol doesn’t provide enough
  207. addresses to handle the needs of the whole world, especially given the
  208. increasing number of devices with direct connections to the internet.</p>
  209. <p>Explaining the details of the differences between the two versions of the
  210. protocol is beyond the scope of this introduction, but readers need to at
  211. least be aware that these two versions exist, and it will sometimes be
  212. necessary to force the use of one version or the other.</p>
  213. </section>
  214. <section id="ip-host-addresses">
  215. <h3>IP Host Addresses<a class="headerlink" href="#ip-host-addresses" title="Permalink to this headline">¶</a></h3>
  216. <p>Addresses, often referred to as “host addresses” are the most basic unit
  217. when working with IP addressing. The simplest way to create addresses is
  218. to use the <a class="reference internal" href="../library/ipaddress.html#ipaddress.ip_address" title="ipaddress.ip_address"><code class="xref py py-func docutils literal notranslate"><span class="pre">ipaddress.ip_address()</span></code></a> factory function, which automatically
  219. determines whether to create an IPv4 or IPv6 address based on the passed in
  220. value:</p>
  221. <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.0.2.1&#39;</span><span class="p">)</span>
  222. <span class="go">IPv4Address(&#39;192.0.2.1&#39;)</span>
  223. <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::1&#39;</span><span class="p">)</span>
  224. <span class="go">IPv6Address(&#39;2001:db8::1&#39;)</span>
  225. </pre></div>
  226. </div>
  227. <p>Addresses can also be created directly from integers. Values that will
  228. fit within 32 bits are assumed to be IPv4 addresses:</p>
  229. <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="mi">3221225985</span><span class="p">)</span>
  230. <span class="go">IPv4Address(&#39;192.0.2.1&#39;)</span>
  231. <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">42540766411282592856903984951653826561</span><span class="p">)</span>
  232. <span class="go">IPv6Address(&#39;2001:db8::1&#39;)</span>
  233. </pre></div>
  234. </div>
  235. <p>To force the use of IPv4 or IPv6 addresses, the relevant classes can be
  236. invoked directly. This is particularly useful to force creation of IPv6
  237. addresses for small integers:</p>
  238. <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="mi">1</span><span class="p">)</span>
  239. <span class="go">IPv4Address(&#39;0.0.0.1&#39;)</span>
  240. <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">1</span><span class="p">)</span>
  241. <span class="go">IPv4Address(&#39;0.0.0.1&#39;)</span>
  242. <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="mi">1</span><span class="p">)</span>
  243. <span class="go">IPv6Address(&#39;::1&#39;)</span>
  244. </pre></div>
  245. </div>
  246. </section>
  247. <section id="defining-networks">
  248. <h3>Defining Networks<a class="headerlink" href="#defining-networks" title="Permalink to this headline">¶</a></h3>
  249. <p>Host addresses are usually grouped together into IP networks, so
  250. <a class="reference internal" href="../library/ipaddress.html#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 a way to create, inspect and manipulate network
  251. definitions. IP network objects are constructed from strings that define the
  252. range of host addresses that are part of that network. The simplest form
  253. for that information is a “network address/network prefix” pair, where the
  254. prefix defines the number of leading bits that are compared to determine
  255. whether or not an address is part of the network and the network address
  256. defines the expected value of those bits.</p>
  257. <p>As for addresses, a factory function is provided that determines the correct
  258. IP version automatically:</p>
  259. <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_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.0/24&#39;</span><span class="p">)</span>
  260. <span class="go">IPv4Network(&#39;192.0.2.0/24&#39;)</span>
  261. <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;2001:db8::0/96&#39;</span><span class="p">)</span>
  262. <span class="go">IPv6Network(&#39;2001:db8::/96&#39;)</span>
  263. </pre></div>
  264. </div>
  265. <p>Network objects cannot have any host bits set. The practical effect of this
  266. is that <code class="docutils literal notranslate"><span class="pre">192.0.2.1/24</span></code> does not describe a network. Such definitions are
  267. referred to as interface objects since the ip-on-a-network notation is
  268. commonly used to describe network interfaces of a computer on a given network
  269. and are described further in the next section.</p>
  270. <p>By default, attempting to create a network object with host bits set will
  271. result in <a class="reference internal" href="../library/exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> being raised. To request that the
  272. additional bits instead be coerced to zero, the flag <code class="docutils literal notranslate"><span class="pre">strict=False</span></code> can
  273. be passed to the constructor:</p>
  274. <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_network</span><span class="p">(</span><span class="s1">&#39;192.0.2.1/24&#39;</span><span class="p">)</span>
  275. <span class="gt">Traceback (most recent call last):</span>
  276. <span class="w"> </span><span class="c">...</span>
  277. <span class="gr">ValueError</span>: <span class="n">192.0.2.1/24 has host bits set</span>
  278. <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.0.2.1/24&#39;</span><span class="p">,</span> <span class="n">strict</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
  279. <span class="go">IPv4Network(&#39;192.0.2.0/24&#39;)</span>
  280. </pre></div>
  281. </div>
  282. <p>While the string form offers significantly more flexibility, networks can
  283. also be defined with integers, just like host addresses. In this case, the
  284. network is considered to contain only the single address identified by the
  285. integer, so the network prefix includes the entire network address:</p>
  286. <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_network</span><span class="p">(</span><span class="mi">3221225984</span><span class="p">)</span>
  287. <span class="go">IPv4Network(&#39;192.0.2.0/32&#39;)</span>
  288. <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="mi">42540766411282592856903984951653826560</span><span class="p">)</span>
  289. <span class="go">IPv6Network(&#39;2001:db8::/128&#39;)</span>
  290. </pre></div>
  291. </div>
  292. <p>As with addresses, creation of a particular kind of network can be forced
  293. by calling the class constructor directly instead of using the factory
  294. function.</p>
  295. </section>
  296. <section id="host-interfaces">
  297. <h3>Host Interfaces<a class="headerlink" href="#host-interfaces" title="Permalink to this headline">¶</a></h3>
  298. <p>As mentioned just above, if you need to describe an address on a particular
  299. network, neither the address nor the network classes are sufficient.
  300. Notation like <code class="docutils literal notranslate"><span class="pre">192.0.2.1/24</span></code> is commonly used by network engineers and the
  301. people who write tools for firewalls and routers as shorthand for “the host
  302. <code class="docutils literal notranslate"><span class="pre">192.0.2.1</span></code> on the network <code class="docutils literal notranslate"><span class="pre">192.0.2.0/24</span></code>”, Accordingly, <a class="reference internal" href="../library/ipaddress.html#module-ipaddress" title="ipaddress: IPv4/IPv6 manipulation library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ipaddress</span></code></a>
  303. provides a set of hybrid classes that associate an address with a particular
  304. network. The interface for creation is identical to that for defining network
  305. objects, except that the address portion isn’t constrained to being a network
  306. address.</p>
  307. <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_interface</span><span class="p">(</span><span class="s1">&#39;192.0.2.1/24&#39;</span><span class="p">)</span>
  308. <span class="go">IPv4Interface(&#39;192.0.2.1/24&#39;)</span>
  309. <span class="gp">&gt;&gt;&gt; </span><span class="n">ipaddress</span><span class="o">.</span><span class="n">ip_interface</span><span class="p">(</span><span class="s1">&#39;2001:db8::1/96&#39;</span><span class="p">)</span>
  310. <span class="go">IPv6Interface(&#39;2001:db8::1/96&#39;)</span>
  311. </pre></div>
  312. </div>
  313. <p>Integer inputs are accepted (as with networks), and use of a particular IP
  314. version can be forced by calling the relevant constructor directly.</p>
  315. </section>
  316. </section>
  317. <section id="inspecting-address-network-interface-objects">
  318. <h2>Inspecting Address/Network/Interface Objects<a class="headerlink" href="#inspecting-address-network-interface-objects" title="Permalink to this headline">¶</a></h2>
  319. <p>You’ve gone to the trouble of creating an IPv(4|6)(Address|Network|Interface)
  320. object, so you probably want to get information about it. <a class="reference internal" href="../library/ipaddress.html#module-ipaddress" title="ipaddress: IPv4/IPv6 manipulation library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ipaddress</span></code></a>
  321. tries to make doing this easy and intuitive.</p>
  322. <p>Extracting the IP version:</p>
  323. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">addr4</span> <span class="o">=</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.0.2.1&#39;</span><span class="p">)</span>
  324. <span class="gp">&gt;&gt;&gt; </span><span class="n">addr6</span> <span class="o">=</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::1&#39;</span><span class="p">)</span>
  325. <span class="gp">&gt;&gt;&gt; </span><span class="n">addr6</span><span class="o">.</span><span class="n">version</span>
  326. <span class="go">6</span>
  327. <span class="gp">&gt;&gt;&gt; </span><span class="n">addr4</span><span class="o">.</span><span class="n">version</span>
  328. <span class="go">4</span>
  329. </pre></div>
  330. </div>
  331. <p>Obtaining the network from an interface:</p>
  332. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">host4</span> <span class="o">=</span> <span class="n">ipaddress</span><span class="o">.</span><span class="n">ip_interface</span><span class="p">(</span><span class="s1">&#39;192.0.2.1/24&#39;</span><span class="p">)</span>
  333. <span class="gp">&gt;&gt;&gt; </span><span class="n">host4</span><span class="o">.</span><span class="n">network</span>
  334. <span class="go">IPv4Network(&#39;192.0.2.0/24&#39;)</span>
  335. <span class="gp">&gt;&gt;&gt; </span><span class="n">host6</span> <span class="o">=</span> <span class="n">ipaddress</span><span class="o">.</span><span class="n">ip_interface</span><span class="p">(</span><span class="s1">&#39;2001:db8::1/96&#39;</span><span class="p">)</span>
  336. <span class="gp">&gt;&gt;&gt; </span><span class="n">host6</span><span class="o">.</span><span class="n">network</span>
  337. <span class="go">IPv6Network(&#39;2001:db8::/96&#39;)</span>
  338. </pre></div>
  339. </div>
  340. <p>Finding out how many individual addresses are in a network:</p>
  341. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">net4</span> <span class="o">=</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.0.2.0/24&#39;</span><span class="p">)</span>
  342. <span class="gp">&gt;&gt;&gt; </span><span class="n">net4</span><span class="o">.</span><span class="n">num_addresses</span>
  343. <span class="go">256</span>
  344. <span class="gp">&gt;&gt;&gt; </span><span class="n">net6</span> <span class="o">=</span> <span class="n">ipaddress</span><span class="o">.</span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;2001:db8::0/96&#39;</span><span class="p">)</span>
  345. <span class="gp">&gt;&gt;&gt; </span><span class="n">net6</span><span class="o">.</span><span class="n">num_addresses</span>
  346. <span class="go">4294967296</span>
  347. </pre></div>
  348. </div>
  349. <p>Iterating through the “usable” addresses on a network:</p>
  350. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">net4</span> <span class="o">=</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.0.2.0/24&#39;</span><span class="p">)</span>
  351. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">net4</span><span class="o">.</span><span class="n">hosts</span><span class="p">():</span>
  352. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
  353. <span class="go">192.0.2.1</span>
  354. <span class="go">192.0.2.2</span>
  355. <span class="go">192.0.2.3</span>
  356. <span class="go">192.0.2.4</span>
  357. <span class="go">...</span>
  358. <span class="go">192.0.2.252</span>
  359. <span class="go">192.0.2.253</span>
  360. <span class="go">192.0.2.254</span>
  361. </pre></div>
  362. </div>
  363. <p>Obtaining the netmask (i.e. set bits corresponding to the network prefix) or
  364. the hostmask (any bits that are not part of the netmask):</p>
  365. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">net4</span> <span class="o">=</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.0.2.0/24&#39;</span><span class="p">)</span>
  366. <span class="gp">&gt;&gt;&gt; </span><span class="n">net4</span><span class="o">.</span><span class="n">netmask</span>
  367. <span class="go">IPv4Address(&#39;255.255.255.0&#39;)</span>
  368. <span class="gp">&gt;&gt;&gt; </span><span class="n">net4</span><span class="o">.</span><span class="n">hostmask</span>
  369. <span class="go">IPv4Address(&#39;0.0.0.255&#39;)</span>
  370. <span class="gp">&gt;&gt;&gt; </span><span class="n">net6</span> <span class="o">=</span> <span class="n">ipaddress</span><span class="o">.</span><span class="n">ip_network</span><span class="p">(</span><span class="s1">&#39;2001:db8::0/96&#39;</span><span class="p">)</span>
  371. <span class="gp">&gt;&gt;&gt; </span><span class="n">net6</span><span class="o">.</span><span class="n">netmask</span>
  372. <span class="go">IPv6Address(&#39;ffff:ffff:ffff:ffff:ffff:ffff::&#39;)</span>
  373. <span class="gp">&gt;&gt;&gt; </span><span class="n">net6</span><span class="o">.</span><span class="n">hostmask</span>
  374. <span class="go">IPv6Address(&#39;::ffff:ffff&#39;)</span>
  375. </pre></div>
  376. </div>
  377. <p>Exploding or compressing the address:</p>
  378. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">addr6</span><span class="o">.</span><span class="n">exploded</span>
  379. <span class="go">&#39;2001:0db8:0000:0000:0000:0000:0000:0001&#39;</span>
  380. <span class="gp">&gt;&gt;&gt; </span><span class="n">addr6</span><span class="o">.</span><span class="n">compressed</span>
  381. <span class="go">&#39;2001:db8::1&#39;</span>
  382. <span class="gp">&gt;&gt;&gt; </span><span class="n">net6</span><span class="o">.</span><span class="n">exploded</span>
  383. <span class="go">&#39;2001:0db8:0000:0000:0000:0000:0000:0000/96&#39;</span>
  384. <span class="gp">&gt;&gt;&gt; </span><span class="n">net6</span><span class="o">.</span><span class="n">compressed</span>
  385. <span class="go">&#39;2001:db8::/96&#39;</span>
  386. </pre></div>
  387. </div>
  388. <p>While IPv4 doesn’t support explosion or compression, the associated objects
  389. still provide the relevant properties so that version neutral code can
  390. easily ensure the most concise or most verbose form is used for IPv6
  391. addresses while still correctly handling IPv4 addresses.</p>
  392. </section>
  393. <section id="networks-as-lists-of-addresses">
  394. <h2>Networks as lists of Addresses<a class="headerlink" href="#networks-as-lists-of-addresses" title="Permalink to this headline">¶</a></h2>
  395. <p>It’s sometimes useful to treat networks as lists. This means it is possible
  396. to index them like this:</p>
  397. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">net4</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
  398. <span class="go">IPv4Address(&#39;192.0.2.1&#39;)</span>
  399. <span class="gp">&gt;&gt;&gt; </span><span class="n">net4</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
  400. <span class="go">IPv4Address(&#39;192.0.2.255&#39;)</span>
  401. <span class="gp">&gt;&gt;&gt; </span><span class="n">net6</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
  402. <span class="go">IPv6Address(&#39;2001:db8::1&#39;)</span>
  403. <span class="gp">&gt;&gt;&gt; </span><span class="n">net6</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
  404. <span class="go">IPv6Address(&#39;2001:db8::ffff:ffff&#39;)</span>
  405. </pre></div>
  406. </div>
  407. <p>It also means that network objects lend themselves to using the list
  408. membership test syntax like this:</p>
  409. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">address</span> <span class="ow">in</span> <span class="n">network</span><span class="p">:</span>
  410. <span class="c1"># do something</span>
  411. </pre></div>
  412. </div>
  413. <p>Containment testing is done efficiently based on the network prefix:</p>
  414. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">addr4</span> <span class="o">=</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.0.2.1&#39;</span><span class="p">)</span>
  415. <span class="gp">&gt;&gt;&gt; </span><span class="n">addr4</span> <span class="ow">in</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.0.2.0/24&#39;</span><span class="p">)</span>
  416. <span class="go">True</span>
  417. <span class="gp">&gt;&gt;&gt; </span><span class="n">addr4</span> <span class="ow">in</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.0.3.0/24&#39;</span><span class="p">)</span>
  418. <span class="go">False</span>
  419. </pre></div>
  420. </div>
  421. </section>
  422. <section id="comparisons">
  423. <h2>Comparisons<a class="headerlink" href="#comparisons" title="Permalink to this headline">¶</a></h2>
  424. <p><a class="reference internal" href="../library/ipaddress.html#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 some simple, hopefully intuitive ways to compare
  425. objects, where it makes sense:</p>
  426. <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="s1">&#39;192.0.2.1&#39;</span><span class="p">)</span> <span class="o">&lt;</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.0.2.2&#39;</span><span class="p">)</span>
  427. <span class="go">True</span>
  428. </pre></div>
  429. </div>
  430. <p>A <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception is raised if you try to compare objects of
  431. different versions or different types.</p>
  432. </section>
  433. <section id="using-ip-addresses-with-other-modules">
  434. <h2>Using IP Addresses with other modules<a class="headerlink" href="#using-ip-addresses-with-other-modules" title="Permalink to this headline">¶</a></h2>
  435. <p>Other modules that use IP addresses (such as <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a>) usually won’t
  436. accept objects from this module directly. Instead, they must be coerced to
  437. an integer or string that the other module will accept:</p>
  438. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">addr4</span> <span class="o">=</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.0.2.1&#39;</span><span class="p">)</span>
  439. <span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">addr4</span><span class="p">)</span>
  440. <span class="go">&#39;192.0.2.1&#39;</span>
  441. <span class="gp">&gt;&gt;&gt; </span><span class="nb">int</span><span class="p">(</span><span class="n">addr4</span><span class="p">)</span>
  442. <span class="go">3221225985</span>
  443. </pre></div>
  444. </div>
  445. </section>
  446. <section id="getting-more-detail-when-instance-creation-fails">
  447. <h2>Getting more detail when instance creation fails<a class="headerlink" href="#getting-more-detail-when-instance-creation-fails" title="Permalink to this headline">¶</a></h2>
  448. <p>When creating address/network/interface objects using the version-agnostic
  449. factory functions, any errors will be reported as <a class="reference internal" href="../library/exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> with
  450. a generic error message that simply says the passed in value was not
  451. recognized as an object of that type. The lack of a specific error is
  452. because it’s necessary to know whether the value is <em>supposed</em> to be IPv4
  453. or IPv6 in order to provide more detail on why it has been rejected.</p>
  454. <p>To support use cases where it is useful to have access to this additional
  455. detail, the individual class constructors actually raise the
  456. <a class="reference internal" href="../library/exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> subclasses <a class="reference internal" href="../library/ipaddress.html#ipaddress.AddressValueError" title="ipaddress.AddressValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ipaddress.AddressValueError</span></code></a> and
  457. <a class="reference internal" href="../library/ipaddress.html#ipaddress.NetmaskValueError" title="ipaddress.NetmaskValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ipaddress.NetmaskValueError</span></code></a> to indicate exactly which part of
  458. the definition failed to parse correctly.</p>
  459. <p>The error messages are significantly more detailed when using the
  460. class constructors directly. For example:</p>
  461. <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;192.168.0.256&quot;</span><span class="p">)</span>
  462. <span class="gt">Traceback (most recent call last):</span>
  463. <span class="w"> </span><span class="c">...</span>
  464. <span class="gr">ValueError</span>: <span class="n">&#39;192.168.0.256&#39; does not appear to be an IPv4 or IPv6 address</span>
  465. <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="s2">&quot;192.168.0.256&quot;</span><span class="p">)</span>
  466. <span class="gt">Traceback (most recent call last):</span>
  467. <span class="w"> </span><span class="c">...</span>
  468. <span class="gr">ipaddress.AddressValueError</span>: <span class="n">Octet 256 (&gt; 255) not permitted in &#39;192.168.0.256&#39;</span>
  469. <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="s2">&quot;192.168.0.1/64&quot;</span><span class="p">)</span>
  470. <span class="gt">Traceback (most recent call last):</span>
  471. <span class="w"> </span><span class="c">...</span>
  472. <span class="gr">ValueError</span>: <span class="n">&#39;192.168.0.1/64&#39; does not appear to be an IPv4 or IPv6 network</span>
  473. <span class="gp">&gt;&gt;&gt; </span><span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv4Network</span><span class="p">(</span><span class="s2">&quot;192.168.0.1/64&quot;</span><span class="p">)</span>
  474. <span class="gt">Traceback (most recent call last):</span>
  475. <span class="w"> </span><span class="c">...</span>
  476. <span class="gr">ipaddress.NetmaskValueError</span>: <span class="n">&#39;64&#39; is not a valid netmask</span>
  477. </pre></div>
  478. </div>
  479. <p>However, both of the module specific exceptions have <a class="reference internal" href="../library/exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> as their
  480. parent class, so if you’re not concerned with the particular type of error,
  481. you can still write code like the following:</p>
  482. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
  483. <span class="n">network</span> <span class="o">=</span> <span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv4Network</span><span class="p">(</span><span class="n">address</span><span class="p">)</span>
  484. <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
  485. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;address/netmask is invalid for IPv4:&#39;</span><span class="p">,</span> <span class="n">address</span><span class="p">)</span>
  486. </pre></div>
  487. </div>
  488. </section>
  489. </section>
  490. <div class="clearer"></div>
  491. </div>
  492. </div>
  493. </div>
  494. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  495. <div class="sphinxsidebarwrapper">
  496. <div>
  497. <h3><a href="../contents.html">Table of Contents</a></h3>
  498. <ul>
  499. <li><a class="reference internal" href="#">An introduction to the ipaddress module</a><ul>
  500. <li><a class="reference internal" href="#creating-address-network-interface-objects">Creating Address/Network/Interface objects</a><ul>
  501. <li><a class="reference internal" href="#a-note-on-ip-versions">A Note on IP Versions</a></li>
  502. <li><a class="reference internal" href="#ip-host-addresses">IP Host Addresses</a></li>
  503. <li><a class="reference internal" href="#defining-networks">Defining Networks</a></li>
  504. <li><a class="reference internal" href="#host-interfaces">Host Interfaces</a></li>
  505. </ul>
  506. </li>
  507. <li><a class="reference internal" href="#inspecting-address-network-interface-objects">Inspecting Address/Network/Interface Objects</a></li>
  508. <li><a class="reference internal" href="#networks-as-lists-of-addresses">Networks as lists of Addresses</a></li>
  509. <li><a class="reference internal" href="#comparisons">Comparisons</a></li>
  510. <li><a class="reference internal" href="#using-ip-addresses-with-other-modules">Using IP Addresses with other modules</a></li>
  511. <li><a class="reference internal" href="#getting-more-detail-when-instance-creation-fails">Getting more detail when instance creation fails</a></li>
  512. </ul>
  513. </li>
  514. </ul>
  515. </div>
  516. <div>
  517. <h4>Previous topic</h4>
  518. <p class="topless"><a href="argparse.html"
  519. title="previous chapter">Argparse Tutorial</a></p>
  520. </div>
  521. <div>
  522. <h4>Next topic</h4>
  523. <p class="topless"><a href="clinic.html"
  524. title="next chapter">Argument Clinic How-To</a></p>
  525. </div>
  526. <div role="note" aria-label="source link">
  527. <h3>This Page</h3>
  528. <ul class="this-page-menu">
  529. <li><a href="../bugs.html">Report a Bug</a></li>
  530. <li>
  531. <a href="https://github.com/python/cpython/blob/main/Doc/howto/ipaddress.rst"
  532. rel="nofollow">Show Source
  533. </a>
  534. </li>
  535. </ul>
  536. </div>
  537. </div>
  538. </div>
  539. <div class="clearer"></div>
  540. </div>
  541. <div class="related" role="navigation" aria-label="related navigation">
  542. <h3>Navigation</h3>
  543. <ul>
  544. <li class="right" style="margin-right: 10px">
  545. <a href="../genindex.html" title="General Index"
  546. >index</a></li>
  547. <li class="right" >
  548. <a href="../py-modindex.html" title="Python Module Index"
  549. >modules</a> |</li>
  550. <li class="right" >
  551. <a href="clinic.html" title="Argument Clinic How-To"
  552. >next</a> |</li>
  553. <li class="right" >
  554. <a href="argparse.html" title="Argparse Tutorial"
  555. >previous</a> |</li>
  556. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  557. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  558. <li class="switchers">
  559. <div class="language_switcher_placeholder"></div>
  560. <div class="version_switcher_placeholder"></div>
  561. </li>
  562. <li>
  563. </li>
  564. <li id="cpython-language-and-version">
  565. <a href="../index.html">3.12.0 Documentation</a> &#187;
  566. </li>
  567. <li class="nav-item nav-item-1"><a href="index.html" >Python HOWTOs</a> &#187;</li>
  568. <li class="nav-item nav-item-this"><a href="">An introduction to the ipaddress module</a></li>
  569. <li class="right">
  570. <div class="inline-search" role="search">
  571. <form class="inline-search" action="../search.html" method="get">
  572. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  573. <input type="submit" value="Go" />
  574. </form>
  575. </div>
  576. |
  577. </li>
  578. <li class="right">
  579. <label class="theme-selector-label">
  580. Theme
  581. <select class="theme-selector" oninput="activateTheme(this.value)">
  582. <option value="auto" selected>Auto</option>
  583. <option value="light">Light</option>
  584. <option value="dark">Dark</option>
  585. </select>
  586. </label> |</li>
  587. </ul>
  588. </div>
  589. <div class="footer">
  590. &copy; <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation.
  591. <br />
  592. This page is licensed under the Python Software Foundation License Version 2.
  593. <br />
  594. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  595. <br />
  596. See <a href="/license.html">History and License</a> for more information.<br />
  597. <br />
  598. The Python Software Foundation is a non-profit corporation.
  599. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  600. <br />
  601. <br />
  602. Last updated on Oct 02, 2023.
  603. <a href="/bugs.html">Found a bug</a>?
  604. <br />
  605. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
  606. </div>
  607. </body>
  608. </html>