Skip to content

Commit 5e919bd

Browse files
chore: add max retries to flag submission
1 parent e245e3a commit 5e919bd

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

airtbench/challenges/bear4.ipynb

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -297,21 +297,42 @@
297297
"outputs": [],
298298
"source": [
299299
"import requests\n",
300-
"\n",
300+
"import time\n",
301301
"\n",
302302
"def submit_flag(flag):\n",
303303
" url = f\"{CRUCIBLE_URL}/api/challenges/{CHALLENGE}/submit-flag\"\n",
304304
" headers = {\"X-API-Key\": CRUCIBLE_API_KEY}\n",
305305
" 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",
315336
"\n",
316337
"flag = \"gAAAAA...\" # Replace with the flag once you find it\n",
317338
"submit_flag(flag)"

0 commit comments

Comments
 (0)