简体   繁体   English

使用python在CIDR中的IP地址和掩码

[英]ip address and mask in CIDR with python

I would like to convert ipaddress/mask in ipaddress/CIDR: For that two variables:我想在 ipaddress/CIDR 中转换 ipaddress/mask:对于这两个变量:

i = '192.168.0.0'
m = '255.255.255.0'

The result I'm trying to obtain is:我试图获得的结果是:

p to be: '192.168.0.0/24'

by using ipaddress module it would be super simple to just define like:通过使用 ipaddress 模块,可以非常简单地定义如下:

p = ipaddress.ip_network(i/m)

unfortunately, I get TypeError: unsupported operand type(s) for /: 'str' and 'str' which is not surprising.不幸的是,我得到 TypeError: unsupported operand type(s) for /: 'str' and 'str' 这并不奇怪。

How could I do that?我怎么能那样做?

It works with:它适用于:

p = ipaddress.ip_network(i+"/"+m)

Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM