How do I break out of a While True Loop and Go back to the Loop

Submitted 4 years, 7 months ago
Ticket #130
Views 346
Language/Framework Python
Priority Medium
Status Closed

So each request it sends keeps printing

{"success":false,"code":7,"message":"Ad creation cooldown has not elapsed"}

and everytime it prints this I want it to break the loop and wait for 15 seconds then do the loop again. My Code:

headers = {'content-type': 'application/json'}

cookies = dict(_RoliData='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ2ZXJzaW9uIjoxLCJwbGF5ZXJfZGF0YSI6eyJuYW1lIjoiNzZfNzciLCJpZCI6MTUxMjExNDM0fSwiaWF0IjoxNTk5NzIyMjM4LCJleHAiOjE2MDc0OTgyOTh9.qVFHIhsXKgQIZvLvJzH0WFZbcsgZE5Ou_H_0laIbFk4', _RoliVerification= 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ2ZXJzaW9uIjoxLCJwbGF5ZXJfZGF0YSI6eyJuYW1lIjoiNzZfNzciLCJpZCI6MTUxMjExNDM0fSwiaWF0IjoxNTk5NzIyMjM4LCJleHAiOjE2MDc0OTgyOTh9.WtCyuWnKnIzA7PMUyCBqihXwaqfunZQkiMQd1n6s6-Q')

payload = json.dumps({"player_id":151211434,"offer_item_ids":[1241224444,20573078,187483689,24826811],"request_item_ids":[144506778,96095042,1533893,1051578],"request_tags":[]})

while True:
    g = requests.post('https://www.rolimons.com/tradeapi/create', headers=headers, data=payload, cookies=cookies)
    print(g.text)

Submitted on Sep 10, 20
add a comment

1 Answer

Verified

As mentioned in comments. Use time.sleep(t) to stop for t seconds.
You could use a flag to exit the loop when it's ready and why not, to check if the status_code was the expected too.

response_not = {"success":False,"code":7,"message":"Ad creation cooldown has not elapsed"}
exit = False
while not exit:
    g = requests.post(
          'https://www.rolimons.com/tradeapi/create',
          headers=headers,
          data=payload,
          cookies=cookies
        )
    if g.status_code == 200:
        if g.json() == response_not:
            time.sleep(15)
        else:
            exit = True
    else:
        print(f"Warn: Status code was {g.status_code}")
        exit = True

Submitted 4 years, 6 months ago


Latest Blogs