Jump to content

Python 3 if _ in _ statement not working?

Hampoon

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!!

space for rent

Link to comment
Share on other sites

Link to post
Share on other sites

19 minutes ago, Hampoon said:

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.

Interesting 🤔

20 minutes ago, Hampoon said:

My code:

The way you used it means

if there is a string equal to "Reply" in the list testping:

or

if part of the string testping is equal to "Reply":

which I don't think is what you're looking for.

 

The documentation explains that ping() does not return a string but rather a special ResponseList object.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Sauron said:

Interesting 🤔

The way you used it means


if there is a string equal to "Reply" in the list testping:

or


if part of the string testping is equal to "Reply":

which I don't think is what you're looking for.

 

The documentation explains that ping() does not return a string but rather a special ResponseList object.

Ohhh. How would I make it check the OUTPUT of the command for Reply?

space for rent

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Hampoon said:

Ohhh. How would I make it check the OUTPUT of the command for Reply?

You should use it as an iterable

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

As Sauron said, you should iterate over the actual returned ResponseList.

If, for whatever reason, you want to brute force what you have to make it work like you were expecting then convert the returned value to a string.

if "Reply" in str(testping):

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×