(Python file manipulation) I’m having trouble checking ip validation, how can I continue the code?

Asked

Viewed 47 times

0

A program that reads a text file containing a list of IP addresses and store invalid IP addresses in an array.

Input file:

200.135.80.9
192.168.1.1
8.35.67.74
257.32.4.5
85.345.1.2
1.2.3.4
9.8.234.5
192.168.0.256

Exit:

["257.32.4.5", "85.345.1.2", "9.8.234.5", "192.168.0.256"]

My code:

ip = open('endereços.txt','r')
line = []
line = ip.read().split('\n')

#Estrutura incompleta
if 0 > ip[0] > 255:
    print(ip[0])

1 answer

0


By simplifying this code: ip_validos=[]

for ip in open("endereços.txt"):
      #condição...validar ips
      ip_validos = ip_validos + [ip.replace("\n",'')]
      print(ip.replace("\n",'')) #replace remove os espaço em branco

#ip list

print(ip_validos)

Did not specify which class of ip will be checked

Classe A    
Classe B    
Classe C
  • in the case , were those as output

  • Do you have any idea what that exit would be like?

  • cool, you can use the import socket and call the function to check ... socket.inet_aton(ip.replace("n",')) and treat using Exception , inside the Try the list of valid ip and except those that are not variable.

  • All right, I’ll try !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.