Skip to content

Commit eeb4fec

Browse files
added threading
1 parent 8ef2ed7 commit eeb4fec

File tree

1 file changed

+69
-71
lines changed

1 file changed

+69
-71
lines changed

server.py

Lines changed: 69 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import socket
2+
import _thread
23
import os
34
import os.path
45

@@ -14,78 +15,75 @@
1415
authhash = "" # this is the md5 hash of the correct password, this is required if auth is enabled but ignored if disabled
1516
# end of server config
1617

17-
s = socket.socket()
18-
s.bind((myip, myport))
19-
s.listen(5)
20-
sock, addr = s.accept()
21-
print(addr)
22-
wel = f"Welcome to {socket.gethostname()}"
23-
sock.send(wel.encode())
18+
def newclient(sock, addr):
19+
print(addr)
20+
wel = f"Welcome to {socket.gethostname()}"
21+
sock.send(wel.encode())
2422

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}"
23+
if(authenabled == 1):
24+
sock.send("auth is active".encode())
25+
passhash = sock.recv(1024).decode()
26+
if(passhash == authhash):
27+
sock.send("correct".encode())
28+
else:
29+
sock.send("incorrect".encode())
30+
sock.close()
31+
else:
32+
sock.send("no auth is needed".encode())
33+
while True:
34+
filecommand = sock.recv(1024)
35+
if(filecommand.decode() == "help"):
36+
back = "commands:\nhelp - list this\nlist - list directory\ndownload - lets you download a file\ncloseserver - close socket"
37+
sock.send(back.encode())
38+
elif(filecommand.decode() == "list"):
39+
note = "Note: this only lists files, dirs are not supported"
40+
sock.send(note.encode())
41+
size = []
42+
filelist = [f for f in os.listdir(serverpath) if os.path.isfile(os.path.join(serverpath,f))]
43+
for i in os.listdir(serverpath):
44+
si = os.path.getsize(serverpath + "/" + i)
45+
si = readsize(si)
46+
size.append(si)
47+
filelist2 = []
48+
for x, y in zip(filelist, size):
49+
filelist2.append(f"{x} -- {y}")
50+
filelist2 = str(filelist2)
51+
sock.send(filelist2.encode())
52+
elif(filecommand.decode() == "download"):
53+
whatfile = sock.recv(1024).decode()
54+
if(os.path.exists(serverpath + "/" + whatfile)):
55+
openfile = serverpath + "/" + whatfile
56+
sendfile = open(openfile, "rb")
57+
readcontent = sendfile.read(1024)
58+
while(readcontent):
59+
sock.send(readcontent)
60+
readcontent = sendfile.read(1024)
61+
sendfile.close()
62+
else:
63+
cool = "no file"
64+
sock.send(cool.encode())
65+
elif(filecommand.decode() == "query"):
66+
checkfile = sock.recv(1024).decode()
67+
if(os.path.exists(serverpath + "/" + checkfile)):
68+
sock.send("Does".encode())
69+
else:
70+
sock.send("Does not".encode())
71+
elif(filecommand.decode() == "closeserver"):
72+
sock.close()
73+
else:
74+
back = "invalid command - use help for help"
75+
sock.send(back.encode())
3176

32-
if(authenabled == 1):
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()
41-
else:
42-
sock.send("no auth is needed".encode())
77+
def readsize(size):
78+
for u in ["B", "KiB", "MiB", "GiB", "TiB"]:
79+
if size < 1024.0:
80+
break
81+
size /= 1024.0
82+
return f"{size:.3f}{u}"
4383

84+
s = socket.socket()
85+
s.bind((myip, myport))
86+
s.listen(5)
4487
while True:
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())
88+
sock, addr = s.accept()
89+
_thread.start_new_thread(newclient,(sock, addr))

0 commit comments

Comments
 (0)