简体   繁体   中英

How can I get away from a loop?

So I have been trying to play around abit with monitor - For those who doesn't know what monitoring is - basically what it means is that you are checking something etc element, url or whatever in a certain time and check again if it has been changed.

This is what I have done and...

url = 'mrcnoir'

while True:
        try:
            password_page = requests.get('https://{}.com'.format(url), timeout=5)
            password_page.raise_for_status()

        except requests.exceptions.RequestException as err:
            print('Error checking password page! - https://{}.com'.format(url) + ' - ' + str(err))
            continue



        else:
            # *************---If password up---**************

            if ('password' in password_page.url):
                        # Password page is up
                        print('Password page is up! - ' + 'https://{}.com'.format(url))
                        if not ('password' in password_page.url):

                            # No password page -> password page


                            # *************---Send---**************

                            print("SENDING...1")

                            time.sleep(random.randint(6, 12))


            # *************---If password down---**************

            else:
                # Password page is down
                print('Password page is down! - ' + 'https://{}.com'.format(url))
                if ('password' in password_page.url):

                    # Password page -> no password page


                    # *************---Send---**************

                    print("SENDING...2") #<---- If it comes in here - it will be stuck forever and just keep posting this print...
                    time.sleep(random.randint(6, 12))

        # *************---Retry between 6-12 random.---**************
        finally:
            time.sleep(random.randint(6, 12))

The issue im having is at the bottom where it prints "SENDING...2" - What happened is that it just continues to print out the SENDING...2 all the time which means it been stuck in the loop -

Basically what I want to do is that whenever it comes to the second Else part it should print it out once and then continue to "monitor" and check until there is new changes. Meaning it will be needing to wait until it appears/password in the url.

How could I in that case make it happen?

url = 'mrcnoir'
last_status = False
while True:
        try:
            password_page = requests.get('https://{}.com'.format(url), timeout=5)
            password_page.raise_for_status()

        except requests.exceptions.RequestException as err:
            print('Error checking password page! - https://{}.com'.format(url) + ' - ' + str(err))
            continue

        else:
            # *************---If password up---**************

            if 'password' in password_page.url and last_status == False:
                # Password page is up
                last_status = True
                print('Password page is up! - ' + 'https://{}.com'.format(url))

                time.sleep(random.randint(6, 12))


            # *************---If password down---**************

            elif not 'password' in password_page.url and last_status == True:
                # Password page is down
                last_status = False
                print('Password page is down! - ' + 'https://{}.com'.format(url))
                time.sleep(random.randint(6, 12))

        # *************---Retry between 6-12 random.---**************
        finally:
            time.sleep(random.randint(6, 12))
import requests
import random
import time

url = 'https://mrcnoir.com/account/login'
errState = False

while True:
    try:
        password_page = requests.get('{}'.format(url), timeout=5)
        password_page.raise_for_status()

    except requests.exceptions.RequestException as err:
        if not errState:            
            print('Error checking password page! - {}'.format(url) + ' - ' + str(err))
            print("SENDING...2")   # <-----------------
            errState = True
        continue

    else:
        # *************---If password up---**************

        if ('password' in password_page.text):
            # Password page is up
            print('Password page is up! - ' + '{}'.format(url))
            print("SENDING...1")
            errState = False


        #else:
        #    # Password page is down ???
        #    # this is not a useful test, if it is down you get an exception and
        #    # should handle it there
        #    print('Password page is down! - ' + '{}'.format(url))
        #    print("SENDING...2") #<---- If it comes in here - it will be stuck forever and just keep posting this print...

    # *************---Retry between 6-12 random.---**************
    finally:
        time.sleep(random.randint(6, 12))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM