-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
executable file
·117 lines (85 loc) · 3.09 KB
/
Copy pathMain.py
File metadata and controls
executable file
·117 lines (85 loc) · 3.09 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
#!/usr/bin/python3
import os
import cgi
# root = "/home/calvin/MainCode/Self"
root = "/var/www/html/"
# parses url
def get_args():
arguments = cgi.FieldStorage()
path = arguments.getfirst("dir")
if not path:
path = ""
search = arguments.getfirst("search")
if not search:
search = ""
return path, search
# Converts bytes to needed unit
def format_file_size(size):
unit = 0
units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
while (size >= 1024):
size /= 1024
unit += 1
return f"{round(size, 2)} {units[unit]}"
def print_directery(path):
print_list = []
for x in os.listdir(root + path):
if os.path.isdir(root + path + "/" + x):
print_list.append(f"<li><a href=\"?dir={path}/{x}\">{x}</a></li>")
else:
file_size = os.path.getsize(f"{root}{path}/{x}")
print_list.append(f"<li><a href=\"{path}/{x}\">{x} 	 | 	 {format_file_size(file_size)}</a></li>")
return print_list
# Recursivly finds file names that match the search term
def search_func(search, path):
print_list = []
for x in os.listdir(root + path):
if os.path.isdir(root + path + "/" + x):
# print (f"<h1>search = {search}, path = {path}, x = {x} ----- </h1>")
search_func(search, path + "/" + x)
if search.lower() in x.lower():
file_size = os.path.getsize(f"{root}{path}/{x}")
print_list.append(f"<li><a href=\"{path}/{x}\">{x} 	 | 	 {format_file_size(file_size)}</a></li>")
return print_list
def alpha_sort(test_list):
def myFunc(e):
return e[e.rfind('">'):]
test_list.sort(key=myFunc)
def main():
css = "body {color: #fca311; background-color: #14213d;} a {color: #ffffff; font-size: 1.5em; line-height: 1.6;} a:hover {color: #f8adff} ol li {list-style-type: none;}"
path, search = get_args()
print_list = []
print ("Content-type:text/html\n\n")
print (f"<style>{css}</style>")
print ("<html>")
print ("<head>")
print ("<title>Index - Cal</title>")
print ("</head>")
print ("<body>")
print ("<form action=\"?\" method=\"GET\">")
print ("<h2>Index of </h2>")
# print ("Content-Type: text/html")
print (f'<input type="hidden" name="dir" value="{path}" />')
print ("<input type=\"text\" name=\"search\">")
print ("<input type=\"submit\" value=\"send\">")
print ("<ol>")
# allows navigation to the parent directory
PD = path[:path.rfind("/")] #the ":" allows the string is a substring operation
print (f"<li><a href=\"?dir={PD}\"> << PD</a></li>")
if search == "":
print_list = print_directery(path)
if search != "":
print_list = search_func(search, path)
alpha_sort(print_list)
for x in print_list:
print (x)
print ("</ol>")
print ("</form>")
print ("</body>")
print ("</html>")
if __name__ == '__main__':
main()
# run command:
# ./Mian.py
# push to server command:
# scp /home/calvin/MainCode/PyServer/Main.py ubuntu@192.168.50.2:/usr/lib/cgi-bin/foo/