Jump to content
Sign in to follow this  
venkatklr

Python while loop not working in the code.

Recommended Posts

Hi All,

 

I have created small script to check 2 URL and if these two is not matching with success code 200, while loop should execute but it is not working. Please can some one help me where I am doing wrong.

import requests
url1 = ('http://myhost.com/cgi-bin/')
url2 = ('http://google.com')
responseurl1 = requests.get(url1)
responseurl2 = requests.get(url2)
r1 = responseurl1.status_code
r2 = responseurl2.status_code
print(r1)
print(r2)
statuscode = 200
try:
   if r1 is r2 is statuscode:
       print("Link is up")
       counter = 0
       while counter < 3 and r1 is r2 != statuscode:
           counter = counter +1
           print(counter)
   else:
       print("Link is down")
except Exception as e:
    print("down")

 

Share this post


Link to post
Share on other sites

I just realised, how is that suppose to work if you check first IF r1, r2 and status code are equal and then use a conditional statement inside that IF block to check if r1, r2 and statuscode are not equal, that does not make any sense because you haven't changed anything about those variables, they are the same, why would you check that twice with the opposite comparison.

If you want that counter if they are not equal, just put the while loop inside ELSE block

Edited by gear
correction

Share this post


Link to post
Share on other sites
28 minutes ago, gear said:

The order of the condition is wrong.

Here, I fixed it:


import requests


url1 = ('http://myhost.com/cgi-bin/')
url2 = ('http://google.com')

responseurl1 = requests.get(url1)
responseurl2 = requests.get(url2)

r1 = responseurl1.status_code
r2 = responseurl2.status_code

print(r1)
print(r2)

statuscode = 200

try:
   if r1 is r2 is statuscode:
       print("Link is up")
       counter = 0
       while counter < 3 and (r1 is r2) != statuscode:
           counter = counter +1
           print(counter)
   else:
       print("Link is down")
except Exception as e:
    print("down")

 

Hi ,

 

here is the output of this code. no counter is getting for 403 and 200  but if both r1 and r2 gets 200 then the counter starts.

 

403
200
Link is down

 

 

200
200
Link is up
counting start
counter 1
counter 2
counter 3

 

Share this post


Link to post
Share on other sites
1 minute ago, venkatklr said:

Hi ,

 

here is the output of this code. no counter is getting for 403 and 200  but if both r1 and r2 gets 200 then the counter starts.

 

403
200
Link is down

 

 

200
200
Link is up
counting start
counter 1
counter 2
counter 3

 

Yes, I know, I was wrong, I edited the post.

Share this post


Link to post
Share on other sites

Don't you see it?
Just put the while loop inside else: remove the second comparison and r1 is r2 != statuscode.

Here:

import requests


url1 = ('http://myhost.com/cgi-bin/')
url2 = ('http://google.com')

responseurl1 = requests.get(url1)
responseurl2 = requests.get(url2)

r1 = responseurl1.status_code
r2 = responseurl2.status_code

print(r1)
print(r2)

statuscode = 200
counter = 0

try:
   if r1 is r2 is statuscode:
       print("Link is up")

   else:
       print("Link is down")
       while counter < 3:
           counter += 1
           print(counter)
except Exception as e:
    print("down")

 

Edited by gear

Share this post


Link to post
Share on other sites
1 minute ago, gear said:

Don't you see it?
Just put the while loop inside else: remove the second comparison and r1 is r2 != statuscode.

Gear, I got the output what I wanted. thank you for the help.

Share this post


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
Sign in to follow this  

×