Tuesday, 15 February 2011

python - Abbreviating IPv6 addresses -


There is an array with IP address I need a selected IP address to shorten. For example:

  ['fcef: b0e7: 7d20: 0000: 0000: 0000: 3b95: 0565']  

Short rule: If any part Dell is needed from start 0 part or for example there is 0000 to convert it to 0 . The last example after the abbreviation:

  ['fcef: b0e7: 7d20: 0: 0: 0: 3b95: 565']  

You should use the module from the standard library:

  & gt; & Gt; & Gt; Import an iPad & gt; & Gt; & Gt; Str (ipaddress.ip_address ('fcef: b0e7: 7d20: 0000: 0000: 0000: 3b95: 0565')) 'fcef: b0e7: 7d20 :: 3b95: 565' & gt; & Gt; & Gt; Ip = ipaddress.ip_address ('fcef: b0e7: 7d20: 0000: 0000: 0000: 3b95: 0565') & gt; & Gt; & Gt; Ip.compressed 'fcef: b0e7: 7d20 :: 3b95: 565' & gt; & Gt; & Gt; Ip.exploded 'fcef: b0e7: 7d20: 0000: 0000: 0000: 3b95: 0565'  

This makes the IP smaller as per the actual rules.

With the only rule to minimize the IP, you can simply replace :

  gt; & Gt; & Gt; 'Fcef: b0e7: 7d20: 0000: 0000: 0000: 3b95: 0565'. ('0000', '0') 'fcef: b0e7: 7d20: 0: 0: 3b95: 0565' ' / pre> 

No comments:

Post a Comment