client.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # client program to send requests to server
  2. import socket
  3. import pickle
  4. from file import files
  5. from tree import directories
  6. mydir = directories("main")
  7. myfile = files("mainer")
  8. MAX_SIZE_BYTES = 65535 # Mazimum size of a UDP datagram - 64KBytes
  9. def client():
  10. c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #UDP
  11. host = socket.gethostname()
  12. port = 95
  13. x = input("Enter the IP Address of the machine: ") #asks IP of machine but not yet implemented
  14. y = input("Enter the username: ") #asks username of the person but not yet implemented
  15. while True:
  16. c.connect((host, port))
  17. print("-----------------------------------------------")
  18. print("Welcome to Client Server File System")
  19. print("-----------------------------------------------")
  20. print("What would operation would you like to perform?")
  21. print("1. Create File")
  22. print("2. Write to File")
  23. print("3. Delete File")
  24. print("4. Create Directory")
  25. print("5. Check Directory")
  26. print("6. Read File")
  27. print("7. Truncate File")
  28. print("8. Move")
  29. print("9. Move Within File")
  30. print("10. Memory Map")
  31. print("11. Exit")
  32. print("-----------------------------------------------")
  33. y = input("Enter your choice: ")
  34. #if user input 1 then file name and text to be written is asked and the data is send to server via pickle.dumps
  35. #pickle.dumps takes in a list and encodes it in a tuple and sends the data
  36. #Creates a File
  37. if int(y) == 1:
  38. a = input("Enter File name: ")
  39. d = input("Enter text to be written to file: ")
  40. b = [y, a, d]
  41. data = pickle.dumps(b)
  42. c.send(data) # data is sent
  43. addr = c.recv(MAX_SIZE_BYTES) # data is received from server.py
  44. datar = pickle.loads(addr) #data is decoded from server.py
  45. print(datar[0]) # the first item of the tuple is printed out
  46. #Write to a File
  47. if int(y) == 2:
  48. a = input("Enter File Name: ")
  49. b = int(input("Enter offset: "))
  50. d = input("Enter text to be written: ")
  51. e = [y, a, b, d]
  52. data = pickle.dumps(e)
  53. c.send(data)
  54. addr = c.recv(MAX_SIZE_BYTES)
  55. datar = pickle.loads(addr)
  56. print(datar[0])
  57. mydir.read(a) #the contents of the file entered by the user is printed out
  58. #Deletes a File
  59. if int(y) == 3:
  60. a = input("Enter file name: ")
  61. b = [y, a]
  62. data = pickle.dumps(b)
  63. c.send(data)
  64. addr = c.recv(MAX_SIZE_BYTES)
  65. datar = pickle.loads(addr)
  66. print(datar[0])
  67. #Creates a Directory
  68. if int(y) == 4:
  69. a = input("Enter directory name: ")
  70. b = [y, a]
  71. data = pickle.dumps(b)
  72. c.send(data)
  73. addr = c.recv(MAX_SIZE_BYTES)
  74. datar = pickle.loads(addr)
  75. print(datar[0])
  76. #Checks a Directory whether that directory is present or not
  77. if int(y) == 5:
  78. a = input("Enter directory name: ")
  79. b = [y, a]
  80. data = pickle.dumps(b)
  81. c.send(data)
  82. addr = c.recv(MAX_SIZE_BYTES)
  83. datar = pickle.loads(addr)
  84. print(datar[0])
  85. print(mydir.chDir(a)) #prints out the result whether the directory is present or not
  86. # Prints out the contents of the file that is requested
  87. if int(y) == 6:
  88. a = input("Enter file name: ")
  89. e = [y, a]
  90. data = pickle.dumps(e)
  91. c.send(data)
  92. addr = c.recv(MAX_SIZE_BYTES)
  93. datar = pickle.loads(addr)
  94. mydir.read(a)
  95. # Truncates or removes the contents of the file by a specific range and prints out the contents of the file
  96. if int(y) == 7:
  97. a = input("Enter file name: ")
  98. f = int(input("Enter maximum size till you want to truncate: "))
  99. d = [y, a, f]
  100. data = pickle.dumps(d)
  101. c.send(data)
  102. addr = c.recv(MAX_SIZE_BYTES)
  103. datar = pickle.loads(addr)
  104. print(datar[0])
  105. mydir.read(a)
  106. # Move the file to a specific directory
  107. if int(y) == 8:
  108. a = input("Enter Source File Name: ")
  109. d = input("Enter Destination Directory: ")
  110. b = [y, a, d]
  111. data = pickle.dumps(b)
  112. c.send(data)
  113. addr = c.recv(MAX_SIZE_BYTES)
  114. datar = pickle.loads(addr)
  115. print(datar[0])
  116. # Move the contents within the file
  117. # Not sure whether it works perfect or not
  118. if int(y) == 9:
  119. a = "Directory Names: "
  120. b = [y, a]
  121. data = pickle.dumps(b)
  122. c.send(data)
  123. addr = c.recv(MAX_SIZE_BYTES)
  124. datar = pickle.loads(addr)
  125. print(datar[0])
  126. print(datar[1])
  127. # Shows all directories created
  128. if int(y) == 10:
  129. a = "Memory Map"
  130. b = [y, a]
  131. data = pickle.dumps(b)
  132. c. send(data)
  133. addr = c.recv(MAX_SIZE_BYTES)
  134. datar = pickle.loads(addr)
  135. print(datar[0])
  136. mydir.memoryMap()
  137. # Exists the program
  138. if int(y) == 11:
  139. a = "Exiting program...."
  140. b = [y, a]
  141. data = pickle.dumps(b)
  142. c.send(data)
  143. addr = c.recv(MAX_SIZE_BYTES)
  144. datar = pickle.loads(addr)
  145. print("Exiting program by {}".format(host)) # prints out from which hostname was the client was closed
  146. print(datar[0])
  147. break
  148. c.close() #closes the connection
  149. if __name__ == '__main__':
  150. client()