-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
92 lines (86 loc) · 2.87 KB
/
client.py
File metadata and controls
92 lines (86 loc) · 2.87 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
import socket
import tkinter as tk
IP = socket.gethostbyname(socket.gethostname()) #Replace this with server ip address
PORT = 4469
ADDR = (IP, PORT)
FORMAT = "utf-8"
SIZE = 1024
def authenticate(client, username, password):
data = f"{username}@{password}"
client.send(data.encode(FORMAT))
response = client.recv(SIZE).decode(FORMAT)
if response == "OK@Authenticated":
print("Authentication successful")
return True
else:
print("Authentication failed. Please try again.")
return False
def get_credentials():
def submit():
nonlocal username, password
username = username_entry.get()
password = password_entry.get()
root.destroy()
username=None
password=None
root = tk.Tk()
root.title("Login")
root.geometry("250x100")
username_label = tk.Label(root, text="Username:")
username_label.grid(row=0, column=0, sticky="e")
username_entry = tk.Entry(root,width=15)
username_entry.grid(row=0, column=1)
username_entry.config(font=("Times New Roman",15))
password_label = tk.Label(root, text="Password:")
password_label.grid(row=1, column=0, sticky="e")
password_entry = tk.Entry(root, show="*",width=15)
password_entry.grid(row=1, column=1)
password_entry.config(font=("Times New Roman", 15))
submit_button = tk.Button(root, text="Submit", command=submit)
submit_button.grid(row=2, columnspan=2,pady=20,padx=10)
root.mainloop()
return username, password
def main():
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
dta = client.recv(SIZE).decode(FORMAT)
cd, messag = dta.split("@")
if cd == "DISCONNECTED":
print(f"[SERVER]: {messag}")
return
elif cd == "OK":
print(f"{messag}")
username, password = get_credentials()
a=authenticate(client,username,password)
if a==False:
return
while True:
data = client.recv(SIZE).decode(FORMAT)
cmd, msg = data.split("@")
if cmd == "DISCONNECTED":
print(f"[SERVER]: {msg}")
break
elif cmd == "OK":
print(f"{msg}")
data = input("> ")
data = data.split(" ")
cmd = data[0]
if cmd == "HELP":
client.send(cmd.encode(FORMAT))
elif cmd == "LOGOUT":
client.send(cmd.encode(FORMAT))
break
elif cmd == "LIST":
client.send(cmd.encode(FORMAT))
elif cmd == "UPLOAD":
path = data[1]
with open(f"{path}", "r") as f:
text = f.read()
filename = path.split("/")[-1]
send_data = f"{cmd}@{filename}@{text}"
client.send(send_data.encode(FORMAT))
elif cmd == "DELETE":
client.send(f"{cmd}@{data[1]}".encode(FORMAT))
print("Disconnected From The Server")
if __name__ == "__main__":
main()