I have a function which sends a request to a server. But sometimes the server doesn't reply and my code doesn't seem to continue. Now I wanted to implement, that the code continues to run, if no reply is received after a given amount of time. Below the code:
for attempt in range (3):
try:
print('Attempt: ' + str(attempt+1))
print('DUT: ' + str(x))
time.sleep(1)
response = request("http://" + ipEntry.get() + port, "temperature_test", x)
time.sleep(1)
print(response.text)
print(response.data.result)
answer = response.data.result
logFile.write(time.strftime("%Y_%m_%d-%H_%M_%S\t\t") + str(x) + "\t\t" + str(answer) + "\t\t" + str(readVoltage()) + "\t\t" + str(readCurrent()) + "\n")
except:
print('An error occured!')
else:
if (response.data.result==False):
print("Attempt " + str(attempt + 1) + " failed!")
else:
break
else:
print('All 3 attempts failed')
Is it possible to start a counter after the beginning of this function and check with an "if" statement if it has elapsed?
Thanks for any hints and best regards!