Python syntax error while taking an IP address as an input

后端 未结 2 1844
萌比男神i
萌比男神i 2021-01-29 16:31

I have a text file called sample_ips.txt containing some random IP addresses as follows:-

182.0.0.15
182.0.0.16
182.0.0.17

I am giving an IP ad

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-29 16:47

    input_ip = raw_input("Enter IP:") # In python 2.x. If you use input() then type your inputs as string (>>Enter IP:"182.0.0.15")
    #input_ip = input("Enter IP:")  for python 3.x
    
    with open("ip.txt", "r") as ip:
        data = ip.readlines()
        for ips in data:
            ips = ips.strip("\n")
            if input_ip in ips:
                print ("true")
            else:
                print ("false")
    

提交回复
热议问题