server.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import socket
  2. from file import files
  3. from tree import directories
  4. import pickle
  5. import threading
  6. import time
  7. mydir = directories("main")
  8. myfile = files("mainer")
  9. # Mazimum size of a UDP datagram - 64Kbytes
  10. MAX_SIZE_BYTES = 65535
  11. def serverprog():
  12. # gets the local host name from which server program is initiaited
  13. host = socket.gethostname()
  14. # binding the port to port 95
  15. port = 95
  16. # UDP
  17. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  18. # binding hostname and port number
  19. s.bind((host, port))
  20. # count initialized to 0 will be incremented when a new user is registered but not yet implemented
  21. count = 0
  22. while True:
  23. #receives data from client.py
  24. clientsocket, address = s.recvfrom(MAX_SIZE_BYTES)
  25. print("Connection from " + str(address) + " has been established.")
  26. # decodes data from client
  27. data = pickle.loads(clientsocket)
  28. #increments counter by 1; whenever a new connection is established
  29. count += 1
  30. print('The client at {} says {!r}'.format(address, data[0]))
  31. print(data)
  32. # checks for the data entered by the user and performs function on it
  33. # creates file
  34. if data[0] == '1':
  35. # assigns a to 2nd item in the list and and b to 3rd item in the list which is loaded by pickle
  36. a = data[1]
  37. b = data[2]
  38. # calls addFile func from file.py and pass the data sent from client
  39. mydir.addFile(a, b)
  40. b = "File Created!!"
  41. # shows the file names
  42. a = mydir.getFiles()
  43. k = [a]
  44. # data encoding
  45. da = pickle.dumps(k)
  46. # sends enocoded data to client
  47. s.sendto(da, address)
  48. # Writes to the file
  49. if data[0] == '2':
  50. a = data[1]
  51. b = data[2]
  52. d = data[3]
  53. # writes to file by calling writeFile funtion in file.py
  54. mydir.writeFile(a, b, d)
  55. print("text written")
  56. z = "Text written to File"
  57. k = [z]
  58. m = pickle.dumps(k)
  59. #sends data to client
  60. s.sendto(m, address)
  61. # Deletes the file
  62. if data[0] == '3':
  63. a = data[1]
  64. mydir.delFile(a)
  65. c = "File Deleted"
  66. k = [c]
  67. m = pickle.dumps(k)
  68. #sends data to client
  69. s.sendto(m, address)
  70. # Creates the Directory
  71. if data[0] == '4':
  72. a = data[1]
  73. mydir.addChildDir(a)
  74. a = "Directory Added"
  75. k = [a]
  76. m = pickle.dumps(k)
  77. #sends data to client
  78. s.sendto(m, address)
  79. if data[0] == '5':
  80. a = data[1]
  81. b = mydir.chDir(a)
  82. c = "Directory Status: "
  83. print(b)
  84. k = [c, b]
  85. m = pickle.dumps(k)
  86. #sends data to client
  87. s.sendto(m, address)
  88. if data[0] == '6':
  89. a = data[1]
  90. x = "File Contents: "
  91. time.sleep(3)
  92. mydir.read(a)
  93. k = [x]
  94. m = pickle.dumps(k)
  95. #sends data to client
  96. s.sendto(m, address)
  97. if data[0] == '7':
  98. a = data[1]
  99. b = data[2]
  100. x = "File Truncated: "
  101. mydir.truncate(a, b)
  102. k = [x]
  103. m = pickle.dumps(k)
  104. #sends data to client
  105. s.sendto(m, address)
  106. if data[0] == '8':
  107. a = data[1]
  108. b = data[2]
  109. c = "File Moved Successfully. "
  110. mydir.move(a, b)
  111. k = [c]
  112. m = pickle.dumps(k)
  113. #sends data to client
  114. s.sendto(m, address)
  115. if data[0] == '9':
  116. print(data[1])
  117. a = "Directory List"
  118. k = [a]
  119. m = pickle.dumps(k)
  120. #sends data to client
  121. s.sendto(m, address)
  122. if data[0] == '10':
  123. print(data[1])
  124. a = "Memory Map: "
  125. k = [a]
  126. m = pickle.dumps(k)
  127. #sends data to client
  128. s.sendto(m, address)
  129. if data[0] == '11':
  130. print(data[1])
  131. a = "Thank you for visiting us. Hope to see you soon!"
  132. k = [a]
  133. m = pickle.dumps(k)
  134. #sends data to client
  135. s.sendto(m, address)
  136. #sends data to client
  137. s.sendto(clientsocket, address)
  138. if __name__ == '__main__':
  139. # Creates a new thread each time server is called
  140. # prevents race condition and read - write problem
  141. t1 = threading.Thread(target=serverprog, args=())
  142. # starts the thread
  143. t1.start()
  144. # joins the thread to previous thread
  145. t1.join()