繁体   English   中英

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

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

我想编写一个可以帮助我确定IP类并对其进行编辑的应用程序,例如,如果我使用的IP类是子网掩码为255.0.0.0的“ 10.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:必须是str,而不是int

最后,我想使该数字适用于对其进行编辑,并请您说明您的代码以帮助我正确理解它。 提前致谢

使用Python 3.3,您可以使用ipaddress — IPv4 / IPv6操作库

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

暂无
暂无

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

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