venkatklr Posted November 3, 2017 Report Posted November 3, 2017 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") Quote
gear Posted November 3, 2017 Report Posted November 3, 2017 (edited) 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 November 3, 2017 by gear correction Quote
venkatklr Posted November 3, 2017 Author Report Posted November 3, 2017 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 Quote
gear Posted November 3, 2017 Report Posted November 3, 2017 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. Quote
venkatklr Posted November 3, 2017 Author Report Posted November 3, 2017 7 minutes ago, gear said: Yes, I know, I was wrong, I edited the post. Gear, did you updated code in your post? Quote
gear Posted November 3, 2017 Report Posted November 3, 2017 (edited) 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 November 3, 2017 by gear Quote
venkatklr Posted November 3, 2017 Author Report Posted November 3, 2017 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. Quote