-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer
82 lines (68 loc) · 2.12 KB
/
Server
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
import socket
import threading
import os
def SendAllFile(conn,addr):
basedir = "content"
subpath = os.listdir(basedir)
print("Sending All files")
for subnames in subpath:
print(subnames)
file = open(basedir + "/" + subnames, "rb")
buffersize = os.path.getsize(basedir + "/" + subnames)
#conn.send(subnames.encode())
_sizeheader = subnames.__len__()
_h = bytearray()
_h.append(0x00)
_h.append(_sizeheader)
conn.send(_h)
conn.send(subnames.encode())
t = True
while t:
buf = file.read(1024) #.encode()
if not buf:
t = False
print("cancel")
else:
conn.send(buf)
print("All files Send")
def Connect(conn,addr):
print(addr)
header = True
fi = ""
while header:
rc = conn.recv(2)
if rc[0] == 0x00 and rc[1] == 0x00:
SendAllFile(conn,addr)
return
fi += rc.decode("UTF-8")
print(fi)
if fi.find(".txt") > -1 or fi.find(".java") > -1 or fi.find(".xml") > -1 or fi.find(".png") > -1 or fi.find(".css") > -1 or fi.find(".js") > -1 or fi.find(".html") > -1:
print(fi)
header = False
dest = open("content/" + fi, 'w')
t = True
while t:
try:
data = conn.recv(1024)
dest.write(data.decode("UTF-8"))
except: pass
if not data:
print('cancel file')
t = False
dest.close()
conn.close()
def Start(host,port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(1)
while True:
try:
conn, addr = s.accept()
Connect(conn,addr)
except Exception as e:
print("Err " + e)
if __name__ == "__main__":
host = "192.168.1.33" #input("Please input your IP")
port = 5001 #int(input("Please input you PORT"))
_Server = threading.Thread(target=Start,args=(host,port))
_Server.run()