for a 'good python'

Lars Liedtke lal at solute.de
Thu Apr 13 03:42:24 EDT 2023




Lars Liedtke
Software Entwickler

[Tel.]  +49 721 98993-
[Fax]   +49 721 98993-
[E-Mail]        lal at solute.de<mailto:lal at solute.de>


solute GmbH
Zeppelinstraße 15
76185 Karlsruhe
Germany


[Logo Solute]


Marken der solute GmbH | brands of solute GmbH
[Marken]
[Advertising Partner]

Geschäftsführer | Managing Director: Dr. Thilo Gans, Bernd Vermaaten
Webseite | www.solute.de <http://www.solute.de/>
Sitz | Registered Office: Karlsruhe
Registergericht | Register Court: Amtsgericht Mannheim
Registernummer | Register No.: HRB 110579
USt-ID | VAT ID: DE234663798



Informationen zum Datenschutz | Information about privacy policy
https://www.solute.de/ger/datenschutz/grundsaetze-der-datenverarbeitung.php




Am 13.04.23 um 00:21 schrieb jak:
Barry ha scritto:



On 12 Apr 2023, at 18:10, jak <nospam at please.ty><mailto:nospam at please.ty> wrote:
Hi everyone,
some time ago I wrote a class to determine if an ipv4 address belonged
to a subnet. Seldom using python I'm pretty sure it's not written in
'good python' nor too portable. Could you give me some advice to make it
better?

class calcip:
    def __init__(self, psubnet: str):
        ssubnet, scidr = psubnet.replace(' ', '').split('/')
        subnet = int.from_bytes(tuple(
                                  map(lambda n: (int(n)), ssubnet.split('.'))),
                                'big')
        cidr = int(scidr)
        mask = ((2 ** cidr) - 1) << (32 - cidr)
        self.__network = subnet & mask
        self.__wildcard = ~mask & 0xffffffff
        self.__broadcast = (subnet | self.__wildcard) & 0xffffffff
        self.__tsubnet = tuple(subnet.to_bytes(4, 'big'))
        self.__tnetwork = tuple(self.__network.to_bytes(4, 'big'))
        self.__tbroadcast = tuple(self.__broadcast.to_bytes(4, 'big'))
        self.__tmask = tuple(mask.to_bytes(4, 'big'))
        self.__twildcard = tuple(self.__wildcard.to_bytes(4, 'big'))
        self.__host_min = tuple((self.__network + 1).to_bytes(4, 'big'))
        self.__host_max = tuple((self.__broadcast - 1).to_bytes(4, 'big'))

    @staticmethod
    def __to_str(val: tuple):
        return '.'.join(str(v) for v in val)

    @property
    def subnet(self):
        return self.__to_str(self.__tsubnet)

    @property
    def network(self):
        return self.__to_str(self.__tnetwork)

    @property
    def broadcast(self):
        return self.__to_str(self.__tbroadcast)

    @property
    def mask(self):
        return self.__to_str(self.__tmask)

    @property
    def wildcard(self):
        return self.__to_str(self.__twildcard)

    @property
    def host_min(self):
        return self.__to_str(self.__host_min)

    @property
    def host_max(self):
        return self.__to_str(self.__host_max)

    @property
    def hosts_num(self):
        return self.__wildcard - 1

    @property
    def net_class(self):
        tst = (self.__tnetwork[0] & 0xf0) >> 4
        if (tst & 0x8) == 0:
            clx = 'A'
        elif (tst & 0xc) == 0x8:
            clx = 'B'
        elif (tst & 0xe) == 0xc:
            clx = 'C'
        elif (tst & 0xf) == 0xe:
            clx = 'D'
        elif (tst & 0xf) == 0xf:
            clx = 'E'
        return clx

    def __contains__(self, item):
        ret = True
        row_hdr = None
        try:
            row_hdr = int.from_bytes(tuple(map(lambda n: (int(n)), item.split('.'))), 'big')
        except:
            ret = False
        if ret:
            if not self.__network < row_hdr < self.__broadcast:
                ret = False
        return ret


def main():
    sn = calcip('10.0.0.0/26')

    print(f"subnet: {sn.subnet}")
    print(f"network: {sn.network}")
    print(f"broadcast: {sn.broadcast}")
    print(f"mask: {sn.mask}")
    print(f"wildcard: {sn.wildcard}")
    print(f"host_min: {sn.host_min}")
    print(f"host_max: {sn.host_max}")
    print(f"Avaible hosts: {sn.hosts_num}")
    print(f"Class: {sn.net_class}")

    tst_hdr = '10.0.0.31'
    is_not = 'is '
    if not tst_hdr in sn:
        is_not = 'is NOT '
    print("hdr %s %sin range %s - %s" %
          (tst_hdr, is_not, sn.host_min, sn.host_max))

if __name__ == '__main__':
    main()

There is this https://docs.python.org/3/howto/ipaddress.html if you just want a solution.

Or are you after code review feedback?

Barry

--
https://mail.python.org/mailman/listinfo/python-list


Thank you too. I had seen this library but I always try not to use
libraries outside the standard ones. Now I don't remember why I was
convinced that this wasn't part of it, perhaps because it was like that
at the time or because I got confused. Only now I realized that it is
not necessary to install it. Now I'm considering whether to use
'ipaddress' or 'socket'. What is certain is that this one you have
suggested is really comfortable. Thanks again for the report.

Unless I am not mistakes, ipadress is "standard" because it is in the standard library


More information about the Python-list mailing list