Skip to content

Thinking more…

Just thinking more…

Day: May 13, 2023

Posted on 2023/05/132023/05/13

如何用Python判斷網段是否重疊

來源:python – Check if two CIDR addresses intersect? – Stack Overflow

可以用 ipaddress 這個模組,這在 python3 是內建的。

import ipaddress
n1 = ipaddress.ip_network('192.168.1.0/24')
n2 = ipaddress.ip_network('192.168.2.0/24')
n3 = ipaddress.ip_network('192.168.2.0/25')
print( n1.overlaps(n2) )  # False
print( n1.overlaps(n3) )  # False
print( n2.overlaps(n3) )  # True
print( n2.overlaps(n1) )  # False
May 2023
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
293031  
« Apr   Jun »

站內搜索

Categories

  • .Net
  • Android
  • Blog
  • Boo
  • C/C++
  • Cook
  • Linux 及其相關
  • Lyrics
  • Python
  • Visual FoxPro
  • Wine品嚐美酒
  • 亂亂逛
  • 個人新聞台
  • 理財
  • 資訊相關Idea與筆記
  • 雜記,隨筆
  • 電影流水帳
Privacy Policy Proudly powered by WordPress