-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
153 lines (144 loc) · 6.04 KB
/
server.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import tornado.ioloop
import tornado.web
import tornado.options
from tornado import gen
import os,sys
from stat import *
GB = 1024 * 1024 * 1024
def read_in_chunks(infile, chunk_size=1024*1024):
chunk = infile.read(chunk_size)
while chunk:
yield chunk
chunk = infile.read(chunk_size)
def read_in_chunks_pos(base_dir, pos, size, chunk_size=1024*1024):
with open(base_dir, 'rb') as infile:
infile.seek(int(pos))
no = int(size) // chunk_size
i = 0
#chunk = infile.read(chunk_size)
#while chunk and i<no:
while i<no:
chunk = infile.read(chunk_size)
yield chunk
i = i+1
if (int(size) % chunk_size) != 0:
last = int(size) % chunk_size
chunk = infile.read(last)
yield chunk
infile.close()
class ReadRequestHandler(tornado.web.RequestHandler):
@gen.coroutine
def get(self):
total_sent = 0
uid = self.get_argument('uid')
gid = self.get_argument('gid')
base_dir = self.get_argument('filepath')
pos = self.get_argument('pos')
size = self.get_argument('size')
# Python protocol does not require () on it's if statements like you are
if base_dir==None or uid==None or gid==None or pos==None or size==None:
self.write("Invalid argument!You caused a %d error."%status_code)
exit(1)
if os.path.exists(base_dir):
statinfo = os.stat(base_dir)
if(int(uid)==statinfo.st_uid and int(gid)==statinfo.st_gid):
mode = statinfo.st_mode
else:
self.write("Permission denied.")
exit(1)
else:
self.write("File or directory doesn't exist!You caused a %d error."%status_code)
exit(1)
if (S_ISDIR(mode)):
self.write("This is not a file!You caused a %d error."%status_code)
exit(1)
else:
# with open(base_dir, 'rb') as infile:
for chunk in read_in_chunks_pos(base_dir,pos,size):
self.write(chunk)
yield gen.Task(self.flush)
total_sent += len(chunk)
print("sent",total_sent)
self.finish()
class ListRequestHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@gen.coroutine
def get(self):
uid = self.get_argument('uid')
gid = self.get_argument('gid')
base_dir = self.get_argument('path')
if (base_dir==None or uid==None or gid==None):
self.write("Invalid argument!You caused a %d error."%status_code)
exit(1)
if(os.path.exists(base_dir)):
statinfo = os.stat(base_dir)
self.write('{'+'"father_node"'+':')
statdict = {'path':base_dir,'mode':str(statinfo.st_mode),'ino':str(statinfo.st_ino),'dev':str(statinfo.st_dev),'nlink':str(statinfo.st_nlink),'uid':str(statinfo.st_uid),'gid':str(statinfo.st_gid),'size':str(statinfo.st_size),'atime':str(statinfo.st_atime),'mtime':str(statinfo.st_mtime),'ctime':str(statinfo.st_ctime)}
if(int(uid)==statinfo.st_uid and int(gid)==statinfo.st_gid):
self.write(statdict)
mode = statinfo.st_mode
else:
self.write("Permission denied.")
exit(1)
else:
self.write("File or directory doesn't exist!You caused a %d error."%status_code)
exit(1)
if (S_ISDIR(mode)==None):
self.write("This is not a directory!You caused a %d error."%status_code)
exit(1)
else:
files = os.listdir(base_dir)
for f in files:
statinfo = os.stat(base_dir + '/' +f)
self.write(',"'+f+'":')
statdict = {'path':(base_dir + '/' +f),'mode':str(statinfo.st_mode),'ino':str(statinfo.st_ino),'dev':str(statinfo.st_dev),'nlink':str(statinfo.st_nlink),'uid':str(statinfo.st_uid),'gid':str(statinfo.st_gid),'size':str(statinfo.st_size),'atime':str(statinfo.st_atime),'mtime':str(statinfo.st_mtime),'ctime':str(statinfo.st_ctime)}
# print ("statdict",statdict)
self.write(statdict)
self.write("}")
def write_error(self,status_code,**kwargs):
self.write("Gosh darnit,user!You caused a %d error."%status_code)
class StreamingRequestHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@gen.coroutine
def get(self):
# back as right now this is limited to what is hard coded in
total_sent = 0
uid = self.get_argument('uid')
gid = self.get_argument('gid')
base_dir = self.get_argument('filepath')
if (base_dir==None or uid==None or gid==None):
self.write("Invalid argument!You caused a %d error."%status_code)
exit(1)
if(os.path.exists(base_dir)):
statinfo = os.stat(base_dir)
if(int(uid)==statinfo.st_uid and int(gid)==statinfo.st_gid):
mode = statinfo.st_mode
else:
self.write("Permission denied.")
exit(1)
else:
self.write("File or directory doesn't exist!You caused a %d error."%status_code)
exit(1)
if (S_ISDIR(mode)):
self.write("This is not a file!You caused a %d error."%status_code)
exit(1)
else:
with open(base_dir, 'rb') as infile:
for chunk in read_in_chunks(infile):
self.write(chunk)
yield gen.Task(self.flush)
total_sent += len(chunk)
print("sent",total_sent)
self.finish()
if __name__ == "__main__":
# this was connected to the pyCurl call and as far as I know now not
# beng used so try without to insure it's no longer needed
# tornado.options.parse_command_line()
print (tornado.version)
application = tornado.web.Application([
(r"/download", StreamingRequestHandler),
(r"/list",ListRequestHandler),
(r"/read",ReadRequestHandler)
])
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()