0

When exception occurs it skips the current "y". I can't manage to prevent this.

  for x in range(1, 100):
            for y in range(1, 100):
                try:
                    text = requests.post(url, {"x":a, "y":b})
                except:
                    time.sleep(10)

solved with while True but now it doesnt break the first loop

kusky
  • 15
  • 5

2 Answers2

1

You can wrap the try/except block in a loop and include a break statement at the end of the try block. Your program will continue to try to make the post request until it succeeds and reaches the break. If requests.post encounters an exception, the program will skip the break and move to the except block, wait, then try again.

For example:

for x in range(1, 100):
    for y in range(1, 100):
        while True:
            try:
                text = requests.post(url, {"x":a, "y":b})
                break
            except:
                time.sleep(10)

Edit

Since you mentioned that for each x, the program should try each y until finding the correct y, at which point the program should skip to the next x, I've added this update. To do this, you can keep a variable to track if the correct y has been found yet. Then after each y is tried, you can check the value of this variable (found_correct_y) and if it is True, break out of the for y in ... loop and on to the next value of x.

Here's an example:

for x in range(1, 100):
    found_correct_y = False
    for y in range(1, 100):
        while True:
            try:
                response = requests.post(url, {"x":a, "y":b})
                found_correct_y = was_correct_y(response)
                break # out of the 'while True' loop
            except: 
                time.sleep(10)
        if found_correct_y:
            break # out of the 'for y ...' loop, to the next x


def was_correct_y(response):
    """returns a boolean based on some properties of the response"""
    pass
Henry Woody
  • 14,024
  • 7
  • 39
  • 56
  • it solved the problem but now it doesnt break the first loop and keeps trying with the same x – kusky Mar 02 '19 at 13:39
  • @kusky can you explain a bit more? Do you want it to try again with a different x if an exception is thrown? – Henry Woody Mar 02 '19 at 22:16
  • for every x it tries all the 100 y but if it finds the correct y it should go to the next x – kusky Mar 03 '19 at 18:50
  • @kusky made an edit, let me know if that helps – Henry Woody Mar 03 '19 at 21:17
  • name 'response' is not defined, theres some much code in the for loop i lost my mind. i accepted the answer tho because it seems like it would work – kusky Mar 03 '19 at 22:12
  • @kusky I renamed `text` to `response` because it's a more accurate name, you can get the text by doing `response.text`. Also because figuring out the correct `y` might depend on something other than just the response text, like status codes. Hope you can get it working though. Let me know if you have questions – Henry Woody Mar 03 '19 at 22:25
1

I raised an exception in the inner 'y' block and still had success, ie looping back to the 'x' block.

for x in range(1, 10):
            for y in range(1, 10):
                try:
                    #text = requests.post(url, {"x":a, "y":b})
                    print('y',y,'x',x)
                    if (x==2):
                        raise
                except:
                    time.sleep(10)
Nico
  • 11
  • 1