Skip to content

Commit 8ef2ed7

Browse files
view file sizes in list
1 parent 31b31c1 commit 8ef2ed7

File tree

2 files changed

+124
-107
lines changed

2 files changed

+124
-107
lines changed

client.py

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import ast
44
from getpass import getpass
55

6-
connip = "192.168.0.102"
6+
connip = "192.168.0.101"
77
connport = 4000
88

99
c = socket.socket()
@@ -16,66 +16,66 @@
1616
print(welcome.decode())
1717

1818
if(isauth == "auth is active"):
19-
print(isauth)
20-
sendpass = getpass("Enter password (will not be echoed): ")
21-
passcheck = hashlib.md5(sendpass.encode("utf-8")).hexdigest()
22-
c.send(passcheck.encode())
23-
check = c.recv(1024).decode()
24-
if(check == "correct"):
25-
print("correct password")
26-
else:
27-
print("incorrect password")
28-
quit()
19+
print(isauth)
20+
sendpass = getpass("Enter password (will not be echoed): ")
21+
passcheck = hashlib.md5(sendpass.encode("utf-8")).hexdigest()
22+
c.send(passcheck.encode())
23+
check = c.recv(1024).decode()
24+
if(check == "correct"):
25+
print("correct password")
26+
else:
27+
print("incorrect password")
28+
quit()
2929

3030

3131
while True:
32-
sender = input("fileshell> ")
33-
if(sender == "download"):
34-
choose = input("Name the file you want to download: ")
35-
newname = input("What name do you want to save it as? ")
36-
#check if file exists
37-
c.send("query".encode())
38-
c.send(choose.encode())
39-
check = c.recv(1024)
40-
check = check.decode()
41-
if(check == "Does not"):
42-
print("The file does not exist")
43-
continue
44-
c.send(sender.encode())
45-
c.send(choose.encode())
46-
print("File exists! Downloading file...")
47-
newfile = open(newname, "wb")
48-
content = c.recv(1024)
49-
while(content):
50-
try:
51-
newfile.write(content)
52-
content = c.recv(1024)
53-
except socket.timeout:
54-
newfile.close()
55-
break
56-
continue
57-
except ValueError:
58-
break
59-
continue
60-
newfile.close()
61-
print("Done downloading")
62-
continue
63-
if(sender == "list"):
64-
sender = sender.encode()
65-
c.send(sender)
66-
warnnote = c.recv(1024)
67-
print(warnnote.decode())
68-
print(" ")
69-
filelist = c.recv(4096)
70-
filelist = filelist.decode()
71-
filelist = ast.literal_eval(filelist)
72-
for file in filelist:
73-
print(file)
74-
continue
75-
sender = sender.encode()
76-
c.send(sender)
77-
textback = c.recv(1024)
78-
textback = textback.decode()
79-
print(textback)
80-
81-
32+
sender = input("fileshell> ")
33+
if(sender == "download"):
34+
choose = input("Name the file you want to download: ")
35+
newname = input("What name do you want to save it as? ")
36+
#check if file exists
37+
c.send("query".encode())
38+
c.send(choose.encode())
39+
check = c.recv(1024)
40+
check = check.decode()
41+
if(check == "Does not"):
42+
print("The file does not exist")
43+
continue
44+
c.send(sender.encode())
45+
c.send(choose.encode())
46+
print("File exists! Downloading file...")
47+
newfile = open(newname, "wb")
48+
content = c.recv(1024)
49+
while(content):
50+
try:
51+
newfile.write(content)
52+
content = c.recv(1024)
53+
except socket.timeout:
54+
newfile.close()
55+
break
56+
continue
57+
except ValueError:
58+
break
59+
continue
60+
newfile.close()
61+
print("Done downloading")
62+
continue
63+
if(sender == "list"):
64+
sender = sender.encode()
65+
c.send(sender)
66+
warnnote = c.recv(1024)
67+
print(warnnote.decode())
68+
print(" ")
69+
filelist = c.recv(4096)
70+
filelist = filelist.decode()
71+
filelist = ast.literal_eval(filelist)
72+
for file in filelist:
73+
print(file)
74+
continue
75+
sender = sender.encode()
76+
c.send(sender)
77+
textback = c.recv(1024)
78+
textback = textback.decode()
79+
print(textback)
80+
81+

server.py

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,53 +22,70 @@
2222
wel = f"Welcome to {socket.gethostname()}"
2323
sock.send(wel.encode())
2424

