Created
August 20, 2025 15:28
-
-
Save Endermanch/53e415e7b609225a8117b4a2cdfe7bce to your computer and use it in GitHub Desktop.
Swag and unswag your IP address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def swag(ip: str) -> int: | |
parts = ip.strip().split('.') | |
if len(parts) != 4: | |
raise ValueError("Invalid IPv4 address: must have 4 octets") | |
total = 0 | |
for i, p in enumerate(parts): | |
if not p.isdigit(): | |
raise ValueError(f"Octet {i + 1} is not a number") | |
n = int(p) | |
if not (0 <= n <= 255): | |
raise ValueError(f"Octet {i + 1} out of range (0-255)") | |
total = (total << 8) | n | |
if (total >> 24) == 0: | |
raise ValueError("First octet cannot be 0") | |
return total | |
def unswag(num: int) -> str: | |
if not (0x01000000 <= num <= 0xFFFFFFFF): | |
raise ValueError("Number out of IPv4 range (16777216–4294967295)") | |
octets = [] | |
for shift in (24, 16, 8, 0): | |
octets.append(str((num >> shift) & 0xFF)) | |
return ".".join(octets) | |
def main(): | |
print("1. IP to Decimal") | |
print("2. Decimal to IP") | |
try: | |
mode = int(input("Enter mode (1 or 2): ").strip()) | |
if mode == 1: | |
ip = input("Enter IPv4 address: ").strip() | |
print(swag(ip)) | |
elif mode == 2: | |
num = int(input("Enter decimal number: ").strip()) | |
print(unswag(num)) | |
else: | |
print("Invalid mode. Please enter 1 or 2.") | |
except Exception as e: | |
print(f"Error: {e}") | |
if __name__ == "__main__": | |
main() |
monster manch w
what should i have for dinner
Noice 1
swag is the best function name I’ve ever seen. Congrats
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
super cool video