Skip to main content

IPLib3

A pathlib.Path-equivalent for IP addresses.

PyPI Python versions PyPI - Implementation Wheel Downloads Version
Tests (main) codecov Unit tests Pylint Flake8 Deploy to PyPI
Tests (nightly) codecov Unit tests Pylint Flake8 Deploy to PyPI
Docs Documentation Status
Activity GitHub contributors Last commit GitHub all releases GitHub issues GitHub closed issues GitHub pull requests GitHub closed pull requests
QA CodeFactor Rating
Other License FOSSA Status Requirements Status Repository size Code size Lines of code

This module was heavily inspired by the built-in pathlib module to provide a similar, flexible interface for IP addresses. iplib3 can effortlessly convert between IPv4, IPv6, raw numbers and hex values and it can also verify IP address syntax. It can recognise optional port numbers and store them separately from the main address. The iplib.IPAddress class works like pathlib.Path in that it accepts both IPv4 and IPv6 addresses, returning an object representing whichever format was used. The module also uses some unit tests, and these will be added more over time as functionality grows and becomes more set in stone.

The module is currently lacking in long-term vision as I used it as a practice project, but there are some plans to further flesh it out. It could incorporate URL support in the future and may be extended with requests integration.

This project is not affiliated with iplib, the naming similarity is merely a coincidence.

FOSSA Status

Installation

iplib3 will work with Python versions 3.6 and above. It could be back-ported to 3.5 or for even earlier versions with relatively little change, but the maintainer doesn't see a good reason to focus on earlier versions.

This module will not support Python 2.

Use the package manager pip to install iplib3.

On Windows:

py -m pip install iplib3

On most Unix-like platforms:

pip3 install iplib3

On other platforms, you may try:

pip install iplib3

The library is distributed as wheels and source distributions, and the source distributions use Poetry as the build back-end instead of setuptools.

Usage

The module mainly provides a single class, iplib3.IPAddress, which can be used to initialise IP address objects of any supported type. However, it is possible to use the provided iplib3.IPv4 and iplib3.IPv6 classes directly if needed.

The primary class has the advantage that it also supports raw numbers; you can initialise it with any positive integer in addition to stringified addresses, and since you can directly convert between the two sub-classes at any time you can use all functionality with just iplib3.IPAddress. Since iplib3.IPv4 and iplib3.IPv6 are subclasses of iplib3.IPAddress, you can use isinstance to recognise any of the three types.

Some basic usage examples:

from iplib3 import IPAddress, IPv6

address = IPAddress('222.173.190.239')
print(address) # 222.173.190.239

print(address.port) # None, because we never specified one
address.port = 80
print(address) # 222.173.190.239:80

print(repr(address)) # iplib3.IPv4('222.173.190.239:80')


ipv6_address = address.as_ipv6
print(ipv6_address) # [0:0:0:0:0:0:DEAD:BEEF]:80
ipv6_address.port = None
print(ipv6_address) # 0:0:0:0:0:0:DEAD:BEEF

print(repr(ipv6_address)) # iplib3.IPv6('0:0:0:0:0:0:DEAD:BEEF')

foo = IPv6('[::1337:1337:1337:1337]:25565')
bar = IPv6('::1337:1337:1337:1337', 25565)
baz = IPv6('::1337:1337:1337:1337', port_num=25565)

print(f"Addresses are {'equal' if foo == bar == baz else 'not equal'}")
print(baz) # [::1337:1337:1337:1337]:25565

print(baz.as_ipv4.as_ipv6 == baz)

# If the string contains a port number and you also provide a port separately,
# then the separately provided port takes precedence
spam = IPv6('[::1337:1337:1337:1337]:80', port_num=25565)
print(spam) # [::1337:1337:1337:1337]:25565

print(address.hex) # 0xDEADBEEF
print(address.num) # 3735928559

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

This project is licensed under an MIT license.

FOSSA Status

Metadata

Release files for iplib3 0.2.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 iplib3 0.2.4
File Size Uploaded
iplib3-0.2.4.tar.gz 17.1 kB Details

Built distribution (wheel)

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

Total release size: 34.8 kB

Release files / iplib3-0.2.4.tar.gz

Download URL iplib3-0.2.4.tar.gz
Size 17.1 kB
Tags Source
SHA-256 checksum
How to use checksums
59db0c8225837873b9b28aea53207d0dfee79bfb47dec1d4eb1ec9ea0e4ad684
BLAKE2b-256 checksum
How to use checksums
b4492358cdcc3c099a2c945e5d749eb4c045f22a33df26715a47af50f3a4b7ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.0 Linux/6.2.0-1011-azure

Release files / iplib3-0.2.4-py3-none-any.whl

Download URL iplib3-0.2.4-py3-none-any.whl
Size 17.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5b219af33b78e18ebac7034c9e2998c69ff7560f9d8aa1f20de063a3a4fecf6c
BLAKE2b-256 checksum
How to use checksums
daa1a21c1aedcc5337f59c60be2421b559ded96fa6f369ba7e3571b477fd511e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.0 Linux/6.2.0-1011-azure

Release history Release notifications | RSS feed

This release

0.2.4 This release

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

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