/usr/share/doc/python-netaddr-docs/api/tutorial_01.html is in python-netaddr-docs 0.7.12-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tutorial 1: IP Addresses, Subnets and Ranges — netaddr 0.7.12 documentation</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.7.12',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="copyright" title="Copyright" href="copyright.html" />
<link rel="top" title="netaddr 0.7.12 documentation" href="index.html" />
<link rel="next" title="Tutorial 2: MAC addresses" href="tutorial_02.html" />
<link rel="prev" title="Installing netaddr" href="installation.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="tutorial_02.html" title="Tutorial 2: MAC addresses"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="installation.html" title="Installing netaddr"
accesskey="P">previous</a> |</li>
<li><a href="index.html">netaddr 0.7.12 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="tutorial-1-ip-addresses-subnets-and-ranges">
<h1>Tutorial 1: IP Addresses, Subnets and Ranges<a class="headerlink" href="#tutorial-1-ip-addresses-subnets-and-ranges" title="Permalink to this headline">¶</a></h1>
<p>First of all you need to pull the various netaddr classes and functions into your namespace.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Do this for the purpose of this tutorial only. In your own code, you should be explicit about the classes, functions and constants you import to avoid name clashes.</p>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">netaddr</span> <span class="kn">import</span> <span class="o">*</span>
</pre></div>
</div>
<p>We also import the standard library module <cite>pprint</cite> to help format our output.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">pprint</span>
</pre></div>
</div>
<div class="section" id="basic-operations">
<h2>Basic operations<a class="headerlink" href="#basic-operations" title="Permalink to this headline">¶</a></h2>
<p>The following <cite>IPAddress</cite> object represents a single IP address.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip</span> <span class="o">=</span> <span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.1'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">version</span>
<span class="go">4</span>
</pre></div>
</div>
<p>The <cite>repr()</cite> call returns a Python statement that can be used to reconstruct an equivalent IP address object state from scratch when run in the Python interpreter.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">repr</span><span class="p">(</span><span class="n">ip</span><span class="p">)</span>
<span class="go">"IPAddress('192.0.2.1')"</span>
<span class="gp">>>> </span><span class="n">ip</span>
<span class="go">IPAddress('192.0.2.1')</span>
</pre></div>
</div>
<p>Access in the string context returns the IP object as a string value.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">str</span><span class="p">(</span><span class="n">ip</span><span class="p">)</span>
<span class="go">'192.0.2.1'</span>
<span class="gp">>>> </span><span class="s">'</span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="n">ip</span>
<span class="go">'192.0.2.1'</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">format</span><span class="p">()</span> <span class="c"># only really useful for IPv6 addresses.</span>
<span class="go">'192.0.2.1'</span>
</pre></div>
</div>
</div>
<div class="section" id="numerical-representation">
<h2>Numerical representation<a class="headerlink" href="#numerical-representation" title="Permalink to this headline">¶</a></h2>
<p>You can view an IP address in various other formats.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">int</span><span class="p">(</span><span class="n">ip</span><span class="p">)</span> <span class="o">==</span> <span class="mi">3221225985</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="nb">hex</span><span class="p">(</span><span class="n">ip</span><span class="p">)</span>
<span class="go">'0xc0000201'</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">bin</span>
<span class="go">'0b11000000000000000000001000000001'</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">bits</span><span class="p">()</span>
<span class="go">'11000000.00000000.00000010.00000001'</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">words</span> <span class="o">==</span> <span class="p">(</span><span class="mi">192</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="representing-networks-and-subnets">
<h2>Representing networks and subnets<a class="headerlink" href="#representing-networks-and-subnets" title="Permalink to this headline">¶</a></h2>
<p><cite>IPNetwork</cite> objects are used to represent subnets, networks or VLANs that accept CIDR prefixes and netmasks.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip</span> <span class="o">=</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.1'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">ip</span>
<span class="go">IPAddress('192.0.2.1')</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">network</span><span class="p">,</span> <span class="n">ip</span><span class="o">.</span><span class="n">broadcast</span>
<span class="go">(IPAddress('192.0.2.1'), IPAddress('192.0.2.1'))</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">netmask</span><span class="p">,</span> <span class="n">ip</span><span class="o">.</span><span class="n">hostmask</span>
<span class="go">(IPAddress('255.255.255.255'), IPAddress('0.0.0.0'))</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">size</span>
<span class="go">1</span>
</pre></div>
</div>
<p>In this case, the network and broadcast address are the same, akin to a host route.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip</span> <span class="o">=</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/24'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">ip</span>
<span class="go">IPAddress('192.0.2.0')</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">network</span><span class="p">,</span> <span class="n">ip</span><span class="o">.</span><span class="n">broadcast</span>
<span class="go">(IPAddress('192.0.2.0'), IPAddress('192.0.2.255'))</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">netmask</span><span class="p">,</span> <span class="n">ip</span><span class="o">.</span><span class="n">hostmask</span>
<span class="go">(IPAddress('255.255.255.0'), IPAddress('0.0.0.255'))</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">size</span>
<span class="go">256</span>
</pre></div>
</div>
<p>And finally, this IPNetwork object represents an IP address that belongs to a given IP subnet.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip</span> <span class="o">=</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.3.112/22'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">ip</span>
<span class="go">IPAddress('192.0.3.112')</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">network</span><span class="p">,</span> <span class="n">ip</span><span class="o">.</span><span class="n">broadcast</span>
<span class="go">(IPAddress('192.0.0.0'), IPAddress('192.0.3.255'))</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">netmask</span><span class="p">,</span> <span class="n">ip</span><span class="o">.</span><span class="n">hostmask</span>
<span class="go">(IPAddress('255.255.252.0'), IPAddress('0.0.3.255'))</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">size</span>
<span class="go">1024</span>
</pre></div>
</div>
<p>Internally, each IPNetwork object only stores 3 values :-</p>
<blockquote>
<div><ul class="simple">
<li>the IP address value as an unsigned integer</li>
<li>a reference to the IP protocol module for the IP version being represented</li>
<li>the CIDR prefix bitmask</li>
</ul>
</div></blockquote>
<p>All the other values are calculated on-the-fly on access.</p>
<p>It is possible to adjust the IP address value and the CIDR prefix after object instantiation.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip</span> <span class="o">=</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'0.0.0.0/0'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">ip</span>
<span class="go">IPNetwork('0.0.0.0/0')</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="mi">3221225985</span>
<span class="gp">>>> </span><span class="n">ip</span>
<span class="go">IPNetwork('192.0.2.1/0')</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">prefixlen</span>
<span class="go">0</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">prefixlen</span> <span class="o">=</span> <span class="mi">23</span>
<span class="gp">>>> </span><span class="n">ip</span>
<span class="go">IPNetwork('192.0.2.1/23')</span>
</pre></div>
</div>
<p>There is also a property that lets you access the <em>true</em> CIDR address which removes all host bits from the network address based on the CIDR subnet prefix.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">cidr</span>
<span class="go">IPNetwork('192.0.2.0/23')</span>
</pre></div>
</div>
<p>This is handy for specifying some networking configurations correctly.</p>
<p>If you want to access information about each of the various IP addresses that form the IP subnet, this is available by performing pass through calls to sub methods of each <cite>IPAddress</cite> object.</p>
<p>For example if you want to see a binary digit representation of each address you can do the following.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">ip</span><span class="o">.</span><span class="n">bits</span><span class="p">()</span>
<span class="go">'11000000.00000000.00000010.00000001'</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">network</span><span class="o">.</span><span class="n">bits</span><span class="p">()</span>
<span class="go">'11000000.00000000.00000010.00000000'</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">netmask</span><span class="o">.</span><span class="n">bits</span><span class="p">()</span>
<span class="go">'11111111.11111111.11111110.00000000'</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">broadcast</span><span class="o">.</span><span class="n">bits</span><span class="p">()</span>
<span class="go">'11000000.00000000.00000011.11111111'</span>
</pre></div>
</div>
</div>
<div class="section" id="ipv6-support">
<h2>IPv6 support<a class="headerlink" href="#ipv6-support" title="Permalink to this headline">¶</a></h2>
<p>Full support for IPv6 is provided. Let’s try a few examples:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip</span> <span class="o">=</span> <span class="n">IPAddress</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">ip</span>
<span class="go">IPAddress('::')</span>
<span class="gp">>>> </span><span class="n">ip</span> <span class="o">=</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'fe80::dead:beef/64'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="nb">str</span><span class="p">(</span><span class="n">ip</span><span class="p">),</span> <span class="n">ip</span><span class="o">.</span><span class="n">prefixlen</span><span class="p">,</span> <span class="n">ip</span><span class="o">.</span><span class="n">version</span>
<span class="go">('fe80::dead:beef/64', 64, 6)</span>
<span class="gp">>>> </span><span class="nb">int</span><span class="p">(</span><span class="n">ip</span><span class="o">.</span><span class="n">ip</span><span class="p">)</span> <span class="o">==</span> <span class="mi">338288524927261089654018896845083623151</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="nb">hex</span><span class="p">(</span><span class="n">ip</span><span class="o">.</span><span class="n">ip</span><span class="p">)</span>
<span class="go">'0xfe8000000000000000000000deadbeef'</span>
</pre></div>
</div>
<p>Bit-style output isn’t as quite as friendly as hexadecimal for such a long numbers, but here the proof that it works!</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">ip</span><span class="o">.</span><span class="n">bits</span><span class="p">()</span>
<span class="go">'1111111010000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:1101111010101101:1011111011101111'</span>
</pre></div>
</div>
<p>Here are some networking details for an IPv6 subnet.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">network</span><span class="p">,</span> <span class="n">ip</span><span class="o">.</span><span class="n">broadcast</span><span class="p">,</span> <span class="n">ip</span><span class="o">.</span><span class="n">netmask</span><span class="p">,</span> <span class="n">ip</span><span class="o">.</span><span class="n">hostmask</span>
<span class="go">(IPAddress('fe80::'), IPAddress('fe80::ffff:ffff:ffff:ffff'), IPAddress('ffff:ffff:ffff:ffff::'), IPAddress('::ffff:ffff:ffff:ffff'))</span>
</pre></div>
</div>
</div>
<div class="section" id="interoperability-between-ipv4-and-ipv6">
<h2>Interoperability between IPv4 and IPv6<a class="headerlink" href="#interoperability-between-ipv4-and-ipv6" title="Permalink to this headline">¶</a></h2>
<p>It is likely that with IPv6 becoming more prevalent, you’ll want to be able to interoperate between IPv4 and IPv6 address seemlessly.</p>
<p>Here are a couple of methods that help achieve this.</p>
<div class="section" id="ipv4-to-ipv6-conversion">
<h3>IPv4 to IPv6 conversion<a class="headerlink" href="#ipv4-to-ipv6-conversion" title="Permalink to this headline">¶</a></h3>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.15'</span><span class="p">)</span><span class="o">.</span><span class="n">ipv4</span><span class="p">()</span>
<span class="go">IPAddress('192.0.2.15')</span>
<span class="gp">>>> </span><span class="n">ip</span> <span class="o">=</span> <span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.15'</span><span class="p">)</span><span class="o">.</span><span class="n">ipv6</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">ip</span>
<span class="go">IPAddress('::ffff:192.0.2.15')</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">is_ipv4_mapped</span><span class="p">()</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">is_ipv4_compat</span><span class="p">()</span>
<span class="go">False</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.15'</span><span class="p">)</span><span class="o">.</span><span class="n">ipv6</span><span class="p">(</span><span class="n">ipv4_compatible</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="go">IPAddress('::192.0.2.15')</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.15'</span><span class="p">)</span><span class="o">.</span><span class="n">ipv6</span><span class="p">(</span><span class="n">ipv4_compatible</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span><span class="o">.</span><span class="n">is_ipv4_compat</span><span class="p">()</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.15'</span><span class="p">)</span><span class="o">.</span><span class="n">ipv6</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span>
<span class="go">IPAddress('::192.0.2.15')</span>
<span class="gp">>>> </span><span class="n">ip</span> <span class="o">=</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.1/23'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">ipv4</span><span class="p">()</span>
<span class="go">IPNetwork('192.0.2.1/23')</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">ipv6</span><span class="p">()</span>
<span class="go">IPNetwork('::ffff:192.0.2.1/119')</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">ipv6</span><span class="p">(</span><span class="n">ipv4_compatible</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="go">IPNetwork('::192.0.2.1/119')</span>
</pre></div>
</div>
</div>
<div class="section" id="ipv6-to-ipv4-conversion">
<h3>IPv6 to IPv4 conversion<a class="headerlink" href="#ipv6-to-ipv4-conversion" title="Permalink to this headline">¶</a></h3>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'::ffff:192.0.2.1/119'</span><span class="p">)</span><span class="o">.</span><span class="n">ipv6</span><span class="p">()</span>
<span class="go">IPNetwork('::ffff:192.0.2.1/119')</span>
<span class="gp">>>> </span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'::ffff:192.0.2.1/119'</span><span class="p">)</span><span class="o">.</span><span class="n">ipv6</span><span class="p">(</span><span class="n">ipv4_compatible</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="go">IPNetwork('::192.0.2.1/119')</span>
<span class="gp">>>> </span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'::ffff:192.0.2.1/119'</span><span class="p">)</span><span class="o">.</span><span class="n">ipv4</span><span class="p">()</span>
<span class="go">IPNetwork('192.0.2.1/23')</span>
<span class="gp">>>> </span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'::192.0.2.1/119'</span><span class="p">)</span><span class="o">.</span><span class="n">ipv4</span><span class="p">()</span>
<span class="go">IPNetwork('192.0.2.1/23')</span>
</pre></div>
</div>
<p>Note that the IP object returns IPv4 “mapped” addresses by default in preference to IPv4 “compatible” ones. This has been chosen purposefully as the latter form has been deprecated (see RFC 4291 for details).</p>
</div>
</div>
<div class="section" id="list-operations">
<h2>List operations<a class="headerlink" href="#list-operations" title="Permalink to this headline">¶</a></h2>
<p>If you treat an <cite>IPNetwork</cite> object as if it were a standard Python list object it will give you access to a list of individual IP address objects. This of course is illusory and they are not created until you access them.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip</span> <span class="o">=</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.16/29'</span><span class="p">)</span>
</pre></div>
</div>
<p>Accessing an IP object using the <cite>list()</cite> context invokes the default generator which returns a list of all IP objects in the range specified by the IP object’s subnet.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip_list</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">ip</span><span class="p">)</span>
<span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">ip_list</span><span class="p">)</span>
<span class="go">8</span>
<span class="gp">>>> </span><span class="n">ip_list</span>
<span class="go">[IPAddress('192.0.2.16'), IPAddress('192.0.2.17'), ..., IPAddress('192.0.2.22'), IPAddress('192.0.2.23')]</span>
</pre></div>
</div>
<p>The length of that list is 8 individual IP addresses.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">ip</span><span class="p">)</span>
<span class="go">8</span>
</pre></div>
</div>
<div class="section" id="indexing">
<h3>Indexing<a class="headerlink" href="#indexing" title="Permalink to this headline">¶</a></h3>
<p>You can use standard index access to IP addresses in the subnet.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="go">IPAddress('192.0.2.16')</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="go">IPAddress('192.0.2.17')</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="go">IPAddress('192.0.2.23')</span>
</pre></div>
</div>
</div>
<div class="section" id="slicing">
<h3>Slicing<a class="headerlink" href="#slicing" title="Permalink to this headline">¶</a></h3>
<p>You can also use list slices on IP addresses in the subnet.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span>
<span class="go"><generator object ...></span>
</pre></div>
</div>
<p>The slice is a generator function. This was done to save time and system resources as some slices can end up being very large for certain subnets!</p>
<p>Here is how you’d access all elements in a slice.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">ip</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">4</span><span class="p">])</span>
<span class="go">[IPAddress('192.0.2.16'), IPAddress('192.0.2.17'), IPAddress('192.0.2.18'), IPAddress('192.0.2.19')]</span>
</pre></div>
</div>
<p>Extended slicing is also supported.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">ip</span><span class="p">[</span><span class="mi">0</span><span class="p">::</span><span class="mi">2</span><span class="p">])</span>
<span class="go">[IPAddress('192.0.2.16'), IPAddress('192.0.2.18'), IPAddress('192.0.2.20'), IPAddress('192.0.2.22')]</span>
</pre></div>
</div>
<p>List reversal.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">ip</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">::</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="go">[IPAddress('192.0.2.23'), IPAddress('192.0.2.22'), ..., IPAddress('192.0.2.17'), IPAddress('192.0.2.16')]</span>
</pre></div>
</div>
<p>Use of generators ensures working with large IP subnets is efficient.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">for</span> <span class="n">ip</span> <span class="ow">in</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/23'</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">print</span> <span class="s">'</span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="n">ip</span>
<span class="gp">...</span>
<span class="go">192.0.2.0</span>
<span class="go">192.0.2.1</span>
<span class="go">192.0.2.2</span>
<span class="go">192.0.2.3</span>
<span class="gp">...</span>
<span class="go">192.0.3.252</span>
<span class="go">192.0.3.253</span>
<span class="go">192.0.3.254</span>
<span class="go">192.0.3.255</span>
</pre></div>
</div>
<p>In IPv4 networks you only usually assign the addresses between the network and broadcast addresses to actual host interfaces on systems.</p>
<p>Here is the iterator provided for accessing these IP addresses :-</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">for</span> <span class="n">ip</span> <span class="ow">in</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/23'</span><span class="p">)</span><span class="o">.</span><span class="n">iter_hosts</span><span class="p">():</span>
<span class="gp">... </span> <span class="k">print</span> <span class="s">'</span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="n">ip</span>
<span class="gp">...</span>
<span class="go">192.0.2.1</span>
<span class="go">192.0.2.2</span>
<span class="go">192.0.2.3</span>
<span class="go">192.0.2.4</span>
<span class="gp">...</span>
<span class="go">192.0.3.251</span>
<span class="go">192.0.3.252</span>
<span class="go">192.0.3.253</span>
<span class="go">192.0.3.254</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="sorting-ip-addresses-and-networks">
<h2>Sorting IP addresses and networks<a class="headerlink" href="#sorting-ip-addresses-and-networks" title="Permalink to this headline">¶</a></h2>
<p>It is fairly common and useful to be able to sort IP addresses and networks canonically.</p>
<p>Here is how sorting works with individual addresses.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">random</span>
<span class="gp">>>> </span><span class="n">ip_list</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.128/28'</span><span class="p">))</span>
<span class="gp">>>> </span><span class="n">random</span><span class="o">.</span><span class="n">shuffle</span><span class="p">(</span><span class="n">ip_list</span><span class="p">)</span>
<span class="gp">>>> </span><span class="nb">sorted</span><span class="p">(</span><span class="n">ip_list</span><span class="p">)</span>
<span class="go">[IPAddress('192.0.2.128'), IPAddress('192.0.2.129'), ..., IPAddress('192.0.2.142'), IPAddress('192.0.2.143')]</span>
</pre></div>
</div>
<p>For convenience, you are able to sort IP subnets at the same time as addresses and they can be combinations of IPv4 and IPv6 addresses at the same time as well (IPv4 addresses and network appear before IPv6 ones).</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip_list</span> <span class="o">=</span> <span class="p">[</span>
<span class="gp">... </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.130'</span><span class="p">),</span>
<span class="gp">... </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'10.0.0.1'</span><span class="p">),</span>
<span class="gp">... </span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.128/28'</span><span class="p">),</span>
<span class="gp">... </span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.3.0/24'</span><span class="p">),</span>
<span class="gp">... </span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/24'</span><span class="p">),</span>
<span class="gp">... </span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'fe80::/64'</span><span class="p">),</span>
<span class="gp">... </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'::'</span><span class="p">),</span>
<span class="gp">... </span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'172.24/12'</span><span class="p">)]</span>
<span class="gp">>>> </span><span class="n">random</span><span class="o">.</span><span class="n">shuffle</span><span class="p">(</span><span class="n">ip_list</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">ip_list</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">ip_list</span><span class="p">)</span>
<span class="go">[IPAddress('10.0.0.1'),</span>
<span class="go"> IPNetwork('172.24.0.0/12'),</span>
<span class="go"> IPNetwork('192.0.2.0/24'),</span>
<span class="go"> IPNetwork('192.0.2.128/28'),</span>
<span class="go"> IPAddress('192.0.2.130'),</span>
<span class="go"> IPNetwork('192.0.3.0/24'),</span>
<span class="go"> IPAddress('::'),</span>
<span class="go"> IPNetwork('fe80::/64')]</span>
</pre></div>
</div>
<p>Notice how overlapping subnets also sort in order from largest to smallest.</p>
</div>
<div class="section" id="summarizing-list-of-addresses-and-subnets">
<h2>Summarizing list of addresses and subnets<a class="headerlink" href="#summarizing-list-of-addresses-and-subnets" title="Permalink to this headline">¶</a></h2>
<p>Another useful operation is the ability to summarize groups of IP subnets and addresses, merging them together where possible to create the smallest possible list of CIDR subnets.</p>
<p>You do this in netaddr using the <cite>cidr_merge()</cite> function.</p>
<p>First we create a list of IP objects that contains a good mix of individual addresses and subnets, along with some string based IP address values for good measure. To make things more interesting some IPv6 addresses are thrown in as well.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">ip</span> <span class="k">for</span> <span class="n">ip</span> <span class="ow">in</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'fe80::/120'</span><span class="p">)]</span>
<span class="gp">>>> </span><span class="n">ip_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/24'</span><span class="p">))</span>
<span class="gp">>>> </span><span class="n">ip_list</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">ip</span><span class="p">)</span> <span class="k">for</span> <span class="n">ip</span> <span class="ow">in</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.3.0/24'</span><span class="p">)])</span>
<span class="gp">>>> </span><span class="n">ip_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.4.0/25'</span><span class="p">))</span>
<span class="gp">>>> </span><span class="n">ip_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.4.128/25'</span><span class="p">))</span>
<span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">ip_list</span><span class="p">)</span>
<span class="go">515</span>
<span class="gp">>>> </span><span class="n">cidr_merge</span><span class="p">(</span><span class="n">ip_list</span><span class="p">)</span>
<span class="go">[IPNetwork('192.0.2.0/23'), IPNetwork('192.0.4.0/24'), IPNetwork('fe80::/120')]</span>
</pre></div>
</div>
<p>Useful isn’t it?</p>
</div>
<div class="section" id="supernets-and-subnets">
<h2>Supernets and subnets<a class="headerlink" href="#supernets-and-subnets" title="Permalink to this headline">¶</a></h2>
<p>It is quite common to have a large CIDR subnet that you may want to split up into multiple smaller component blocks to better manage your network allocations, firewall rules etcc and netaddr gives you the tools required to do this.</p>
<p>Here we take a large /16 private class B network block and split it up into a set of smaller 512 sized blocks.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip</span> <span class="o">=</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'172.24.0.0/16'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">subnet</span><span class="p">(</span><span class="mi">23</span><span class="p">)</span>
<span class="go"><generator object ...></span>
</pre></div>
</div>
<p>Once again, this method produces and iterator because of the possibility for a large number of return values depending on this subnet size specified.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">subnets</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">ip</span><span class="o">.</span><span class="n">subnet</span><span class="p">(</span><span class="mi">23</span><span class="p">))</span>
<span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">subnets</span><span class="p">)</span>
<span class="go">128</span>
<span class="gp">>>> </span><span class="n">subnets</span>
<span class="go">[IPNetwork('172.24.0.0/23'), IPNetwork('172.24.2.0/23'), IPNetwork('172.24.4.0/23'), ..., IPNetwork('172.24.250.0/23'), IPNetwork('172.24.252.0/23'), IPNetwork('172.24.254.0/23')]</span>
</pre></div>
</div>
<p>It is also possible to retrieve the list of supernets that a given IP address or subnet belongs to. You can also specify an optional limit.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip</span> <span class="o">=</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.114'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">supernets</span> <span class="o">=</span> <span class="n">ip</span><span class="o">.</span><span class="n">supernet</span><span class="p">(</span><span class="mi">22</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">supernets</span><span class="p">)</span>
<span class="go">[IPNetwork('192.0.0.0/22'),</span>
<span class="go"> IPNetwork('192.0.2.0/23'),</span>
<span class="go"> IPNetwork('192.0.2.0/24'),</span>
<span class="go"> IPNetwork('192.0.2.0/25'),</span>
<span class="go"> IPNetwork('192.0.2.64/26'),</span>
<span class="go"> IPNetwork('192.0.2.96/27'),</span>
<span class="go"> IPNetwork('192.0.2.112/28'),</span>
<span class="go"> IPNetwork('192.0.2.112/29'),</span>
<span class="go"> IPNetwork('192.0.2.112/30'),</span>
<span class="go"> IPNetwork('192.0.2.114/31')]</span>
</pre></div>
</div>
<p>Here, we return a list rather than a generator because the potential list of values is of a predictable size (no more than 31 subnets for an IPv4 address and 127 for IPv6).</p>
</div>
<div class="section" id="support-for-non-standard-address-ranges">
<h2>Support for non-standard address ranges<a class="headerlink" href="#support-for-non-standard-address-ranges" title="Permalink to this headline">¶</a></h2>
<p>While CIDR is a useful way to describe networks succinctly, it is often necessary (particularly with IPv4 which predates the CIDR specification) to be able to generate lists of IP addresses that have an arbitrary start and end address that do not fall on strict bit mask boundaries.</p>
<p>The <cite>iter_iprange()</cite> function allow you to do just this.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip_list</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">iter_iprange</span><span class="p">(</span><span class="s">'192.0.2.1'</span><span class="p">,</span> <span class="s">'192.0.2.14'</span><span class="p">))</span>
<span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">ip_list</span><span class="p">)</span>
<span class="go">14</span>
<span class="gp">>>> </span><span class="n">ip_list</span>
<span class="go">[IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), ..., IPAddress('192.0.2.13'), IPAddress('192.0.2.14')]</span>
</pre></div>
</div>
<p>It is equally nice to know what the actual list of CIDR subnets is that would correctly cover this non-aligned range of addresses.</p>
<p>Here <cite>cidr_merge()</cite> comes to the rescue once more.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">cidr_merge</span><span class="p">(</span><span class="n">ip_list</span><span class="p">)</span>
<span class="go">[IPNetwork('192.0.2.1/32'), IPNetwork('192.0.2.2/31'), IPNetwork('192.0.2.4/30'), IPNetwork('192.0.2.8/30'), IPNetwork('192.0.2.12/31'), IPNetwork('192.0.2.14/32')]</span>
</pre></div>
</div>
</div>
<div class="section" id="dealing-with-older-ip-network-specifications">
<h2>Dealing with older IP network specifications<a class="headerlink" href="#dealing-with-older-ip-network-specifications" title="Permalink to this headline">¶</a></h2>
<p>Until the advent of the CIDR specification it was common to infer the netmask of an IPv4 address based on its first octet using an set of classful rules (first defined in RFC 791).</p>
<p>You frequently come across reference to them in various RFCs and they are well supported by a number of software libraries. For completeness, rather than leave out this important (but now somewhat historical) set of rules, they are supported via the cryptically named <cite>cidr_abbrev_to_verbose()</cite> function.</p>
<p>Here is an example of these rules for the whole of the IPv4 address space.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">cidrs</span> <span class="o">=</span> <span class="p">[</span><span class="n">cidr_abbrev_to_verbose</span><span class="p">(</span><span class="n">octet</span><span class="p">)</span> <span class="k">for</span> <span class="n">octet</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">256</span><span class="p">)]</span>
<span class="gp">>>> </span><span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">cidrs</span><span class="p">)</span>
<span class="go">['0.0.0.0/8',</span>
<span class="gp">...</span>
<span class="go"> '127.0.0.0/8',</span>
<span class="go"> '128.0.0.0/16',</span>
<span class="gp">...</span>
<span class="go"> '191.0.0.0/16',</span>
<span class="go"> '192.0.0.0/24',</span>
<span class="gp">...</span>
<span class="go"> '223.0.0.0/24',</span>
<span class="go"> '224.0.0.0/4',</span>
<span class="gp">...</span>
<span class="go"> '239.0.0.0/4',</span>
<span class="go"> '240.0.0.0/32',</span>
<span class="gp">...</span>
<span class="go"> '255.0.0.0/32']</span>
<span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">cidrs</span><span class="p">)</span>
<span class="go">256</span>
</pre></div>
</div>
</div>
<div class="section" id="ip-address-categorisation">
<h2>IP address categorisation<a class="headerlink" href="#ip-address-categorisation" title="Permalink to this headline">¶</a></h2>
<p>IP addresses fall into several categories, not all of which are suitable for assignment as host addresses.</p>
<div class="section" id="unicast">
<h3>Unicast<a class="headerlink" href="#unicast" title="Permalink to this headline">¶</a></h3>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.1'</span><span class="p">)</span><span class="o">.</span><span class="n">is_unicast</span><span class="p">()</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'fe80::1'</span><span class="p">)</span><span class="o">.</span><span class="n">is_unicast</span><span class="p">()</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="multicast">
<h3>Multicast<a class="headerlink" href="#multicast" title="Permalink to this headline">¶</a></h3>
<p>Used to indentify multicast groups (see RFC 2365 and 3171 for more info).</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'239.192.0.1'</span><span class="p">)</span><span class="o">.</span><span class="n">is_multicast</span><span class="p">()</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'ff00::1'</span><span class="p">)</span><span class="o">.</span><span class="n">is_multicast</span><span class="p">()</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="private">
<h3>Private<a class="headerlink" href="#private" title="Permalink to this headline">¶</a></h3>
<p>Found on intranets and used behind NAT routers.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'172.24.0.1'</span><span class="p">)</span><span class="o">.</span><span class="n">is_private</span><span class="p">()</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'10.0.0.1'</span><span class="p">)</span><span class="o">.</span><span class="n">is_private</span><span class="p">()</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.168.0.1'</span><span class="p">)</span><span class="o">.</span><span class="n">is_private</span><span class="p">()</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'fc00::1'</span><span class="p">)</span><span class="o">.</span><span class="n">is_private</span><span class="p">()</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="reserved">
<h3>Reserved<a class="headerlink" href="#reserved" title="Permalink to this headline">¶</a></h3>
<p>Addresses in reserved ranges are not available for general use.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'253.0.0.1'</span><span class="p">)</span><span class="o">.</span><span class="n">is_reserved</span><span class="p">()</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="public">
<h3>Public<a class="headerlink" href="#public" title="Permalink to this headline">¶</a></h3>
<p>Addresses accessible via the Internet.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">circa the end of 2011 all IPv4 addresses had been allocated to the Regional Internet Registrars. A booming after market in IPv4 addresses has started. There is still plenty of life left in this protocol version yet :)</p>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ip</span> <span class="o">=</span> <span class="n">IPAddress</span><span class="p">(</span><span class="s">'62.125.24.5'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">ip</span><span class="o">.</span><span class="n">is_unicast</span><span class="p">()</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">ip</span><span class="o">.</span><span class="n">is_private</span><span class="p">()</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="netmasks">
<h3>Netmasks<a class="headerlink" href="#netmasks" title="Permalink to this headline">¶</a></h3>
<p>A bitmask used to divide an IP address into its network address and host address.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'255.255.254.0'</span><span class="p">)</span><span class="o">.</span><span class="n">is_netmask</span><span class="p">()</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="hostmasks">
<h3>Hostmasks<a class="headerlink" href="#hostmasks" title="Permalink to this headline">¶</a></h3>
<p>Similar to a netmask but with the all the bits flipped the opposite way.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'0.0.1.255'</span><span class="p">)</span><span class="o">.</span><span class="n">is_hostmask</span><span class="p">()</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="loopback">
<h3>Loopback<a class="headerlink" href="#loopback" title="Permalink to this headline">¶</a></h3>
<p>These addresses are used internally within an IP network stack and packets sent to these addresses are not distributed via a physical network connection.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'127.0.0.1'</span><span class="p">)</span><span class="o">.</span><span class="n">is_loopback</span><span class="p">()</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'::1'</span><span class="p">)</span><span class="o">.</span><span class="n">is_loopback</span><span class="p">()</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="comparing-ip-addresses">
<h2>Comparing IP addresses<a class="headerlink" href="#comparing-ip-addresses" title="Permalink to this headline">¶</a></h2>
<p><cite>IPAddress</cite> objects can be compared with each other. As an <cite>IPAddress</cite> object can represent both an individual IP address and an implicit network, it pays to get both sides of your comparison into the same terms before you compare them to avoid odd results.</p>
<p>Here are some comparisons of individual IP address to get the ball rolling.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.1'</span><span class="p">)</span> <span class="o">==</span> <span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.1'</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.1'</span><span class="p">)</span> <span class="o"><</span> <span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.2'</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.2'</span><span class="p">)</span> <span class="o">></span> <span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.1'</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.1'</span><span class="p">)</span> <span class="o">!=</span> <span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.1'</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.1'</span><span class="p">)</span> <span class="o">>=</span> <span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.1'</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.2'</span><span class="p">)</span> <span class="o">>=</span> <span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.1'</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.1'</span><span class="p">)</span> <span class="o"><=</span> <span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.1'</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.1'</span><span class="p">)</span> <span class="o"><=</span> <span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.2'</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Now, lets try something a little more interesting.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/24'</span><span class="p">)</span> <span class="o">==</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.112/24'</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Hmmmmmmmm... looks a bit odd doesn’t it? That’s because by default, IP objects compare their subnets (or lower and upper boundaries) rather than their individual IP address values.</p>
<p>The solution to this situation is very simple. Knowing this default behaviour, just be explicit about exactly which portion of each IP object you’d like to compare using pass-through properties.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/24'</span><span class="p">)</span><span class="o">.</span><span class="n">ip</span> <span class="o">==</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.112/24'</span><span class="p">)</span><span class="o">.</span><span class="n">ip</span>
<span class="go">False</span>
<span class="gp">>>> </span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/24'</span><span class="p">)</span><span class="o">.</span><span class="n">ip</span> <span class="o"><</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.112/24'</span><span class="p">)</span><span class="o">.</span><span class="n">ip</span>
<span class="go">True</span>
</pre></div>
</div>
<p>That’s more like it. You can also be explicit about comparing networks in this way if you so wish (although it is not strictly necessary).</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/24'</span><span class="p">)</span><span class="o">.</span><span class="n">cidr</span> <span class="o">==</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.112/24'</span><span class="p">)</span><span class="o">.</span><span class="n">cidr</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Armed with this information here are some examples of network comparisons.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/24'</span><span class="p">)</span> <span class="o">==</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.3.0/24'</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">>>> </span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/24'</span><span class="p">)</span> <span class="o"><</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.3.0/24'</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/24'</span><span class="p">)</span> <span class="o"><</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.3.0/24'</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>This will inevitably raise questions about comparing IPAddress (scalar) objects and IPNetwork (vector) objects with each other (or at least it should).</p>
<p>Here is how netaddr chooses to address this situation.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.0'</span><span class="p">)</span> <span class="o">==</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/32'</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.0'</span><span class="p">)</span> <span class="o">!=</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/32'</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>An IP network or subnet is different from an individual IP address and therefore cannot be (directly) compared.</p>
<p>If you want to compare them successfully, you must be explicit about which aspect of the IP network you wish to match against the IP address in question.</p>
<p>You can use the index of the first or last address if it is a /32 like so :-</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.0'</span><span class="p">)</span> <span class="o">==</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/32'</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.0'</span><span class="p">)</span> <span class="o">==</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/32'</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.0'</span><span class="p">)</span> <span class="o">!=</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/32'</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
<span class="go">False</span>
</pre></div>
</div>
<p>You can also use the base address if this is what you wish to compare :-</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.0'</span><span class="p">)</span> <span class="o">==</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/32'</span><span class="p">)</span><span class="o">.</span><span class="n">ip</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.0'</span><span class="p">)</span> <span class="o">!=</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/32'</span><span class="p">)</span><span class="o">.</span><span class="n">ip</span>
<span class="go">False</span>
</pre></div>
</div>
<p>While this may seem a bit pointless at first, netaddr strives to keep IP addresses and network separate from one another while still allowing reasonable interoperability.</p>
</div>
<div class="section" id="dns-support">
<h2>DNS support<a class="headerlink" href="#dns-support" title="Permalink to this headline">¶</a></h2>
<p>It is a common administrative task to generate reverse IP lookups for DNS. This is particularly arduous for IPv6 addresses.</p>
<p>Here is how you do this using an IPAddress object’s <cite>reverse_dns()</cite> method.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'172.24.0.13'</span><span class="p">)</span><span class="o">.</span><span class="n">reverse_dns</span>
<span class="go">'13.0.24.172.in-addr.arpa.'</span>
<span class="gp">>>> </span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'fe80::feeb:daed'</span><span class="p">)</span><span class="o">.</span><span class="n">reverse_dns</span>
<span class="go">'d.e.a.d.b.e.e.f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa.'</span>
</pre></div>
</div>
<p>Note that <tt class="docutils literal"><span class="pre">ip6.int</span></tt> is not used as this has been deprecated (see RFC 3152 for details).</p>
</div>
<div class="section" id="non-standard-address-ranges">
<h2>Non standard address ranges<a class="headerlink" href="#non-standard-address-ranges" title="Permalink to this headline">¶</a></h2>
<p>As CIDR is a relative newcomer given the long history of IP version 4 you are quite likely to come across systems and documentation which make reference to IP address ranges in formats other than CIDR. Converting from these arbitrary range types to CIDR and back again isn’t a particularly fun task. Fortunately, netaddr tries to make this job easy for you with two purpose built classes.</p>
<div class="section" id="arbitrary-ip-address-ranges">
<h3>Arbitrary IP address ranges<a class="headerlink" href="#arbitrary-ip-address-ranges" title="Permalink to this headline">¶</a></h3>
<p>You can represent an arbitrary IP address range using a lower and upper bound address in the form of an IPRange object.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">r1</span> <span class="o">=</span> <span class="n">IPRange</span><span class="p">(</span><span class="s">'192.0.2.1'</span><span class="p">,</span> <span class="s">'192.0.2.15'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">r1</span>
<span class="go">IPRange('192.0.2.1', '192.0.2.15')</span>
</pre></div>
</div>
<p>You can iterate across and index these ranges just like and IPNetwork object.</p>
<p>Importantly, you can also convert it to it’s CIDR equivalent.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">r1</span><span class="o">.</span><span class="n">cidrs</span><span class="p">()</span>
<span class="go">[IPNetwork('192.0.2.1/32'), IPNetwork('192.0.2.2/31'), IPNetwork('192.0.2.4/30'), IPNetwork('192.0.2.8/29')]</span>
</pre></div>
</div>
<p>Here is how individual IPRange and IPNetwork compare.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPRange</span><span class="p">(</span><span class="s">'192.0.2.0'</span><span class="p">,</span> <span class="s">'192.0.2.255'</span><span class="p">)</span> <span class="o">!=</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/24'</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">>>> </span><span class="n">IPRange</span><span class="p">(</span><span class="s">'192.0.2.0'</span><span class="p">,</span> <span class="s">'192.0.2.255'</span><span class="p">)</span> <span class="o">==</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/24'</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>You may wish to compare an IP range against a list of IPAddress and IPNetwork
objects.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">r1</span> <span class="o">=</span> <span class="n">IPRange</span><span class="p">(</span><span class="s">'192.0.2.1'</span><span class="p">,</span> <span class="s">'192.0.2.15'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">addrs</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">r1</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">addrs</span>
<span class="go">[IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), IPAddress('192.0.2.3'), IPAddress('192.0.2.4'), IPAddress('192.0.2.5'), IPAddress('192.0.2.6'), IPAddress('192.0.2.7'), IPAddress('192.0.2.8'), IPAddress('192.0.2.9'), IPAddress('192.0.2.10'), IPAddress('192.0.2.11'), IPAddress('192.0.2.12'), IPAddress('192.0.2.13'), IPAddress('192.0.2.14'), IPAddress('192.0.2.15')]</span>
<span class="gp">>>> </span><span class="n">r1</span> <span class="o">==</span> <span class="n">addrs</span>
<span class="go">False</span>
</pre></div>
</div>
<p>Oops! Not quite what we were looking for or expecting.</p>
<p>The way to do this is to get either side of the comparison operation into the same terms.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">r1</span><span class="p">)</span> <span class="o">==</span> <span class="n">addrs</span>
<span class="go">True</span>
</pre></div>
</div>
<p>That’s more like it.</p>
<p>The same goes for IPNetwork objects.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">subnets</span> <span class="o">=</span> <span class="n">r1</span><span class="o">.</span><span class="n">cidrs</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">subnets</span>
<span class="go">[IPNetwork('192.0.2.1/32'), IPNetwork('192.0.2.2/31'), IPNetwork('192.0.2.4/30'), IPNetwork('192.0.2.8/29')]</span>
<span class="gp">>>> </span><span class="n">r1</span> <span class="o">==</span> <span class="n">subnets</span>
<span class="go">False</span>
<span class="gp">>>> </span><span class="n">r1</span><span class="o">.</span><span class="n">cidrs</span><span class="p">()</span> <span class="o">==</span> <span class="n">subnets</span>
<span class="go">True</span>
</pre></div>
</div>
<p>The above works if the list you are comparing contains one type or the other, but what if you have a mixed list of <cite>IPAddress</cite>, <cite>IPNetwork</cite> and string addresses?</p>
<p>Time for some slightly more powerful operations. Let’s make use of a new class for dealing with groups of IP addresses and subnets. The IPSet class.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ips</span> <span class="o">=</span> <span class="p">[</span><span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.1'</span><span class="p">),</span> <span class="s">'192.0.2.2/31'</span><span class="p">,</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.4/31'</span><span class="p">),</span> <span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.6'</span><span class="p">),</span> <span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.7'</span><span class="p">),</span> <span class="s">'192.0.2.8'</span><span class="p">,</span> <span class="s">'192.0.2.9'</span><span class="p">,</span> <span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.10'</span><span class="p">),</span> <span class="n">IPAddress</span><span class="p">(</span><span class="s">'192.0.2.11'</span><span class="p">),</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.12/30'</span><span class="p">)]</span>
<span class="gp">>>> </span><span class="n">s1</span> <span class="o">=</span> <span class="n">IPSet</span><span class="p">(</span><span class="n">r1</span><span class="o">.</span><span class="n">cidrs</span><span class="p">())</span>
<span class="gp">>>> </span><span class="n">s2</span> <span class="o">=</span> <span class="n">IPSet</span><span class="p">(</span><span class="n">ips</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">s2</span>
<span class="go">IPSet(['192.0.2.1/32', '192.0.2.2/31', '192.0.2.4/30', '192.0.2.8/29'])</span>
<span class="gp">>>> </span><span class="n">s1</span> <span class="o">==</span> <span class="n">s2</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Let’s remove one of the element from one of the IPSet objects and see what happens.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">s2</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
<span class="go">IPNetwork('192.0.2.4/30')</span>
<span class="gp">>>> </span><span class="n">s1</span> <span class="o">==</span> <span class="n">s2</span>
<span class="go">False</span>
</pre></div>
</div>
<p>This is perhaps a somewhat contrived example but it just shows you some of the capabilities on offer.</p>
<p>See the IPSet tutorial <a class="reference internal" href="tutorial_03.html"><em>Tutorial 3: Working with IP sets</em></a> for more details on that class.</p>
</div>
<div class="section" id="ip-glob-ranges">
<h3>IP Glob ranges<a class="headerlink" href="#ip-glob-ranges" title="Permalink to this headline">¶</a></h3>
<p>netaddr also supports a user friendly form of specifying IP address ranges using a “glob” style syntax.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">At present only IPv4 globs are supported.</p>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">IPGlob</span><span class="p">(</span><span class="s">'192.0.2.*'</span><span class="p">)</span> <span class="o">==</span> <span class="n">IPNetwork</span><span class="p">(</span><span class="s">'192.0.2.0/24'</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>IPGlob(‘192.0.2.*’) != IPNetwork(‘192.0.2.0/24’)
False</p>
<p>As <cite>IPGlob</cite> is a subclass of <cite>IPRange</cite>, all of the same operations apply.</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Tutorial 1: IP Addresses, Subnets and Ranges</a><ul>
<li><a class="reference internal" href="#basic-operations">Basic operations</a></li>
<li><a class="reference internal" href="#numerical-representation">Numerical representation</a></li>
<li><a class="reference internal" href="#representing-networks-and-subnets">Representing networks and subnets</a></li>
<li><a class="reference internal" href="#ipv6-support">IPv6 support</a></li>
<li><a class="reference internal" href="#interoperability-between-ipv4-and-ipv6">Interoperability between IPv4 and IPv6</a><ul>
<li><a class="reference internal" href="#ipv4-to-ipv6-conversion">IPv4 to IPv6 conversion</a></li>
<li><a class="reference internal" href="#ipv6-to-ipv4-conversion">IPv6 to IPv4 conversion</a></li>
</ul>
</li>
<li><a class="reference internal" href="#list-operations">List operations</a><ul>
<li><a class="reference internal" href="#indexing">Indexing</a></li>
<li><a class="reference internal" href="#slicing">Slicing</a></li>
</ul>
</li>
<li><a class="reference internal" href="#sorting-ip-addresses-and-networks">Sorting IP addresses and networks</a></li>
<li><a class="reference internal" href="#summarizing-list-of-addresses-and-subnets">Summarizing list of addresses and subnets</a></li>
<li><a class="reference internal" href="#supernets-and-subnets">Supernets and subnets</a></li>
<li><a class="reference internal" href="#support-for-non-standard-address-ranges">Support for non-standard address ranges</a></li>
<li><a class="reference internal" href="#dealing-with-older-ip-network-specifications">Dealing with older IP network specifications</a></li>
<li><a class="reference internal" href="#ip-address-categorisation">IP address categorisation</a><ul>
<li><a class="reference internal" href="#unicast">Unicast</a></li>
<li><a class="reference internal" href="#multicast">Multicast</a></li>
<li><a class="reference internal" href="#private">Private</a></li>
<li><a class="reference internal" href="#reserved">Reserved</a></li>
<li><a class="reference internal" href="#public">Public</a></li>
<li><a class="reference internal" href="#netmasks">Netmasks</a></li>
<li><a class="reference internal" href="#hostmasks">Hostmasks</a></li>
<li><a class="reference internal" href="#loopback">Loopback</a></li>
</ul>
</li>
<li><a class="reference internal" href="#comparing-ip-addresses">Comparing IP addresses</a></li>
<li><a class="reference internal" href="#dns-support">DNS support</a></li>
<li><a class="reference internal" href="#non-standard-address-ranges">Non standard address ranges</a><ul>
<li><a class="reference internal" href="#arbitrary-ip-address-ranges">Arbitrary IP address ranges</a></li>
<li><a class="reference internal" href="#ip-glob-ranges">IP Glob ranges</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="installation.html"
title="previous chapter">Installing netaddr</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="tutorial_02.html"
title="next chapter">Tutorial 2: MAC addresses</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/tutorial_01.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="tutorial_02.html" title="Tutorial 2: MAC addresses"
>next</a> |</li>
<li class="right" >
<a href="installation.html" title="Installing netaddr"
>previous</a> |</li>
<li><a href="index.html">netaddr 0.7.12 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© <a href="copyright.html">Copyright</a> 2008-2014, David P. D. Moss. All rights reserved.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3.
</div>
</body>
</html>
|