def sendrequest(data): try: Encode the data encodeddata = urllib.parse.urlencode(data)…
- User
def send_request(data): try: # Encode the data encoded_data = urllib.parse.urlencode(data) # Send the POST request response = requests.post(url, headers=headers, data=encoded_data, timeout=10) # Check the response status code if response.status_code == 200: # Process the response content response_content = f"Headers:\n{response.headers}\n\nBody:\n{response.text}" filename = f"response_{data['P4']}.txt" with open(filename, "w") as file: file.write(response_content) logging.info(f"Response received and recorded in {filename}.") print(f"Response received and recorded in {filename}.") send_telegram_message(f"Response received and recorded successfully for {data['P4']}.") return True else: logging.warning(f"Received status code {response.status_code}. Retrying...") print(f"Received status code {response.status_code}. Retrying...") except requests.exceptions.RequestException as e: logging.error(f"Request failed: {e}. Retrying...") return False
Keep rotating through the list until all entries are processed
while data_list: # Use a flag to track if any entry is successfully processed in this cycle processed = False for data in data_list[:]: logging.info(f"Processing entry with P4: {data['P4']}") print(f"Processing entry with P4: {data['P4']}") success = send_request(data) if success: logging.info(f"Successfully processed data: {data}") print(f"Successfully processed data: {data}") data_list.remove(data) processed = True # Set flag to True if an entry was processed break # Exit the for loop to start next cycle immediately else: logging.warning(f"Failed to process data: {data['P4']}, will retry.") print(f"Failed to process data: {data['P4']}, will retry.") time.sleep(3) # wait for 3 seconds before trying the next entry
# If no entry was processed in this cycle, wait before retrying if not processed: time.sleep(3)Please fix this code it should keep going when an entry succeds but its exiting
- Assistant