Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kryoip-6.post3.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kryoip-6.post3-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file kryoip-6.post3.tar.gz.

File metadata

  • Download URL: kryoip-6.post3.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for kryoip-6.post3.tar.gz
Algorithm Hash digest
SHA256 5b1be14ce2ce84c3ae8a989e5d1defdc19ac2c0ddf2c9e08c6a456febd7b8ae5
MD5 4f3f99838d704b6eefd4a3835755f24d
BLAKE2b-256 7e93209beda3197ee374940a9c090dbf3bd1b81d173dd38b684c973ad9fcb1f8

See more details on using hashes here.

File details

Details for the file kryoip-6.post3-py3-none-any.whl.

File metadata

  • Download URL: kryoip-6.post3-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

Hashes for kryoip-6.post3-py3-none-any.whl
Algorithm Hash digest
SHA256 a9c901676c917a732225b9c38edd6b627097bb470cc08eab245678705ee03e4c
MD5 905ddcbca66cb773f74cf0cd8eff8952
BLAKE2b-256 5f0fc688256b8df072e9e9a63ed032f8e8d292ab9d295cd3d76e34068b151c0b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page