timeout on .get() and .post() requests

This commit is contained in:
AndyGrant committed 2018-07-04 16:44:42 -04:00
1 parent 5f97c595c7
commit 2e82ea8e92
1 file changed
+9 -9
+9 -9
View File
@@ -26,6 +26,9 @@ IS_WINDOWS = platform.system() == 'Windows'
# Server wants to identify different machines # Server wants to identify different machines
OS_NAME = platform.system() + ' ' + platform.release() OS_NAME = platform.system() + ' ' + platform.release()
# Timeout for .get and .post requests (in seconds)
HTTP_TIMEOUT = 30
# Server tracks machines by IDs, which are saved # Server tracks machines by IDs, which are saved
# locally once assigned. Regesitering a machine is # locally once assigned. Regesitering a machine is
# not a problem, but creates junk in the database # not a problem, but creates junk in the database
@@ -55,7 +58,7 @@ def getFile(source, outname):
# Read a file from the given source and save it locally # Read a file from the given source and save it locally
print('Downloading : {0}'.format(source)) print('Downloading : {0}'.format(source))
request = requests.get(url=source, stream=True) request = requests.get(url=source, stream=True, timeout=HTTP_TIMEOUT)
with open(outname, 'wb') as fout: with open(outname, 'wb') as fout:
for chunk in request.iter_content(chunk_size=1024): for chunk in request.iter_content(chunk_size=1024):
if chunk: fout.write(chunk) if chunk: fout.write(chunk)
@@ -64,7 +67,7 @@ def getFile(source, outname):
def getCoreFiles(): def getCoreFiles():
# Ask the server where the core files are saved # Ask the server where the core files are saved
request = requests.get(SERVER + '/getFiles/') request = requests.get(SERVER + '/getFiles/', timeout=HTTP_TIMEOUT)
location = request.content.decode('utf-8') location = request.content.decode('utf-8')
# Download the proper cutechess program, and a dll if needed # Download the proper cutechess program, and a dll if needed
@@ -265,7 +268,7 @@ def reportWrongBench(data, engine):
'password' : PASSWORD, 'password' : PASSWORD,
'engineid' : engine['id'], 'engineid' : engine['id'],
'testid' : data['test']['id']} 'testid' : data['test']['id']}
return requests.post('{0}/wrongBench/'.format(SERVER), data=postdata).text return requests.post('{0}/wrongBench/'.format(SERVER), data=postdata, timeout=HTTP_TIMEOUT).text
def reportNPS(data, nps): def reportNPS(data, nps):
@@ -276,7 +279,7 @@ def reportNPS(data, nps):
'username' : USERNAME, 'username' : USERNAME,
'password' : PASSWORD, 'password' : PASSWORD,
'machineid' : data['machine']['id']} 'machineid' : data['machine']['id']}
return requests.post('{0}/submitNPS/'.format(SERVER), data=postdata).text return requests.post('{0}/submitNPS/'.format(SERVER), data=postdata, timeout=HTTP_TIMEOUT).text
except: print ('<Warning> Unable to reach server') except: print ('<Warning> Unable to reach server')
def reportResults(data, wins, losses, draws, crashes, timeloss): def reportResults(data, wins, losses, draws, crashes, timeloss):
@@ -294,7 +297,7 @@ def reportResults(data, wins, losses, draws, crashes, timeloss):
'machineid' : data['machine']['id'], 'machineid' : data['machine']['id'],
'resultid' : data['result']['id'], 'resultid' : data['result']['id'],
'testid' : data['test']['id']} 'testid' : data['test']['id']}
return requests.post('{0}/submitResults/'.format(SERVER), data=postdata).text return requests.post('{0}/submitResults/'.format(SERVER), data=postdata, timeout=HTTP_TIMEOUT).text
except: print ('<Warning> Unable to reach server') except: print ('<Warning> Unable to reach server')
def completeWorkload(data): def completeWorkload(data):
@@ -409,7 +412,7 @@ if __name__ == '__main__':
try: try:
# Request the information for the next workload # Request the information for the next workload
request = requests.post('{0}/getWorkload/'.format(SERVER), data=postdata) request = requests.post('{0}/getWorkload/'.format(SERVER), data=postdata, timeout=HTTP_TIMEOUT)
# Response is a dictionary of information, 'None', or an erro # Response is a dictionary of information, 'None', or an erro
data = request.content.decode('utf-8') data = request.content.decode('utf-8')
@@ -438,9 +441,6 @@ if __name__ == '__main__':
with open('machine.txt', 'w') as fout: with open('machine.txt', 'w') as fout:
fout.write(str(postdata['machineid'])) fout.write(str(postdata['machineid']))
# Update machine id in case ours was bad
postdata['machineid'] = data['machine']['id']
# Begin working on the games to be played # Begin working on the games to be played
completeWorkload(data) completeWorkload(data)