Skip to content
Snippets Groups Projects
Commit 81f8c60f authored by vr-group's avatar vr-group
Browse files

Merge branch 'feature/LauncherScriptFileReceiving'

parents 69cf8338 b76d1102
Branches
No related tags found
No related merge requests found
...@@ -147,9 +147,63 @@ try: ...@@ -147,9 +147,63 @@ try:
while bContinue: while bContinue:
try: try:
sResult = oConnection.recv( 128 ) sResult = oConnection.recv( 128 )
if sResult != "":
print( "Received quit event" ) if sResult != '':
#check whether we are about to reveive a file
sMessage = sResult.decode("utf-8")
if sMessage.startswith("file"):
aMessageParts = sMessage.split(":")
iBytesToReceive = int(aMessageParts[2])
Path, Filename = os.path.split(aMessageParts[1])
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)
#send acceptance
oConnection.send( b'ack' )
#print("Send ack")
#receive file
iBytesReceived = 0
with open(Fullpath+Filename, "wb") as f:
bReceivingFile = True
while bReceivingFile:
# read 1024 bytes from the socket (receive)
bytes_read = oConnection.recv(1024)
#print("Read 1024 bytes")
if not bytes_read:
# nothing is received
# file transmitting is done
bReceivingFile = False
else:
# write to the file the bytes we just received
f.write(bytes_read)
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:
oConnection.send( b'fail' ) #send failure
print("File receive failed")
else: #NOT sMessage.startswith("file")
print( "Received quit event: "+sMessage )
bContinue = False bContinue = False
except socket.timeout: except socket.timeout:
bContinue = True # timeouts are okay bContinue = True # timeouts are okay
except socket.error: except socket.error:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment