title: Get subnet from IP address
body: <p>I am trying to get subnet for IP address I have.</p>
  
  <p>Eg :</p>
  
  <p><strong>1</strong></p>
  
  <p>Subnet mask : <code>255.255.255.0</code></p>
  
  <p>Input : <code>192.178.2.55</code></p>
  
  <p>Output : <code>192.178.2.0</code></p>
  
  <p><strong>2</strong></p>
  
  <p>Subnet mask : <code>255.255.0.0</code></p>
  
  <p>Input : <code>192.178.2.55</code></p>
  
  <p>Output : <code>192.178.0.0</code></p>
  
  <p>Currently, the way I do it (for subnet 255.255.255.0)</p>
  
  <pre><code>ip =  '192.178.2.55'
  subnet = '.'.join(ip.split('.')[:2]) + '.0.0'
  subnet
  '192.178.0.0'
  </code></pre>
  
  <p>I see python has <strong>ipaddress</strong> library. However I could not find a method that could do the above task.</p>
  
  <p>Bonus : Since <strong>ipaddress</strong> supports IPv4 and IPv6, if the same function could be used for both.</p>