An open API service indexing awesome lists of open source software.

https://github.com/pykit3/k3net

Utility functions for network related operation.
https://github.com/pykit3/k3net

net python

Last synced: 5 months ago
JSON representation

Utility functions for network related operation.

Awesome Lists containing this project

README

          

# k3net

[![Action-CI](https://github.com/pykit3/k3net/actions/workflows/python-package.yml/badge.svg)](https://github.com/pykit3/k3net/actions/workflows/python-package.yml)
[![Documentation Status](https://readthedocs.org/projects/k3net/badge/?version=stable)](https://k3net.readthedocs.io/en/stable/?badge=stable)
[![Package](https://img.shields.io/pypi/pyversions/k3net)](https://pypi.org/project/k3net)

Utility functions for network related operation.

k3net is a component of [pykit3] project: a python3 toolkit set.

# Install

```
pip install k3net
```

# Synopsis

```python

#!/usr/bin/env python

import k3net

if __name__ == "__main__":

# Check if an IP is valid IPv4
print(k3net.is_ip4("192.168.0.1")) # True
print(k3net.is_ip4("invalid")) # False

# Check if IP is public or private (INN)
print(k3net.is_pub("1.2.3.4")) # True
print(k3net.is_inn("192.168.0.1")) # True

# Get IP class
print(k3net.ip_class("1.2.3.4")) # PUB
print(k3net.ip_class("192.168.0.1")) # INN

# Select IPs by class
ips = ["1.2.3.4", "192.168.0.1", "10.0.0.1"]
print(k3net.choose_pub(ips)) # ['1.2.3.4']
print(k3net.choose_inn(ips)) # ['192.168.0.1', '10.0.0.1']

# Prefer public or private IPs
print(k3net.ips_prefer(ips, k3net.PUB)) # Public IPs first
print(k3net.ips_prefer(ips, k3net.INN)) # Private IPs first

# Get host IP addresses
host_ips = k3net.get_host_ip4()
print(host_ips) # List of host IPv4 addresses

# IP to number conversion
num = k3net.ip_to_num("192.168.0.1")
print(num) # 3232235521
print(k3net.num_to_ip(num)) # '192.168.0.1'

```

# Author

Zhang Yanpo (张炎泼)

# Copyright and License

The MIT License (MIT)

Copyright (c) 2015 Zhang Yanpo (张炎泼)

[pykit3]: https://github.com/pykit3