A Python library to get server's public IP address from system interfaces
Project description
ServerIP
A Python library to get your server's public IP address directly from system network interfaces - no external API calls required.
Features
- 🚀 Zero external requests - Works completely offline
- 🔍 Multi-platform support - Linux, Windows, macOS
- 📡 All network interfaces - Discover all public IPs on the server
- 🛡️ Private IP detection - Automatically filters out private/local IPs
- 💻 CLI included - Use from command line
- 📊 Comprehensive info - Get interface details, MAC addresses, status
- 🔄 Multiple fallback methods - Uses different techniques for reliability
Installation
pip install serverip
Quick Start
from serverip import get_server_public_ip, get_all_server_ips
# Get the primary public IP of this server
public_ip = get_server_public_ip()
print(f"Server public IP: {public_ip}")
# Get all public IPs assigned to this server
all_ips = get_all_server_ips()
print(f"All public IPs: {all_ips}")
Full API Usage
from serverip import ServerIPLocator
# Create locator instance
locator = ServerIPLocator()
# Get primary public IP
public_ip = locator.get_public_ip()
print(f"Primary public IP: {public_ip}")
# Get all public IPs
all_public_ips = locator.get_all_public_ips()
print(f"All public IPs: {all_public_ips}")
# Get comprehensive server info
info = locator.get_server_info()
print(f"Hostname: {info.hostname}")
print(f"Platform: {info.platform}")
print(f"Public IPs: {info.public_ips}")
print(f"Private IPs: {info.private_ips}")
# Examine network interfaces
for interface in info.interfaces:
print(f"Interface {interface.name}:")
print(f" IP: {interface.ip_address}")
print(f" Public: {interface.is_public}")
print(f" Status: {'UP' if interface.is_up else 'DOWN'}")
print(f" MAC: {interface.mac_address}")
# Get specific interface info
eth0 = locator.get_interface_by_name('eth0')
if eth0:
print(f"eth0 IP: {eth0.ip_address}")
# Get default route interface IP
default_ip = locator.get_default_route_ip()
print(f"Default route IP: {default_ip}")
Command Line Usage
# Get primary public IP
serverip
# Get all public IPs
serverip --all
# Show all network interfaces
serverip --interfaces
# Get comprehensive server information
serverip --info
# Output as JSON
serverip --info --json
Example Output
$ serverip --info
Hostname: web-server-01
Platform: linux
Public IPs: 203.0.113.45, 198.51.100.30
Private IPs: 192.168.1.100, 10.0.0.50
Interfaces:
eth0: 203.0.113.45 (PUBLIC, UP)
eth1: 198.51.100.30 (PUBLIC, UP)
eth2: 192.168.1.100 (PRIVATE, UP)
lo: 127.0.0.1 (PRIVATE, UP)
How It Works
The library uses multiple methods to discover your server's IP addresses:
- Primary method: Uses the
netifaceslibrary to enumerate network interfaces - Linux fallback: Parses
ip addrorifconfigcommand output - Windows fallback: Parses
ipconfigcommand output - macOS fallback: Parses
ifconfigcommand output - Socket fallback: Uses Python's socket library as last resort
All methods automatically filter out:
- Private IP ranges (192.168.x.x, 10.x.x.x, 172.16-31.x.x)
- Loopback addresses (127.x.x.x)
- Link-local addresses (169.254.x.x)
- Multicast and reserved ranges
Use Cases
Perfect for:
- Web servers - Determine your server's public IP for configuration
- Load balancers - Identify which IPs are available for binding
- Monitoring systems - Track server network configuration
- Auto-configuration - Set up services that need to know their public IP
- Security applications - Audit network interfaces
- Docker containers - Discover container's public network access
- Cloud instances - Get public IP without cloud provider APIs
Platform Support
- ✅ Linux - Full support with multiple detection methods
- ✅ Windows - Full support via ipconfig parsing
- ✅ macOS - Full support via ifconfig parsing
- ✅ Other Unix-like - Basic support via socket methods
Data Classes
ServerIPInfo
public_ips: List of public IP addressesprivate_ips: List of private IP addressesinterfaces: List of NetworkInterface objectshostname: Server hostnameplatform: Operating system platform
NetworkInterface
name: Interface name (e.g., 'eth0', 'wlan0')ip_address: IP address assigned to interfacenetmask: Network maskis_public: Whether IP is public (not private/local)is_up: Whether interface is activemac_address: MAC address (if available)
Error Handling
The library is designed to be robust:
try:
ip = get_server_public_ip()
if ip:
print(f"Public IP: {ip}")
else:
print("No public IP found - server may be behind NAT")
except Exception as e:
print(f"Error getting IP: {e}")
Requirements
- Python 3.6+
- netifaces library (automatically installed)
Security Note
This library only reads network interface information that's already available to your application. It doesn't make any network requests or access sensitive system resources.
License
MIT License - see LICENSE file for details.
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if needed
- Submit a pull request
Changelog
v1.0.0
- Initial release
- Multi-platform support (Linux, Windows, macOS)
- Network interface enumeration
- Public/private IP detection
- CLI interface
- Comprehensive server information
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 myserverip-1.0.0.tar.gz.
File metadata
- Download URL: myserverip-1.0.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d2efe5ae0eb8319c518a302e9a244023b91a97972d060a1c4443783d3d96ac2
|
|
| MD5 |
399779bcf5d42d54f74f96c76a430bcd
|
|
| BLAKE2b-256 |
8c6f67dde291057479205a59d73a64d647dac86acd01bcfed91de68e4c10481f
|
File details
Details for the file myserverip-1.0.0-py3-none-any.whl.
File metadata
- Download URL: myserverip-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9dc8bd75ea034f5faf047bfe435fe7af9b6bfeee19773765c1bd890c0e7bf644
|
|
| MD5 |
59472364442abb9cfaec76bccd6faa7c
|
|
| BLAKE2b-256 |
6d26ad9459b31414fb44a9ef9114f07a1606b903c4f5fcfd2fa35f2c6a9daf8d
|