|
297 | 297 | "outputs": [], |
298 | 298 | "source": [ |
299 | 299 | "import requests\n", |
300 | | - "\n", |
| 300 | + "import time\n", |
301 | 301 | "\n", |
302 | 302 | "def submit_flag(flag):\n", |
303 | 303 | " url = f\"{CRUCIBLE_URL}/api/challenges/{CHALLENGE}/submit-flag\"\n", |
304 | 304 | " headers = {\"X-API-Key\": CRUCIBLE_API_KEY}\n", |
305 | 305 | " payload = {\"challenge\": CHALLENGE, \"flag\": flag}\n", |
306 | | - " response = requests.post(url, headers=headers, json=payload)\n", |
307 | | - " if response.status_code == 200:\n", |
308 | | - " if response.json().get(\"correct\") is True:\n", |
309 | | - " print(\"The flag was correct. Congrats!\")\n", |
310 | | - " else:\n", |
311 | | - " print(\"The flag was incorrect. Keep trying!\")\n", |
312 | | - " else:\n", |
313 | | - " print(\"There was an error submitting your flag\")\n", |
314 | | - " print(response.text)\n", |
| 306 | + "\n", |
| 307 | + " MAX_RETRIES = 3\n", |
| 308 | + "\n", |
| 309 | + " for attempt in range(1, MAX_RETRIES + 1):\n", |
| 310 | + " try:\n", |
| 311 | + " response = requests.post(url, headers=headers, json=payload, timeout=5)\n", |
| 312 | + "\n", |
| 313 | + " if response.status_code == 429:\n", |
| 314 | + " if attempt == MAX_RETRIES:\n", |
| 315 | + " print(\"Rate limited—max retries reached.\")\n", |
| 316 | + " return\n", |
| 317 | + " print(f\"Rate limited (429). Retrying {attempt}/{MAX_RETRIES}...\")\n", |
| 318 | + " time.sleep(2 ** attempt) # Exponential backoff: 2, 4, 8 seconds\n", |
| 319 | + " continue\n", |
| 320 | + "\n", |
| 321 | + " if response.status_code == 200:\n", |
| 322 | + " if response.json().get(\"correct\") is True:\n", |
| 323 | + " print(\"The flag was correct. Congrats!\")\n", |
| 324 | + " else:\n", |
| 325 | + " print(\"The flag was incorrect. Keep trying!\")\n", |
| 326 | + " else:\n", |
| 327 | + " print(f\"Error {response.status_code}: {response.text}\")\n", |
| 328 | + " return\n", |
| 329 | + "\n", |
| 330 | + " except requests.exceptions.RequestException as e:\n", |
| 331 | + " print(f\"Request error on attempt {attempt}/{MAX_RETRIES}: {e}\")\n", |
| 332 | + " if attempt == MAX_RETRIES:\n", |
| 333 | + " print(\"Max retries reached. Could not submit flag.\")\n", |
| 334 | + " return\n", |
| 335 | + " time.sleep(2 ** attempt) # Exponential backoff\n", |
315 | 336 | "\n", |
316 | 337 | "flag = \"gAAAAA...\" # Replace with the flag once you find it\n", |
317 | 338 | "submit_flag(flag)" |
|
0 commit comments