Monday, July 13, 2020

Real Python: Learn IP Address Concepts With Python's ipaddress Module

Python’s ipaddress module is an underappreciated gem from the Python standard library. You don’t have to be a full-blown network engineer to have been exposed to IP addresses in the wild. IP addresses and networks are ubiquitous in software development and infrastructure. They underpin how computers, well, address each other.

Learning through doing is an effective way to master IP addresses. The ipaddress module allows you to do just that by viewing and manipulating IP addresses as Python objects. In this tutorial, you’ll get a better grasp of IP addresses by using some of the features of Python’s ipaddress module.

In this tutorial, you’ll learn:

  • How IP addresses work, both in theory and in Python code
  • How IP networks represent groups of IP addresses and how you can inspect relationships between the two
  • How Python’s ipaddress module cleverly uses a classic design pattern to allow you to do more with less

To follow along, you just need Python 3.3 or higher since ipaddress was added to the Python standard library in that version. The examples in this tutorial were generated using Python 3.8.

Free Download: Get a sample chapter from CPython Internals: Your Guide to the Python 3 Interpreter showing you how to unlock the inner workings of the Python language, compile the Python interpreter from source code, and participate in the development of CPython.

IP Addresses in Theory and Practice

If you remember only one concept about IP addresses, then remember this: an IP address is an integer. This piece of information will help you better understand both how IP addresses function and how they can be represented as Python objects.

Before you jump into any Python code, it can be helpful to see this concept fleshed out mathematically. If you’re here just for some examples of how to use the ipaddress module, then you can skip down to the next section, on using the module itself.

Mechanics of IP Addresses

You saw above that an IP address boils down to an integer. A fuller definition is that an IPv4 address is a 32-bit integer used to represent a host on a network. The term host is sometimes used synonymously with an address.

It follows that there are 232 possible IPv4 addresses, from 0 to 4,294,967,295 (where the upper bound is 232 - 1). But this is a tutorial for human beings, not robots. No one wants to ping the IP address 0xdc0e0925.

The more common way to express an IPv4 address is using quad-dotted notation, which consists of four dot-separated decimal integers:

220.14.9.37

It’s not immediately obvious what underlying integer the address 220.14.9.37 represents, though. Formulaically, you can break the IP address 220.14.9.37 into its four octet components:

>>>
>>> (
...     220 * (256 ** 3) +
...      14 * (256 ** 2) +
...       9 * (256 ** 1) +
...      37 * (256 ** 0)
... )
3691907365

As shown above, the address 220.14.9.37 represents the integer 3,691,907,365. Each octet is a byte, or a number from 0 to 255. Given this, you can infer that the maximum IPv4 address is 255.255.255.255 (or FF.FF.FF.FF in hex notation), while the minimum is 0.0.0.0.

Next, you’ll see how Python’s ipaddress module does this calculation for you, allowing you to work with the human-readable form and let the address arithmetic occur out of sight.

The Python ipaddress Module

To follow along, you can fetch your computer’s external IP address to work with at the command line:

$ curl -sS ifconfig.me/ip
220.14.9.37

This requests your IP address from the site ifconfig.me, which can be used to show an array of details about your connection and network.

Note: In the interest of technical correctness, this is quite possibly not your computer’s very own public IP address. If your connection sits behind a NATed router, then it’s better thought of as an “agent” IP through which you reach the Internet.

Now open up a Python REPL. You can use the IPv4Address class to build a Python object that encapsulates an address:

>>>
>>> from ipaddress import IPv4Address

>>> addr = IPv4Address("220.14.9.37")
>>> addr
IPv4Address('220.14.9.37')

Read the full article at https://realpython.com/python-ipaddress-module/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...