-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
89 lines (80 loc) · 2.6 KB
/
server.py
File metadata and controls
89 lines (80 loc) · 2.6 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
import socket
import _thread
import os
import os.path
# server config
serverpath = "/root/serv" # set this to your path you want to serve
myip = "192.168.0.101" # set this to the ip to bind
myport = 4000 # set this to the port to bind
authenabled = 0 # if 1, enable very basic password auth, if 0, disable auth
authhash = "" # this is the md5 hash of the correct password, this is required if auth is enabled but ignored if disabled
# end of server config
def newclient(sock, addr):
print(addr)
wel = f"Welcome to {socket.gethostname()}"
sock.send(wel.encode())
if(authenabled == 1):
sock.send("auth is active".encode())
passhash = sock.recv(1024).decode()
if(passhash == authhash):
sock.send("correct".encode())
else:
sock.send("incorrect".encode())
sock.close()
else:
sock.send("no auth is needed".encode())
while True:
filecommand = sock.recv(1024)
if(filecommand.decode() == "help"):
back = "commands:\nhelp - list this\nlist - list directory\ndownload - lets you download a file\ncloseserver - close socket"
sock.send(back.encode())
elif(filecommand.decode() == "list"):
note = "Note: this only lists files, dirs are not supported"
sock.send(note.encode())
size = []
filelist = [f for f in os.listdir(serverpath) if os.path.isfile(os.path.join(serverpath,f))]
for i in os.listdir(serverpath):
si = os.path.getsize(serverpath + "/" + i)
si = readsize(si)
size.append(si)
filelist2 = []
for x, y in zip(filelist, size):
filelist2.append(f"{x} -- {y}")
filelist2 = str(filelist2)
sock.send(filelist2.encode())
elif(filecommand.decode() == "download"):
whatfile = sock.recv(1024).decode()
if(os.path.exists(serverpath + "/" + whatfile)):
openfile = serverpath + "/" + whatfile
sendfile = open(openfile, "rb")
readcontent = sendfile.read(1024)
while(readcontent):
sock.send(readcontent)
readcontent = sendfile.read(1024)
sendfile.close()
else:
cool = "no file"
sock.send(cool.encode())
elif(filecommand.decode() == "query"):
checkfile = sock.recv(1024).decode()
if(os.path.exists(serverpath + "/" + checkfile)):
sock.send("Does".encode())
else:
sock.send("Does not".encode())
elif(filecommand.decode() == "closeserver"):
sock.close()
else:
back = "invalid command - use help for help"
sock.send(back.encode())
def readsize(size):
for u in ["B", "KiB", "MiB", "GiB", "TiB"]:
if size < 1024.0:
break
size /= 1024.0
return f"{size:.3f}{u}"
s = socket.socket()
s.bind((myip, myport))
s.listen(5)
while True:
sock, addr = s.accept()
_thread.start_new_thread(newclient,(sock, addr))