-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
126 lines (118 loc) · 5.45 KB
/
client.py
File metadata and controls
126 lines (118 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import socket
import sys
def clientServerCommunication(message):
#print(message)
client_socket.send(message.encode()) # send message
def serverClientCommuncation():
try:
data = client_socket.recv(2048).decode() # receive response
return str(data)
except:
print("Error! Data from Server not received!")
sys.exit()
#print(data)
return str(data)
def clientCLI():
print("Welcome to the UI for the file management system.")
while(True):
choice = input("Enter p to proceed. e to exit the UI: ")
clientServerCommunication(choice)
if (choice == 'p'):
diskOrFile = input("Enter d to operate on Disk. f to operate on File: ")
clientServerCommunication(diskOrFile)
if (diskOrFile == 'd'):
choice = input("Enter c to create file, d to delete file, m for mem map and anykey for directory creation: ")
clientServerCommunication(choice)
if (choice == 'c'):
clientServerCommunication(input("Enter file name: "))
clientServerCommunication(input("Enter dir name: "))
if (int(serverClientCommuncation()) == 1):
print("File Created Successfully")
else:
print("File Not created!")
elif (choice == 'd'):
clientServerCommunication(input("Enter file name: "))
clientServerCommunication(input("Enter Directory name: "))
if (int(serverClientCommuncation())==1):
print("File deleted successfully!")
else:
print("File not deleted!")
elif (choice == 'm'):
print(serverClientCommuncation())
else:
clientServerCommunication(input("Enter Dir name: "))
clientServerCommunication(input("Enter Parent Directory: "))
if (int(serverClientCommuncation()) == 1):
print("Dir Created Succesfully!")
else:
print("Directory not created!")
elif (diskOrFile == 'f'):
clientServerCommunication(input ("Enter file Name: "))
confirmation = int(serverClientCommuncation())
if (confirmation == 1):
print("Delivered!")
else:
print("Invalid fileName!")
continue
#while (True):
mode = input ("w to write, r to read,t to trunc: ")
clientServerCommunication(mode)
if (mode == 'w'):
if int(serverClientCommuncation()) == 1:
print("File Opened with Write Priveleges")
clientServerCommunication(input("Enter String: "))
clientServerCommunication(input("Enter loc: "))
clientServerCommunication(input("Enter mode (a for append, w for write, W for write at loc):"))
if int(serverClientCommuncation()) == 1:
print("Written to File succesfully")
else:
print("Failed to Write to File!")
elif (mode == 't'):
if int(serverClientCommuncation()) == 1:
print("File Opened with Write Priveleges")
clientServerCommunication(input("Enter size to truncate to: "))
if int(serverClientCommuncation()) == 1:
print("Truncation Succesful!")
else:
print("Failed to truncate File!")
elif (mode == 'r'):
if int(serverClientCommuncation()) == 1:
print("File Opened with Read Priveleges")
check = input("Enter a for for all. s for specific: ")
clientServerCommunication(check)
if (check == 'a'):
print(serverClientCommuncation())
else:
start,size = clientServerCommunication(input("Enter start: ")),clientServerCommunication(input("Enter size: "))
print(serverClientCommuncation())
if (choice == 'e'):
break
def userName():
####Enter username
clientServerCommunication(input("Enter username: "))
sessionNumber = int(serverClientCommuncation())
#print(sessionNumber)
if (int(sessionNumber) == -1):
print("User sessions Overflow. User not logged in!\n")
exit
else:
print("User Session Established! Session No: ", int(sessionNumber) , "\n")
if __name__ == '__main__':
ip = input("Enter IP Address: ")
#host = socket.gethostbyaddr("192.168.1.2")[0][:-5] # as both code is running on same pc
try:
host = socket.gethostbyaddr(ip)[0]
print(host)
except:
print("Invalid IP Address!\n")
sys.exit()
port = 95 # socket server port number
client_socket = socket.socket() # instantiate
try:
client_socket.connect((host, port)) # connect to the server
print("Succesfully connected to the server!\n")
except:
print("Unable to connect to the Server!\n")
sys.exit()
userName()
clientCLI()