Jump to content
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")

 

Link to comment
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
Link to comment
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

 

Link to comment
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.

Link to comment
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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...