25+
def readsize(size):
26+
for u in ["B", "KiB", "MiB", "GiB", "TiB"]:
27+
if size < 1024.0:
28+
break
29+
size /= 1024.0
30+
return f"{size:.3f}{u}"
31+
2532
if(authenabled == 1):
26-
sock.send("auth is active".encode())
27-
passhash = sock.recv(1024).decode()
28-
if(passhash == authhash):
29-
sock.send("correct".encode())
30-
else:
31-
sock.send("incorrect".encode())
32-
s.close()
33-
quit()
33+
sock.send("auth is active".encode())
34+
passhash = sock.recv(1024).decode()
35+
if(passhash == authhash):
36+
sock.send("correct".encode())
37+
else:
38+
sock.send("incorrect".encode())
39+
s.close()
40+
quit()
3441
else:
35-
sock.send("no auth is needed".encode())
42+
sock.send("no auth is needed".encode())
3643

3744
while True:
38-
filecommand = sock.recv(1024)
39-
if(filecommand.decode() == "help"):
40-
back = "commands:\nhelp - list this\nlist - list directory\ndownload - lets you download a file\ncloseserver - close socket"
41-
sock.send(back.encode())
42-
elif(filecommand.decode() == "list"):
43-
note = "Note: this only lists files, dirs are not supported"
44-
sock.send(note.encode())
45-
filelist = [f for f in os.listdir(serverpath) if os.path.isfile(os.path.join(serverpath,f))]
46-
filelist = str(filelist)
47-
sock.send(filelist.encode())
48-
elif(filecommand.decode() == "download"):
49-
whatfile = sock.recv(1024).decode()
50-
if(os.path.exists(serverpath + "/" + whatfile)):
51-
#goodnote = "exists"
52-
#goodcheck = sock.send(goodnote.encode())
53-
openfile = serverpath + "/" + whatfile
54-
sendfile = open(openfile, "rb")
55-
readcontent = sendfile.read(1024)
56-
while(readcontent):
57-
sock.send(readcontent)
58-
readcontent = sendfile.read(1024)
59-
sendfile.close()
60-
else:
61-
cool = "no file"
62-
sock.send(cool.encode())
63-
elif(filecommand.decode() == "query"):
64-
checkfile = sock.recv(1024).decode()
65-
if(os.path.exists(serverpath + "/" + checkfile)):
66-
sock.send("Does".encode())
67-
else:
68-
sock.send("Does not".encode())
69-
elif(filecommand.decode() == "closeserver"):
70-
s.close()
71-
quit()
72-
else:
73-
back = "invalid command - use help for help"
74-
sock.send(back.encode())
45+
filecommand = sock.recv(1024)
46+
if(filecommand.decode() == "help"):
47+
back = "commands:\nhelp - list this\nlist - list directory\ndownload - lets you download a file\ncloseserver - close socket"
48+
sock.send(back.encode())
49+
elif(filecommand.decode() == "list"):
50+
note = "Note: this only lists files, dirs are not supported"
51+
sock.send(note.encode())
52+
size = []
53+
filelist = [f for f in os.listdir(serverpath) if os.path.isfile(os.path.join(serverpath,f))]
54+
#filelist = str(filelist)
55+
#sock.send(filelist.encode())
56+
for i in os.listdir(serverpath):
57+
si = os.path.getsize(serverpath + "/" + i)
58+
si = readsize(si)
59+
size.append(si)
60+
filelist2 = []
61+
for x, y in zip(filelist, size):
62+
filelist2.append(f"{x} -- {y}")
63+
filelist2 = str(filelist2)
64+
sock.send(filelist2.encode())
65+
elif(filecommand.decode() == "download"):
66+
whatfile = sock.recv(1024).decode()
67+
if(os.path.exists(serverpath + "/" + whatfile)):
68+
#goodnote = "exists"
69+
#goodcheck = sock.send(goodnote.encode())
70+
openfile = serverpath + "/" + whatfile
71+
sendfile = open(openfile, "rb")
72+
readcontent = sendfile.read(1024)
73+
while(readcontent):
74+
sock.send(readcontent)
75+
readcontent = sendfile.read(1024)
76+
sendfile.close()
77+
else:
78+
cool = "no file"
79+
sock.send(cool.encode())
80+
elif(filecommand.decode() == "query"):
81+
checkfile = sock.recv(1024).decode()
82+
if(os.path.exists(serverpath + "/" + checkfile)):
83+
sock.send("Does".encode())
84+
else:
85+
sock.send("Does not".encode())
86+
elif(filecommand.decode() == "closeserver"):
87+
s.close()
88+
quit()
89+
else:
90+
back = "invalid command - use help for help"
91+
sock.send(back.encode())

0 commit comments

Comments
 (0)