A library for simplifying IP handling in code
Project description
KryoIP
KryoIP is a Python library designed to simplify the handling and manipulation of IP addresses. Whether you're working with IPv4 or IPv6 addresses, KryoIP provides a set of utility functions for validation, conversion, network checks, and more.
Installation
You can install KryoIP using pip:
pip install kryoip
Features
KryoIP includes a variety of functions to help you work with IP addresses:
IP Validation: Check if an IP address is valid and whether it's private or public.
IP Conversion: Convert IP addresses to binary and integer formats.
CIDR Operations: Convert CIDR blocks to IP ranges.
Ping Test: Perform a basic ping test to check if an IP address is reachable.
Reverse DNS Lookup: Perform a reverse DNS lookup to find the hostname for an IP address.
Network Info: Get network information for an IP address, including its network and host address.
IP Masking: Mask part of an IP address for privacy.
Special Easter Egg: A fun little Easter egg that prints :3.
Functions
1. class IP
A class to manage and manipulate IP addresses.
Methods:
is_private(): Checks if the IP is private.
to_binary(): Converts the IP to binary format.
to_integer(): Converts the IP to integer format.
is_in_network(network): Checks if the IP is within a given network.
2. cidr_to_ip_range(cidr)
Converts a CIDR block to a range of IPs.
cidr_to_ip_range('192.168.1.0/24') # Returns ('192.168.1.0', '192.168.1.255')
3. classify_ip(ip)
Classifies the IP address as 'Private', 'Loopback', or 'Public'.
classify_ip('192.168.1.1') # Returns 'Private'
4. validate_ip_in_network(ip, network_range)
Validates if an IP address is within a given network range.
validate_ip_in_network('192.168.1.1', '192.168.1.0/24') # Returns True
5. reverse_dns_lookup(ip)
Performs a reverse DNS lookup to find the hostname for an IP address.
reverse_dns_lookup('8.8.8.8') # Returns ('dns.google', ['8.8.8.8'])
6. ping_ip(ip)
Pings an IP address to check if it's reachable.
ping_ip('8.8.8.8') # Returns True if the host is reachable
7. mask_ip(ip)
Masks the IP address by replacing the last two octets with *.
mask_ip('192.168.1.1') # Returns '192.168.*.*'
8. validate_ip(ip)
Validates if an IP address is correctly formatted.
validate_ip('192.168.1.1') # Returns None if valid, raises ValueError if invalid
9. validate_ip_batch(ips)
Validates a batch of IPs and returns a dictionary of results.
validate_ip_batch(['192.168.1.1', '8.8.8.8', 'invalid_ip'])
# Returns: {'192.168.1.1': None, '8.8.8.8': None, 'invalid_ip': ValueError('invalid_ip is not a valid IP address')}
10. Special Easter Egg
special_3() # Prints ':3'
Example Usage
# Validation Results
ips = ["192.168.1.1", "8.8.8.8", "2606:2800:220:1:248:1893:25c8:1946"]
for ip in ips:
print(f"{ip} is {classify_ip(ip)}")
# IP to Binary and Integer Conversion
print("\nIP to Binary and Integer Conversion:")
for ip in ips:
print(f"{ip} in binary: {ip_to_binary(ip)}")
print(f"{ip} in integer: {ip_to_integer(ip)}")
# Private IP Check
print("\nPrivate IP Check:")
for ip in ips:
print(f"{ip} is private? {IP(ip).is_private()}")
# Network Info for IPs
print("\nNetwork Info for IPs:")
for ip in ips:
network_info = None
if classify_ip(ip) == "Private":
network_info = {'network': ipaddress.IPv4Network(f'{ip}/24', strict=False).network_address, 'host': ip}
print(f"{ip} Network Info: {network_info}")
# CIDR to IP Range
print("\nCIDR to IP Range Conversion:")
print(f"192.168.1.0/24 -> Network Range: {cidr_to_ip_range('192.168.1.0/24')}")
# Ping a Host
print("\nPing Test:")
print(f"Ping 8.8.8.8: {ping_ip('8.8.8.8')}")
# Reverse DNS Lookup
print("\nReverse DNS Lookup:")
print(f"Reverse DNS for 8.8.8.8: {reverse_dns_lookup('8.8.8.8')}")
# Mask IP
print("\nMask IP:")
print(f"Masking 192.168.1.1: {mask_ip('192.168.1.1')}")
# Batch IP Validation
print("\nBatch IP Validation:")
ips_to_validate = ["192.168.1.1", "8.8.8.8", "invalid_ip"]
print(validate_ip_batch(ips_to_validate))
Contributing
If you'd like to contribute to KryoIP, feel free to open an issue or submit a pull request. All contributions are welcome!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kryoip-6.post4.tar.gz.
File metadata
- Download URL: kryoip-6.post4.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aea90c6d98a78d4b279c9344b8421f126508c95111039d34846bb35b9cacc2c8
|
|
| MD5 |
6748aca63303c9287c3a85019574840a
|
|
| BLAKE2b-256 |
16db6307944feeb49abf2b4749abd78cd8130ef72c375ec4e3413f3b4f28fc86
|
File details
Details for the file kryoip-6.post4-py3-none-any.whl.
File metadata
- Download URL: kryoip-6.post4-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b97465b6ff60cf6419cc968b442c1b89a0212d11a613469e7f63f86b933d7fa
|
|
| MD5 |
6f4c0e31511fbeb3c96442ea90bac6de
|
|
| BLAKE2b-256 |
0bb2a67d7acc815cee70ded15d70201df8f35f9a9748488e381ff950cbcf2790
|