Hit wrong button before. ---------- Forwarded message ---------- From: Nathan Hilterbrand <nhilterbr...@gmail.com> Date: Tue, Oct 17, 2017 at 2:22 PM Subject: Re: Is there a function of ipaddress to get the subnet only from input like 192.168.1.129/25 To: Rob Gaddi <rgaddi@highlandtechnology.invalid>
I may have misunderstood what you were looking for, but went ahead and cobbled this ugly function together. Takes a string like "192.168.1.128/25" as input, and returns the subnet address. Just slapped together, so not the most robust thing in the world def subnet(inp): addr, bits = inp.split('/') mask = 2 ** (32 - int(bits)) - 1 allones = (2 ** 32) - 1 mask = allones ^ mask addroctets = [int(o) for o in addr.split('.')] addrval = 0 for o in addroctets: addrval = addrval * 256 + o snval = addrval & mask snoctets = [] q = snval for i in range(4): q,r = divmod(q, 256) snoctets.insert(0,str(r)) sn = ".".join(snoctets) return sn On Tue, Oct 17, 2017 at 1:36 PM, Rob Gaddi <rgaddi@highlandtechnology. invalid> wrote: > On 10/17/2017 09:59 AM, Daniel Flick wrote: > >> I am very new to Python and have been struggling to find some info on >> processing IP addresses. >> >> get_network returns 192.168.1.128/25 but I need 192.168.1.128 only. I >> can do this with netaddr but I am working with Mako templates and ipaddress >> is a built in module so there are less dependencies. >> >> Any ideas? >> >> > You mean, other than .split('/')? > > -- > Rob Gaddi, Highland Technology -- www.highlandtechnology.com > Email address domain is currently out of order. See above to fix. > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list