简体   繁体   中英

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

I want to code an application that help me to determine class of IP and edit on it like if I'm using IP class a "10.0.0.0" with subnet mask 255.0.0.0 I want to let user input his IP AND SUBNET MASK as above and I make an equation to inform him how many IPs he can use, during my search I got this code

import sys

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

but when I use it I couldn't make any edit on the ip by that code

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)

it shows that error : TypeError: must be str, not int

finally I want to make that number is applicable to edit on it, and kindly explain you're code to help me understand it correctly. thanks in advance

With Python 3.3 you can make use of the 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))

Output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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