-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
117 lines (109 loc) · 4.13 KB
/
Copy pathmain.py
File metadata and controls
117 lines (109 loc) · 4.13 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
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
from http.server import SimpleHTTPRequestHandler, HTTPServer, HTTPStatus
from socketserver import ThreadingMixIn
import ctypes, html, io, os, subprocess, sys, urllib
class FMHandler(SimpleHTTPRequestHandler):
def do_GET(self):
if len(self.path) > 2 and self.path[-2] == '?':
self.process(self.path[-1], self.path[1:-2])
self.send_response(HTTPStatus.SEE_OTHER)
self.send_header('Location', '.')
self.end_headers()
return
super().do_GET()
def do_POST(self):
size = int(self.headers['content-length'])
boundary = self.headers['content-type'].split("=")[1].encode()
line = self.rfile.readline()
size -= len(line)
line = self.rfile.readline()
size -= len(line)
s = line.decode('utf-8')
l = s.find('filename=')
fn = s[l+10:-3]
line = self.rfile.readline()
size -= len(line)
line = self.rfile.readline()
size -= len(line)
last = self.rfile.readline()
size -= len(last)
if size > 0 and len(fn) > 0:
with open(fn, 'wb') as w:
while size > 0:
line = self.rfile.readline()
size -= len(line)
if boundary in line:
last = last[0:-1]
if last.endswith(b'\r'):
last = last[0:-1]
w.write(last)
else:
w.write(last)
last = line
if len(self.path) > 2 and self.path[-2] == '?':
self.path = self.path[:1+self.path.rfind('/')]
super().do_GET()
def process(self, cmd, f):
path = self.translate_path(f)
if cmd == 'd':
subprocess.run(["rm", "-rf", path])
elif cmd == 'x':
subprocess.run(["7z", "x", path])
elif cmd == 't':
subprocess.run(["7z", "a", path+'.tar', path])
elif cmd == 'z':
subprocess.run(["7z", "a", path+'.7z', path])
def calc_size(self, num):
for x in ['B', 'KB', 'MB', 'GB', 'TB']:
if num < 1024.0:
return "%3.2f %s" % (num, x)
num /= 1024.0
def get_free_space(self):
if os.name == 'nt':
free_bytes = ctypes.c_ulonglong(0)
ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(), None, None, ctypes.pointer(free_bytes))
return free_bytes.value
else:
st = os.statvfs('/')
return st.f_bavail * st.f_frsize
def list_directory(self, path):
list = os.listdir(path)
list.sort(key=lambda a: a.lower())
r = []
root = urllib.parse.unquote(self.path, errors='surrogatepass')
root = html.escape(root, quote=False)
title = 'Directory listing for %s' % root
r.append('<html>\n<head>')
r.append('<meta http-equiv="Content-Type" content="text/html">')
r.append('<title>%s</title>\n</head>' % title)
r.append('<body>\n<h1>%s</h1>' % title)
r.append('<form ENCTYPE="multipart/form-data" method="post">')
r.append('<input name="file" type="file"/>')
r.append('<input type="submit" value="upload"/></form>\n<table border=1>')
for name in list:
fullname = os.path.join(path, name)
link = urllib.parse.quote(name, errors='surrogatepass')
display = html.escape(name, quote=False)
if os.path.isdir(fullname):
size = ''
bg = 'gainsboro'
href = link + '/'
else:
size = self.calc_size(os.stat(fullname).st_size)
bg = 'white'
href = '/files' + self.path + link
r.append('<tr bgcolor="%s"><td><a href="%s?d">D</td><td><a href="%s">%s</a></td><td>%s</td><td><a href="%s?x">X</td><td><a href="%s?t">T</td><td><a href="%s?z">Z</td></tr>' % (bg, link, href, display, size, link, link, link))
r.append('</table>\n<br>Remaining Space: %s</body>\n</html>\n' % self.calc_size(self.get_free_space()))
encoded = '\n'.join(r).encode('utf-8', 'surrogateescape')
f = io.BytesIO()
f.write(encoded)
f.seek(0)
self.send_response(HTTPStatus.OK)
self.send_header("Content-type", "text/html; charset=UTF-8")
self.send_header("Content-Length", str(len(encoded)))
self.end_headers()
return f
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
daemon_threads = True
if __name__ == '__main__':
port = int(sys.argv[1]) if len(sys.argv) > 1 else 8000
ThreadedHTTPServer(('127.0.0.1', port), FMHandler).serve_forever()