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.
- Host: GitHub
- URL: https://github.com/pykit3/k3net
- Owner: pykit3
- License: mit
- Created: 2021-07-23T06:14:11.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2025-08-28T12:37:38.000Z (9 months ago)
- Last Synced: 2025-09-19T11:53:08.224Z (9 months ago)
- Topics: net, python
- Language: Python
- Homepage: https://blog.openacid.com
- Size: 33.2 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# k3net
[](https://github.com/pykit3/k3net/actions/workflows/python-package.yml)
[](https://k3net.readthedocs.io/en/stable/?badge=stable)
[](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