-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
132 lines (114 loc) · 4.87 KB
/
client.py
File metadata and controls
132 lines (114 loc) · 4.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
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
127
128
129
130
131
132
import socket
import os
import tkinter as tk
from tkinter import filedialog as fd
from tkinter import messagebox as message
from tkinter import simpledialog as sd
import time
try:
from getmac import getmac
except:
import subprocess
import sys
subprocess.check_call([sys.executable,"-m","pip","install","getmac"])
from getmac import getmac
MAX_CHUNK_SIZE = 1024 * 1024 * 1024
def default():
print("Error")
def select_file():
#to send the files to the Server
filetypes = (('Text files', '*.txt'),('All files', '*.*'))
filename = fd.askopenfilename(title='Open a file',initialdir='/',filetypes=filetypes)
if filename == '':
print("Please select a file.")
else:
client_socket.send(b"MAC"+address.encode()[:MAX_CHUNK_SIZE])
file_name = os.path.basename(filename)
client_socket.send(file_name.encode()[:1024 * 1024 * 1024])
time.sleep(1)
with open(filename,"r") as file:
data = file.read()
client_socket.send(data.encode()[:1024 * 1024 * 1024])
def get_files():
file_status = "0" #false hai ye
#this function will work to get the files from the server
client_socket.send(b"get"+address.encode()[:1024 * 1024 * 1024])
files = client_socket.recv(MAX_CHUNK_SIZE).decode()
files_lable.config(text="Files Name: "+files)
files_lable.pack()
fileName = sd.askstring("Input", "Enter File Name Here: ", parent=root)
if fileName == None:
print("Please give file name")
default()
files_lable.config(text="")
client_socket.send(file_status.encode()[:MAX_CHUNK_SIZE]) #send status (false)
else:
file_status = "1"
client_socket.send(file_status.encode()[:MAX_CHUNK_SIZE]) #send status (true)
time.sleep(1)
client_socket.send(fileName.encode()[:MAX_CHUNK_SIZE])
rev_name = client_socket.recv(MAX_CHUNK_SIZE).decode()
print("fileName", rev_name)
index = rev_name.find(".")
file_content = client_socket.recv(MAX_CHUNK_SIZE).decode()
print("Data", file_content)
save_file = fd.asksaveasfilename(initialfile=rev_name[:index], defaultextension=rev_name[index:], initialdir="/")
if save_file:
with open(save_file,"w") as file:
file.write(file_content)
def exit_program():
#to close the connection with server
global server_ip, server_port, address ,client_socket
client_socket.send("close".encode()[:1024 * 1024 * 1024])
client_socket.close()
root.quit()
exit(0)
def connect_to_server():
global server_ip, server_port, address ,client_socket
server_ip = ip_entry.get()
server_port = int(port_entry.get())
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((server_ip,server_port))
select_button.config(state="active")
get_button.config(state="active")
address = getmac.get_mac_address()
client_socket.send(address.encode()[:1024 * 1024 * 1024])
count =client_socket.recv(MAX_CHUNK_SIZE).decode()
if count == "1":
message.askokcancel("Welcome!!!","Welcome Back You Are Visiting The Server For "+count+"st Time")
elif count == "2":
message.askokcancel("Welcome!!!","Welcome Back You Are Visiting The Server For "+count+"nd Time")
elif count == "3":
message.askokcancel("Welcome!!!","Welcome Back You Are Visiting The Server For "+count+"rd Time")
else:
message.askokcancel("Welcome!!!","Welcome Back You Are Visiting The Server For "+count+"th Time")
#gui for client side
root = tk.Tk()
root.title("Client GUI")
root.geometry("600x300")
# input fields
input_frame = tk.Frame(root)
input_frame.pack(pady=10)
ip_label = tk.Label(input_frame, text="Server IP:")
ip_label.grid(row=0, column=0, padx=5, pady=5)
ip_entry = tk.Entry(input_frame)
ip_entry.grid(row=0, column=1, padx=5, pady=5)
port_label = tk.Label(input_frame, text="Server Port:")
port_label.grid(row=1, column=0, padx=5, pady=5)
port_entry = tk.Entry(input_frame)
port_entry.grid(row=1, column=1, padx=5, pady=5)
# Button frame
button_frame = tk.Frame(root)
button_frame.pack(pady=10)
connect_button = tk.Button(button_frame, text="Connect to Server", command=connect_to_server)
connect_button.grid(row=0, column=0, padx=5, pady=5)
select_button = tk.Button(button_frame, text="Select File", command=select_file, state="disabled")
select_button.grid(row=0, column=1, padx=5, pady=5)
get_button = tk.Button(button_frame, text="Get Files", command=get_files, state="disabled")
get_button.grid(row=0, column=2, padx=5, pady=5)
exit_button = tk.Button(button_frame, text="Exit", command=exit_program)
exit_button.grid(row=0, column=3, padx=5, pady=5)
# Files label
files_lable = tk.Label(root)
files_lable.pack(pady=10)
root.mainloop()