Skip to main content
IP is a protocol; it is IP addresses that are subnetted, static, filtered, whitelisted, have different representations, that devices have, etc., not the protocol itself. Removed meta information (this belongs in comments) - this is not a forum (see e.g. <http://meta.stackexchange.com/a/92115>).
Source Link

Simple IP address subnet calculator

Hello everyone

In this task I had to create simple IP address / subnet calculator in Python. I'm just wondering how you see this problem.

There is my code:

def toBinary(integer):
    binary = ['{0:0>8}'.format(bin(int(x))[2:]) for x in integer.split(".")]
    return binary

def ip_information(ip, mask):
    binary_ip = toBinary(ip)
    binary_mask = toBinary(mask)
    network_ip = ["", "", "", ""]
    broadcast_address = ["", "", "", ""]
    number_of_hosts = 1

    for x in range(4):
        for y in range(8):
            network_ip[x] += str(int(binary_ip[x][y]) and int(binary_mask[x][y]))
            broadcast_address[x] += str(int(not int(binary_mask[x][y])))
            if binary_mask[x][y] == '0':
                number_of_hosts *= 2
                
        network_ip[x] = int(network_ip[x], 2)
        broadcast_address[x] = int(broadcast_address[x], 2) + network_ip[x]
    
    return f"""
    Network IP: {".".join(str(x) for x in network_ip)}
    Broadcast address: {".".join(str(x) for x in broadcast_address)}
    Number of hosts: {number_of_hosts - 2}
    """

Output:

    Network IP: 192.168.0.0
    Broadcast address: 192.168.0.255
    Number of hosts: 254

Any tips on how to make this better, closer to aan advanced (but still, can take under account to make it simple) or? Or just a better solution will be definitely on point. Thanks in advance!

usingUsing Python 3.10

Enjoy your coffee, and your wednesday!.

Simple IP subnet calculator

Hello everyone

In this task I had to create simple IP / subnet calculator in Python. I'm just wondering how you see this problem.

There is my code:

def toBinary(integer):
    binary = ['{0:0>8}'.format(bin(int(x))[2:]) for x in integer.split(".")]
    return binary

def ip_information(ip, mask):
    binary_ip = toBinary(ip)
    binary_mask = toBinary(mask)
    network_ip = ["", "", "", ""]
    broadcast_address = ["", "", "", ""]
    number_of_hosts = 1

    for x in range(4):
        for y in range(8):
            network_ip[x] += str(int(binary_ip[x][y]) and int(binary_mask[x][y]))
            broadcast_address[x] += str(int(not int(binary_mask[x][y])))
            if binary_mask[x][y] == '0':
                number_of_hosts *= 2
                
        network_ip[x] = int(network_ip[x], 2)
        broadcast_address[x] = int(broadcast_address[x], 2) + network_ip[x]
    
    return f"""
    Network IP: {".".join(str(x) for x in network_ip)}
    Broadcast address: {".".join(str(x) for x in broadcast_address)}
    Number of hosts: {number_of_hosts - 2}
    """

Output:

    Network IP: 192.168.0.0
    Broadcast address: 192.168.0.255
    Number of hosts: 254

Any tips how to make this better, closer to a advanced (but still, can take under account to make it simple) or just better solution will be definitely on point. Thanks in advance!

using Python 3.10

Enjoy your coffee, and your wednesday!

Simple IP address subnet calculator

In this task I had to create simple IP address / subnet calculator in Python. I'm just wondering how you see this problem.

There is my code:

def toBinary(integer):
    binary = ['{0:0>8}'.format(bin(int(x))[2:]) for x in integer.split(".")]
    return binary

def ip_information(ip, mask):
    binary_ip = toBinary(ip)
    binary_mask = toBinary(mask)
    network_ip = ["", "", "", ""]
    broadcast_address = ["", "", "", ""]
    number_of_hosts = 1

    for x in range(4):
        for y in range(8):
            network_ip[x] += str(int(binary_ip[x][y]) and int(binary_mask[x][y]))
            broadcast_address[x] += str(int(not int(binary_mask[x][y])))
            if binary_mask[x][y] == '0':
                number_of_hosts *= 2

        network_ip[x] = int(network_ip[x], 2)
        broadcast_address[x] = int(broadcast_address[x], 2) + network_ip[x]

    return f"""
    Network IP: {".".join(str(x) for x in network_ip)}
    Broadcast address: {".".join(str(x) for x in broadcast_address)}
    Number of hosts: {number_of_hosts - 2}
    """

Output:

    Network IP: 192.168.0.0
    Broadcast address: 192.168.0.255
    Number of hosts: 254

Any tips on how to make this better, closer to an advanced (but still, can take under account to make it simple)? Or just a better solution will be definitely on point.

Using Python 3.10.

edited tags
Link
Tweeted twitter.com/StackCodeReview/status/1481279863575175174
Became Hot Network Question
deleted 5 characters in body
Source Link

Hello everyone

In this task I had to create simple IP / subnet calculator in Python. I'm just wondering how you see this problem.

There is my code:

def toBinary(integer):
    binary = ['{0:0>8}'.format(bin(int(x))[2:]) for x in integer.split(".")]
    return binary

def ip_information(ip, mask):
    binary_ip = toBinary(ip)
    binary_mask = toBinary(mask)
    network_ip = ["", "", "", ""]
    broadcast_address = ["", "", "", ""]
    number_of_hosts = 1

    for x in range(4):
        for y in range(8):
            network_ip[x] += str(int(int(binary_ip[x][y]) and int(binary_mask[x][y])))
            broadcast_address[x] += str(int(not int(binary_mask[x][y])))
            if binary_mask[x][y] == '0':
                number_of_hosts *= 2
                
        network_ip[x] = int(network_ip[x], 2)
        broadcast_address[x] = int(broadcast_address[x], 2) + network_ip[x]
    
    return f"""
    Network IP: {".".join(str(x) for x in network_ip)}
    Broadcast address: {".".join(str(x) for x in broadcast_address)}
    Number of hosts: {number_of_hosts - 2}
    """

Output:

    Network IP: 192.168.0.0
    Broadcast address: 192.168.0.255
    Number of hosts: 254

Any tips how to make this better, closer to a advanced (but still, can take under account to make it simple) or just better solution will be definitely on point. Thanks in advance!

using Python 3.10

Enjoy your coffee, and your wednesday!

Hello everyone

In this task I had to create simple IP / subnet calculator in Python. I'm just wondering how you see this problem.

There is my code:

def toBinary(integer):
    binary = ['{0:0>8}'.format(bin(int(x))[2:]) for x in integer.split(".")]
    return binary

def ip_information(ip, mask):
    binary_ip = toBinary(ip)
    binary_mask = toBinary(mask)
    network_ip = ["", "", "", ""]
    broadcast_address = ["", "", "", ""]
    number_of_hosts = 1

    for x in range(4):
        for y in range(8):
            network_ip[x] += str(int(int(binary_ip[x][y]) and int(binary_mask[x][y])))
            broadcast_address[x] += str(int(not int(binary_mask[x][y])))
            if binary_mask[x][y] == '0':
                number_of_hosts *= 2
                
        network_ip[x] = int(network_ip[x], 2)
        broadcast_address[x] = int(broadcast_address[x], 2) + network_ip[x]
    
    return f"""
    Network IP: {".".join(str(x) for x in network_ip)}
    Broadcast address: {".".join(str(x) for x in broadcast_address)}
    Number of hosts: {number_of_hosts - 2}
    """

Output:

    Network IP: 192.168.0.0
    Broadcast address: 192.168.0.255
    Number of hosts: 254

Any tips how to make this better, closer to a advanced (but still, can take under account to make it simple) or just better solution will be definitely on point. Thanks in advance!

using Python 3.10

Enjoy your coffee, and your wednesday!

Hello everyone

In this task I had to create simple IP / subnet calculator in Python. I'm just wondering how you see this problem.

There is my code:

def toBinary(integer):
    binary = ['{0:0>8}'.format(bin(int(x))[2:]) for x in integer.split(".")]
    return binary

def ip_information(ip, mask):
    binary_ip = toBinary(ip)
    binary_mask = toBinary(mask)
    network_ip = ["", "", "", ""]
    broadcast_address = ["", "", "", ""]
    number_of_hosts = 1

    for x in range(4):
        for y in range(8):
            network_ip[x] += str(int(binary_ip[x][y]) and int(binary_mask[x][y]))
            broadcast_address[x] += str(int(not int(binary_mask[x][y])))
            if binary_mask[x][y] == '0':
                number_of_hosts *= 2
                
        network_ip[x] = int(network_ip[x], 2)
        broadcast_address[x] = int(broadcast_address[x], 2) + network_ip[x]
    
    return f"""
    Network IP: {".".join(str(x) for x in network_ip)}
    Broadcast address: {".".join(str(x) for x in broadcast_address)}
    Number of hosts: {number_of_hosts - 2}
    """

Output:

    Network IP: 192.168.0.0
    Broadcast address: 192.168.0.255
    Number of hosts: 254

Any tips how to make this better, closer to a advanced (but still, can take under account to make it simple) or just better solution will be definitely on point. Thanks in advance!

using Python 3.10

Enjoy your coffee, and your wednesday!

Source Link
Loading