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
Metadata
Release files for myserverip 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| myserverip-1.0.0.tar.gz | 7.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| myserverip-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.2 kB
Release files / myserverip-1.0.0.tar.gz
| Download URL | myserverip-1.0.0.tar.gz |
|---|---|
| Size | 7.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0d2efe5ae0eb8319c518a302e9a244023b91a97972d060a1c4443783d3d96ac2
|
|
BLAKE2b-256 checksum How to use checksums |
8c6f67dde291057479205a59d73a64d647dac86acd01bcfed91de68e4c10481f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / myserverip-1.0.0-py3-none-any.whl
| Download URL | myserverip-1.0.0-py3-none-any.whl |
|---|---|
| Size | 7.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9dc8bd75ea034f5faf047bfe435fe7af9b6bfeee19773765c1bd890c0e7bf644
|
|
BLAKE2b-256 checksum How to use checksums |
6d26ad9459b31414fb44a9ef9114f07a1606b903c4f5fcfd2fa35f2c6a9daf8d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|