Delete this before posting. Please make sure to include the language that you're using in the title, and use the <> button for any code.
Hi, I am a beginner to python3. I wrote an if in statement to tell if pinging an IP address worked - If the output of pythonping included "Request timed out" it would do something and if it did not it would do something else.
My code:
from pythonping import ping #Module to ping websites
from faker import Faker #Module to generate IPv4 and IPv6 addresses
#invalid = 0
#valid = 0
ex = Faker()
ip = ex.ipv4()
ip2 = ex.ipv6()
#print('ipv4 address:- ',ip)
#print('ipv6 address:- ',ip2)
#This part pings the IP
testping = ping (ip)
print(testping) #for testing
test = "Testtest test test test"
#After pinging, check if there is a reply from the website. If so, it will be logged to console. Otherwise, it will say that an invalid one was found.
if "Reply" in testping:
print('Good IP found, ipv4 is ' + ip + ' and ipv6 is ' + ip2)
print(testping)
#valid = valid+1
#print(valid)
elif "Request timed out" in testping:
print('Bad IP found, ipv4 is ' + ip + ' and ipv6 is ' + ip2)
#invalid = invalid+1
#print(invalid)
else:
print('Hmm, something went wrong.') #This else statement always triggers and the other two parts never do even under the right condition
When run, all that happens is the "else" statement, uncommenting the print (testping) #for testing
either prints Request timed out or Reply from (...) and then the else statement gets called even if the conditions are met for the if or elif part. What have I done wrong here? Thanks!!