-
Notifications
You must be signed in to change notification settings - Fork 1
/
serverScript.py
60 lines (46 loc) · 1.51 KB
/
serverScript.py
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
#!/usr/bin/python3
from socketserver import *
import os
def make_game_key(fileName):
splited = fileName.split(',')
if len(splited) is not 3 : return ""
else:
splited[0] = splited[0][len(splited[0])-6:len(splited[0])]
splited[2] = splited[2][0:3]
return "%s_%s_%s" % (splited[0],splited[1],splited[2])
TCPServer.allow_reuse_address = True
class MyTCPHandler(StreamRequestHandler):
def handle(self):
streamData = True
fileContent = ""
while(streamData):
fileManage = True
while(fileManage):
self.data = self.rfile.readline()
data = self.data.decode('utf-8')
if self.data.startswith(b"END_FILE"):
fileManage = False
break
if self.data.startswith(b"END_STREAM"):
streamData = False
break
if self.data.startswith(b"Name"):
fileName = data.split()[1]
print(fileName)
else: fileContent += data
if fileManage == False and os.path.isfile("./receivedFiles/"+fileName) == False:
if os.path.isdir("./receivedFiles/") == False:
os.mkdir("receivedFiles")
newFile = open("./receivedFiles/"+fileName, 'w')
newFile.write(fileContent)
result = make_game_key(fileName)
if result is "": print("Formato de arquivo recebido com problema!")
print(result)
if streamData == True and fileManage == False:
print("Enviando... OK_FILE\n")
self.wfile.write(b"OK_FILE")
if streamData == False:
print("Enviando... OK_STREAM\n")
self.wfile.write(b"OK_STREAM")
server = TCPServer(("127.0.0.1", 3560), MyTCPHandler)
server.serve_forever()