Chinaunix首页 | 论坛 | 博客
  • 博客访问: 482039
  • 博文数量: 108
  • 博客积分: 25
  • 博客等级: 民兵
  • 技术积分: 1134
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-29 19:43
文章分类

全部博文(108)

文章存档

2016年(10)

2015年(9)

2014年(73)

2013年(16)

我的朋友

分类: Python/Ruby

2014-11-11 15:01:12

中午突然想起了如何比较2个IP是否在同一个subnet。简单的找了下 找到了Python的netaddr模块。基本的用法如下:

点击(此处)折叠或打开

  1. >>> ip=IPNetwork('10.10.10.2/25')
  2. >>> ab=IPNetwork('10.10.10.200/25')
  3. >>> ip.network
  4. IPAddress('10.10.10.0')
  5. >>> ab.network
  6. IPAddress('10.10.10.128')
  7. >>> ab=IPNetwork('10.10.10.130/25')
  8. >>> ab.network
  9. IPAddress('10.10.10.128')
  10. >>> ab.network == ip.network
  11. False
  12. >>>

下面是netaddr的一些基本用法

点击(此处)折叠或打开

  1. >>> from netaddr import *
  2. >>> import pprint
  3. >>> ip=IPAddress('192.168.1.1')
  4. >>> ip.version
  5. 4
  6. >>> repr(ip)
  7. "IPAddress('192.168.1.1')"
  8. >>> ip
  9. IPAddress('192.168.1.1')
  10. >>> str(ip)
  11. '192.168.1.1'
  12. >>> '%s' % ip
  13. '192.168.1.1'
  14. >>> ip.format()
  15. '192.168.1.1'
  16. >>> int(ip)
  17. 3232235777
  18. >>> hex(ip)
  19. '0xc0a80101'
  20. >>> ip.bin
  21. '0b11000000101010000000000100000001'
  22. >>> ip.bits()
  23. '11000000.10101000.00000001.00000001'
  24. >>> ip.words
  25. (192, 168, 1, 1)
  26. >>> ip.network, ip.broadcast
  27. Traceback (most recent call last):
  28.   File "", line 1, in <module>
  29. AttributeError: 'IPAddress' object has no attribute 'network'
  30. >>> ip=IPNetwork('192.0.2.1')
  31. >>> ip.op
  32. Traceback (most recent call last):
  33.   File "", line 1, in <module>
  34. AttributeError: 'IPNetwork' object has no attribute 'op'
  35. >>> ip.ip
  36. IPAddress('192.0.2.1')
  37. >>> ip.network,ip.broadcast
  38. (IPAddress('192.0.2.1'), IPAddress('192.0.2.1'))
  39. >>> ip.netmask, ip.hostmask
  40. (IPAddress('255.255.255.255'), IPAddress('0.0.0.0'))
  41. >>> ip.size
  42. 1
  43. >>> ip=IPNetwork('192.179.0.1/24')
  44. >>> ip.ip
  45. IPAddress('192.179.0.1')
  46. >>> ip.network
  47. IPAddress('192.179.0.0')
  48. >>> ip.broadcast
  49. IPAddress('192.179.0.255')
  50. >>> ip.netmask,ip.hostmask
  51. (IPAddress('255.255.255.0'), IPAddress('0.0.0.255'))
  52. >>> ip=IPNetwork('0.0.0.0/0')
  53. >>> ip
  54. IPNetwork('0.0.0.0/0')
  55. >>> ip.value=3221225985
  56. >>> ip
  57. IPNetwork('192.0.2.1/0')
  58. >>> ip.prefixlen
  59. 0
  60. >>> ip.prefixlen = 23
  61. >>> ip
  62. IPNetwork('192.0.2.1/23')
  63. >>> ip.cidr
  64. IPNetwork('192.0.2.0/23')
  65. >>> ip.ip.bits()
  66. '11000000.00000000.00000010.00000001'
  67. >>> ip.network.bits()
  68. '11000000.00000000.00000010.00000000'
  69. >>> ip.netmask.bits()
  70. '11111111.11111111.11111110.00000000'
  71. >>> ip.broadcast.bits()
  72. '11000000.00000000.00000011.11111111'
  73. >>> import random
  74. >>> ip_list=list(IPNetwork('192.0.2.198/28'))
  75. >>> random.shuffle(ip_list)
  76. >>> sorted(ip_list)
  77. [IPAddress('192.0.2.192'), IPAddress('192.0.2.193'), IPAddress('192.0.2.194'), IPAddress('192.0.2.195'), IPAddress('192.0.2.196'), IPAddress('192.0.2.197'), IPAddress('192.0.2.198'), IPAddress('192.0.2.199'), IPAddress('192.0.2.200'), IPAddress('192.0.2.201'), IPAddress('192.0.2.202'), IPAddress('192.0.2.203'), IPAddress('192.0.2.204'), IPAddress('192.0.2.205'), IPAddress('192.0.2.206'), IPAddress('192.0.2.207')]
  78. >>> ip_list=[ ip for ip in IPNetwork('fe80::/120')]
  79. >>> ip=IPNetwork('172.24.0.0/16')
  80. >>> ip.subnet(23)
  81. <generator object subnet at 0x7f63d0615a00>
  82. >>> subnets=list(ip.subnet(23))
  83. >>> len(subnets)
  84. 128
  85. >>> subnets
  86. [IPNetwork('172.24.0.0/23'), IPNetwork('172.24.2.0/23'), IPNetwork('172.24.4.0/23'), IPNetwork('172.24.6.0/23'), IPNetwork('172.24.8.0/23'), IPNetwork('172.24.10.0/23'), IPNetwork('172.24.12.0/23'), IPNetwork('172.24.14.0/23'), IPNetwork('172.24.16.0/23'), IPNetwork('172.24.18.0/23'), IPNetwork('172.24.20.0/23'), IPNetwork('172.24.22.0/23'), IPNetwork('172.24.24.0/23'), IPNetwork('172.24.26.0/23'), IPNetwork('172.24.28.0/23'), IPNetwork('172.24.30.0/23'), IPNetwork('172.24.32.0/23'), IPNetwork('172.24.34.0/23'), IPNetwork('172.24.36.0/23'), IPNetwork('172.24.38.0/23'), IPNetwork('172.24.40.0/23'), IPNetwork('172.24.42.0/23'), IPNetwork('172.24.44.0/23'), IPNetwork('172.24.46.0/23'), IPNetwork('172.24.48.0/23'), IPNetwork('172.24.50.0/23'), IPNetwork('172.24.52.0/23'), IPNetwork('172.24.54.0/23'), IPNetwork('172.24.56.0/23'), IPNetwork('172.24.58.0/23'), IPNetwork('172.24.60.0/23'), IPNetwork('172.24.62.0/23'), IPNetwork('172.24.64.0/23'), IPNetwork('172.24.66.0/23'), IPNetwork('172.24.68.0/23'), IPNetwork('172.24.70.0/23'), IPNetwork('172.24.72.0/23'), IPNetwork('172.24.74.0/23'), IPNetwork('172.24.76.0/23'), IPNetwork('172.24.78.0/23'), IPNetwork('172.24.80.0/23'), IPNetwork('172.24.82.0/23'), IPNetwork('172.24.84.0/23'), IPNetwork('172.24.86.0/23'), IPNetwork('172.24.88.0/23'), IPNetwork('172.24.90.0/23'), IPNetwork('172.24.92.0/23'), IPNetwork('172.24.94.0/23'), IPNetwork('172.24.96.0/23'), IPNetwork('172.24.98.0/23'), IPNetwork('172.24.100.0/23'), IPNetwork('172.24.102.0/23'), IPNetwork('172.24.104.0/23'), IPNetwork('172.24.106.0/23'), IPNetwork('172.24.108.0/23'), IPNetwork('172.24.110.0/23'), IPNetwork('172.24.112.0/23'), IPNetwork('172.24.114.0/23'), IPNetwork('172.24.116.0/23'), IPNetwork('172.24.118.0/23'), IPNetwork('172.24.120.0/23'), IPNetwork('172.24.122.0/23'), IPNetwork('172.24.124.0/23'), IPNetwork('172.24.126.0/23'), IPNetwork('172.24.128.0/23'), IPNetwork('172.24.130.0/23'), IPNetwork('172.24.132.0/23'), IPNetwork('172.24.134.0/23'), IPNetwork('172.24.136.0/23'), IPNetwork('172.24.138.0/23'), IPNetwork('172.24.140.0/23'), IPNetwork('172.24.142.0/23'), IPNetwork('172.24.144.0/23'), IPNetwork('172.24.146.0/23'), IPNetwork('172.24.148.0/23'), IPNetwork('172.24.150.0/23'), IPNetwork('172.24.152.0/23'), IPNetwork('172.24.154.0/23'), IPNetwork('172.24.156.0/23'), IPNetwork('172.24.158.0/23'), IPNetwork('172.24.160.0/23'), IPNetwork('172.24.162.0/23'), IPNetwork('172.24.164.0/23'), IPNetwork('172.24.166.0/23'), IPNetwork('172.24.168.0/23'), IPNetwork('172.24.170.0/23'), IPNetwork('172.24.172.0/23'), IPNetwork('172.24.174.0/23'), IPNetwork('172.24.176.0/23'), IPNetwork('172.24.178.0/23'), IPNetwork('172.24.180.0/23'), IPNetwork('172.24.182.0/23'), IPNetwork('172.24.184.0/23'), IPNetwork('172.24.186.0/23'), IPNetwork('172.24.188.0/23'), IPNetwork('172.24.190.0/23'), IPNetwork('172.24.192.0/23'), IPNetwork('172.24.194.0/23'), IPNetwork('172.24.196.0/23'), IPNetwork('172.24.198.0/23'), IPNetwork('172.24.200.0/23'), IPNetwork('172.24.202.0/23'), IPNetwork('172.24.204.0/23'), IPNetwork('172.24.206.0/23'), IPNetwork('172.24.208.0/23'), IPNetwork('172.24.210.0/23'), IPNetwork('172.24.212.0/23'), IPNetwork('172.24.214.0/23'), IPNetwork('172.24.216.0/23'), IPNetwork('172.24.218.0/23'), IPNetwork('172.24.220.0/23'), IPNetwork('172.24.222.0/23'), IPNetwork('172.24.224.0/23'), IPNetwork('172.24.226.0/23'), IPNetwork('172.24.228.0/23'), IPNetwork('172.24.230.0/23'), IPNetwork('172.24.232.0/23'), IPNetwork('172.24.234.0/23'), IPNetwork('172.24.236.0/23'), IPNetwork('172.24.238.0/23'), IPNetwork('172.24.240.0/23'), IPNetwork('172.24.242.0/23'), IPNetwork('172.24.244.0/23'), IPNetwork('172.24.246.0/23'), IPNetwork('172.24.248.0/23'), IPNetwork('172.24.250.0/23'), IPNetwork('172.24.252.0/23'), IPNetwork('172.24.254.0/23')]
  87. >>>
参考链接:

阅读(6755) | 评论(0) | 转发(0) |
0

上一篇:Python re.match

下一篇:Python socket 编程

给主人留下些什么吧!~~