将 IP 和子网掩码存储在变量中并在 Python 中对其进行编辑

store IP and subnet mask number in a variable and edit on it in Python

我想编写一个应用程序来帮助我确定 class IP 并对其进行编辑,就像我使用 IP class 子网掩码为 255.0 的“10.0.0.0”一样。 0.0 我想让用户像上面那样输入他的 IP 和子网掩码,我做了一个等式来告诉他他可以使用多少个 IP,在我的搜索过程中我得到了这个代码

import sys

sys.stdout.write("Enter IP address: ")
sys.stdout.flush()
ip = sys.stdin.readline()
print("you entered: " + ip)

但是当我使用它时,我无法通过该代码对 ip 进行任何编辑

import sys

sys.stdout.write("Enter IP address: ")
sys.stdout.flush()
ip = sys.stdin.readline()
a = ip + 1
print("you entered: " + ip + "and your IP will be : " + a)

显示错误:TypeError: must be str, not int

最后我想让那个数字适用于编辑它,请解释你的代码以帮助我正确理解它。 提前致谢

使用 Python 3.3,您可以使用 ipaddress — IPv4/IPv6 manipulation library

import sys
import ipaddress

sys.stdout.write("Enter IP address: ")
sys.stdout.flush()
ip = sys.stdin.readline().strip()   # remove the trailing '\n'
assigned_ip = ipaddress.IPv4Address(ip) + 1
print("You entered: " + ip + " and your IP will be: " + str(assigned_ip))

输出:

Enter IP address: 192.168.1.0
You entered: 192.168.1.0 and your IP will be: 192.168.1.1