123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- # client program to send requests to server
- import socket
- import pickle
- from file import files
- from tree import directories
- mydir = directories("main")
- myfile = files("mainer")
- MAX_SIZE_BYTES = 65535 # Mazimum size of a UDP datagram - 64KBytes
- def client():
- c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #UDP
- host = socket.gethostname()
- port = 95
- x = input("Enter the IP Address of the machine: ") #asks IP of machine but not yet implemented
- y = input("Enter the username: ") #asks username of the person but not yet implemented
- while True:
- c.connect((host, port))
- print("-----------------------------------------------")
- print("Welcome to Client Server File System")
- print("-----------------------------------------------")
- print("What would operation would you like to perform?")
- print("1. Create File")
- print("2. Write to File")
- print("3. Delete File")
- print("4. Create Directory")
- print("5. Check Directory")
- print("6. Read File")
- print("7. Truncate File")
- print("8. Move")
- print("9. Move Within File")
- print("10. Memory Map")
- print("11. Exit")
- print("-----------------------------------------------")
- y = input("Enter your choice: ")
- #if user input 1 then file name and text to be written is asked and the data is send to server via pickle.dumps
- #pickle.dumps takes in a list and encodes it in a tuple and sends the data
- #Creates a File
- if int(y) == 1:
- a = input("Enter File name: ")
- d = input("Enter text to be written to file: ")
- b = [y, a, d]
- data = pickle.dumps(b)
- c.send(data) # data is sent
- addr = c.recv(MAX_SIZE_BYTES) # data is received from server.py
- datar = pickle.loads(addr) #data is decoded from server.py
- print(datar[0]) # the first item of the tuple is printed out
- #Write to a File
- if int(y) == 2:
- a = input("Enter File Name: ")
- b = int(input("Enter offset: "))
- d = input("Enter text to be written: ")
- e = [y, a, b, d]
- data = pickle.dumps(e)
- c.send(data)
- addr = c.recv(MAX_SIZE_BYTES)
- datar = pickle.loads(addr)
- print(datar[0])
- mydir.read(a) #the contents of the file entered by the user is printed out
- #Deletes a File
- if int(y) == 3:
- a = input("Enter file name: ")
- b = [y, a]
- data = pickle.dumps(b)
- c.send(data)
- addr = c.recv(MAX_SIZE_BYTES)
- datar = pickle.loads(addr)
- print(datar[0])
- #Creates a Directory
- if int(y) == 4:
- a = input("Enter directory name: ")
- b = [y, a]
- data = pickle.dumps(b)
- c.send(data)
- addr = c.recv(MAX_SIZE_BYTES)
- datar = pickle.loads(addr)
- print(datar[0])
- #Checks a Directory whether that directory is present or not
- if int(y) == 5:
- a = input("Enter directory name: ")
- b = [y, a]
- data = pickle.dumps(b)
- c.send(data)
- addr = c.recv(MAX_SIZE_BYTES)
- datar = pickle.loads(addr)
- print(datar[0])
- print(mydir.chDir(a)) #prints out the result whether the directory is present or not
- # Prints out the contents of the file that is requested
- if int(y) == 6:
- a = input("Enter file name: ")
- e = [y, a]
- data = pickle.dumps(e)
- c.send(data)
- addr = c.recv(MAX_SIZE_BYTES)
- datar = pickle.loads(addr)
- mydir.read(a)
- # Truncates or removes the contents of the file by a specific range and prints out the contents of the file
- if int(y) == 7:
- a = input("Enter file name: ")
- f = int(input("Enter maximum size till you want to truncate: "))
- d = [y, a, f]
- data = pickle.dumps(d)
- c.send(data)
- addr = c.recv(MAX_SIZE_BYTES)
- datar = pickle.loads(addr)
- print(datar[0])
- mydir.read(a)
- # Move the file to a specific directory
- if int(y) == 8:
- a = input("Enter Source File Name: ")
- d = input("Enter Destination Directory: ")
- b = [y, a, d]
- data = pickle.dumps(b)
- c.send(data)
- addr = c.recv(MAX_SIZE_BYTES)
- datar = pickle.loads(addr)
- print(datar[0])
- # Move the contents within the file
- # Not sure whether it works perfect or not
- if int(y) == 9:
- a = "Directory Names: "
- b = [y, a]
- data = pickle.dumps(b)
- c.send(data)
- addr = c.recv(MAX_SIZE_BYTES)
- datar = pickle.loads(addr)
- print(datar[0])
- print(datar[1])
- # Shows all directories created
- if int(y) == 10:
- a = "Memory Map"
- b = [y, a]
- data = pickle.dumps(b)
- c. send(data)
- addr = c.recv(MAX_SIZE_BYTES)
- datar = pickle.loads(addr)
- print(datar[0])
- mydir.memoryMap()
- # Exists the program
- if int(y) == 11:
- a = "Exiting program...."
- b = [y, a]
- data = pickle.dumps(b)
- c.send(data)
- addr = c.recv(MAX_SIZE_BYTES)
- datar = pickle.loads(addr)
- print("Exiting program by {}".format(host)) # prints out from which hostname was the client was closed
- print(datar[0])
- break
- c.close() #closes the connection
- if __name__ == '__main__':
- client()
|