Is there a function of ipaddress to get the subnet only from input like 192.168.1.129/25

Peter Otten __peter__ at web.de
Tue Oct 17 13:45:38 EDT 2017


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 you have

>>> import ipaddress
>>> n = ipaddress.ip_network("192.168.1.128/25")
>>> n
IPv4Network('192.168.1.128/25')

? Then one

>>> dir(n)

[snip private names]

'_version', 'address_exclude', 'broadcast_address', 'compare_networks', 
'compressed', 'exploded', 'hostmask', 'hosts', 'is_global', 'is_link_local', 
'is_loopback', 'is_multicast', 'is_private', 'is_reserved', 
'is_unspecified', 'max_prefixlen', 'netmask', 'network_address', 
'num_addresses', 'overlaps', 'prefixlen', 'subnets', 'supernet', 'version', 
'with_hostmask', 'with_netmask', 'with_prefixlen']

later:

>>> n.network_address
IPv4Address('192.168.1.128')





More information about the Python-list mailing list