Skip to content
Snippets Groups Projects
Commit b76d1102 authored by Ehret's avatar Ehret
Browse files

check whether file already exists and correctly receive it

parent 8735d3c2
Branches
No related tags found
No related merge requests found
......@@ -158,9 +158,23 @@ try:
ProjectName = aMessageParts[3]
Fullpath = os.path.join(sCurrentScriptsDirectory, "..", "tmp", ProjectName, Path, "")
print("Should receive file: "+Filename+" in path "+Fullpath+ " with "+str(iBytesToReceive)+" bytes")
#check whether the file with this exact size already exists
if os.path.isfile(Fullpath+Filename) and os.stat(Fullpath+Filename).st_size==iBytesToReceive:
oConnection.send( b'exists' )
print("File already exists with this size, so no need for resending")
else: #file need to be received
#create dir if it does not exist
if not os.path.exists(Fullpath):
os.makedirs(Fullpath)
oConnection.send( b'a' ) #send acceptance
#send acceptance
oConnection.send( b'ack' )
#print("Send ack")
#receive file
iBytesReceived = 0
with open(Fullpath+Filename, "wb") as f:
bReceivingFile = True
while bReceivingFile:
......@@ -174,12 +188,21 @@ try:
else:
# write to the file the bytes we just received
f.write(bytes_read)
print("File received")
iBytesReceived += len(bytes_read)
if iBytesReceived == iBytesToReceive:
bReceivingFile = False
f.close()
#check whether received file seems ok
if iBytesReceived == iBytesToReceive:
oConnection.send( b'ack' ) #send acceptance
print("File received successfully")
else:
print( "Received message: "+sResult.decode("utf-8"))
print( "First byte: "+str(sResult[0]))
#print( "Received quit event" )
#bContinue = False
oConnection.send( b'fail' ) #send failure
print("File receive failed")
else: #NOT sMessage.startswith("file")
print( "Received quit event: "+sMessage )
bContinue = False
except socket.timeout:
bContinue = True # timeouts are okay
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment