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

将IP和子网掩码号存储在变量中,并在Python中对其进行编辑,python,ip,subnet,Python,Ip,Subnet,我想编写一个应用程序,帮助我确定IP的类别,并对其进行编辑,就像我使用带有子网掩码255.0.0.0的IP类别a“10.0.0”一样。我想让用户如上所述输入他的IP和子网掩码,我建立一个等式,告知他可以使用多少IP,在我的搜索过程中,我得到了这个代码 import sys sys.stdout.write("Enter IP address: ") sys.stdout.flush() ip = sys.stdin.readline() print("you entered: " + ip)

我想编写一个应用程序,帮助我确定IP的类别,并对其进行编辑,就像我使用带有子网掩码255.0.0.0的IP类别a“10.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)
它显示error:TypeError:必须是str,而不是int

最后,我想使这个数字是适用于编辑它,并请解释您的代码,以帮助我正确理解它。
提前感谢

使用Python 3.3,您可以使用

输出:

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

sys.stdin.readline()
返回字符串。不能将整数(
1
)添加到字符串(
ip
)中。为什么不使用
input()
?看看我应该用什么来代替sys.stdin.readline()?如果我使用输入,我如何在其中存储xxx.xxx.xxx.xxx?我知道输入应该检索一种类型的变量,比如int、string或float…等等,你可以使用正则函数。你不必这么做,这不是问题,但我不明白你为什么要使用
sys.stdin.readline()
而不是
input()
。我用了它,我看到有人用它在变量中存储IP,因为我用了它,你能给我一个如何使用常规输入的例子吗()我如何将IP存储在一个变量中进行编辑你真是个天才,我想给你一百万美元,比如:)我使用了你的代码,它工作正常,非常感谢。
Enter IP address: 192.168.1.0
You entered: 192.168.1.0 and your IP will be: 192.168.1.1