This repository has been archived by the owner on Dec 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility_classes.py
70 lines (60 loc) · 1.81 KB
/
utility_classes.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
60
61
62
63
64
65
66
67
68
69
70
import socket
import threading
from socketserver import ThreadingMixIn
import zipfile
import os
import io
from http.server import HTTPServer
from enum import Enum
def get_rid_of_last_path_sep(path):
if path[-1] == os.path.sep:
return path[:-1]
else:
return path
class FileType(Enum):
undecided = 0
realfile = 1
zipstream = 2
class ZipStream(threading.Thread):
def __init__(self, dirToZip, group=None, target=None, name=None,
args=(), kwargs=None, *, daemon=None):
super(ZipStream, self).__init__(group, target, name,
args, kwargs, daemon=daemon)
self.__dirToZip = dirToZip
(self.__readpipefd, self.__writepipefd) = os.pipe()
def run(self):
pipeout = io.FileIO(self.__writepipefd, "wb")
z=zipfile.ZipFile(pipeout,"w", zipfile.ZIP_DEFLATED)
for x in os.walk(self.__dirToZip):
for y in x[2]:
z.write(x[0]+"/"+y)
z.close()
#os.close(self.__pipefd[1])
def getPipein(self):
return io.FileIO(self.__readpipefd,"rb")
class ServerThread(threading.Thread):
__server = None
def set_server(self, server):
"""
:type server: HTTPServer
"""
self.__server = server
def stop(self): #TODO should also stop the running zipper thread(s)
if (self.__server != None) and (self.isAlive() == True):
self.__server.shutdown()
print("Server stopped serving on %s on port %s." %
(self.__server.server_address[0], self.__server.server_address[1])
)
else:
return
def run(self):
if self.__server != None:
print("Server started listening on %s on port %s." %
(self.__server.server_address[0], self.__server.server_address[1])
)
self.__server.serve_forever()
else:
return
class ServerV6(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
address_family = socket.AF_INET6