Skip to main content

IP Address Library:

This is the work I did when working on TCP/IP assignments. Particularly the ones dealing with IP addresses and subnets. This library contains an easy and convenient, albeit not the most efficient, implementation of IP address operations.

Note :

I made the library more for educational and academic reasons than for any actual use. And as such, I'm open sourcing it with the intention that someone else may find it useful while learning IP addresses.

Dependencies :

  1. Python 3 runtime environment.

Usage :

There are two main classes that can be used to compute IP address operations.

  1. IPConv
  2. Subnet

To import the library use-

from ipconv.ipconv import IPConv, Subnet

IPConv:

This class is used for all operations related to IP addresses.

  • An object can be instantiated by passing the IP address in one of these forms- dotted-decimal, binary, hexadecimal or CIDR notation

ip = IPConv('127.0.0.1/8')

  • Once the object has been created, you can convert this into any alternate notation by-
ip.hexadecimal() # 7F:00:00:01
ip.dotted_decimal() # 127.0.0.1
ip.binary() # 01111111000000000000000000000001
  • You can print all relevant info regarding the IP address by doing-
ip.desc()

Dotted-Decimal      : 127.0.0.1
Binary              : 01111111000000000000000000000001
Hexadecimal         : 7F:00:00:01

Classful Addressing Scheme:
Class               : A
Network mask        : 8
Range               : 16777216
Network address     : 127.0.0.0
Broadcast address   : 127.255.255.255

Classless Addressing Scheme:
Network mask        : 8
Range               : 16777216
Network address     : 127.0.0.0
Broadcast address   : 127.255.255.255
  • You can also get the next or prev IP address from the original address as follows-
for i in ip.next(5):
	print(i)

for i in ip.prev(5):
	print(i)

Subnet:

This class is used for working with subnets. Note that this implementation is for classless IP addressing. But since classful IP addressing can be considered as a special case of classless, it is compatible with both of them. You just need to give correct mask.

  • Initalizing a Subnet object requires two things- IP address in CIDR notation and number of subnets you want.
snet = Subnet('127.0.0.1/8', 8)
  • You can get a table of all the IP ranges by doing-
snet.desc()

+===================================================================+
|Index  |First Address      |Last Address       |Range              |
+===================================================================+
|1      |127.0.0.0          |127.31.255.255     |2097152            |
---------------------------------------------------------------------
|2      |127.32.0.0         |127.63.255.255     |2097152            |
---------------------------------------------------------------------
|3      |127.64.0.0         |127.95.255.255     |2097152            |
---------------------------------------------------------------------
|4      |127.96.0.0         |127.127.255.255    |2097152            |
---------------------------------------------------------------------
|5      |127.128.0.0        |127.159.255.255    |2097152            |
---------------------------------------------------------------------
|6      |127.160.0.0        |127.191.255.255    |2097152            |
---------------------------------------------------------------------
|7      |127.192.0.0        |127.223.255.255    |2097152            |
---------------------------------------------------------------------
|8      |127.224.0.0        |127.255.255.255    |2097152            |
---------------------------------------------------------------------
  • You can get the first or last subnets by-
for i in snet.first(5):
    print(i)

for i in snet.last(5):
	print(i)
  • To get the subnet from any IP address
snet.get_subnet('127.34.78.25')

<subnet 127.32.0.0/21>

License :

This project is licensed under the terms of the MIT license.

Release files for ipconv 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ipconv 0.0.4
File Size Uploaded
ipconv-0.0.4.tar.gz 6.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ipconv 0.0.4
File Interpreter ABI Platform
ipconv-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size:12.9 kB

Release files / ipconv-0.0.4.tar.gz

Download URL ipconv-0.0.4.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a55ae3b29e6646f73ca00e25622390a503764a13f340278a49f5f9af2d0ff5f5
BLAKE2b-256 checksum
How to use checksums
67b2b381148b3fa509719e2f63d56423c790a1b3298ef1a9f6c9da380fad1eb1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / ipconv-0.0.4-py3-none-any.whl

Download URL ipconv-0.0.4-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a2298ef90cce29c1ca07fe9f05287ab9e0fce2b77b85d6cf5568421c9c1c694f
BLAKE2b-256 checksum
How to use checksums
d6fbed275766d9d1ff357386a27e9ad27c75e793d1458188746770fb78ffb194
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page