-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSimpleHTTPServer.patch
More file actions
74 lines (72 loc) · 2.82 KB
/
SimpleHTTPServer.patch
File metadata and controls
74 lines (72 loc) · 2.82 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
--- C:\Python27\Lib\SimpleHTTPServer-orig.py
+++ C:\Python27\Lib\SimpleHTTPServer.py
@@ -114,31 +114,65 @@
error). In either case, the headers are sent, making the
interface the same as for send_head().
- """
+ Modified to display in Directories-first order
+ ref: https://stackoverflow.com/questions/46951468/make-python-simplehttpserver-list-subdirectories-on-top
+ """
+ dirList = []
+ fileList = []
try:
list = os.listdir(path)
+ for i in list:
+ if os.path.isdir(os.path.join(path,i)):
+ dirList.append(i)
+ elif os.path.isfile(os.path.join(path,i)):
+ fileList.append(i)
except os.error:
self.send_error(404, "No permission to list directory")
return None
- list.sort(key=lambda a: a.lower())
+
+ dirList.sort(key=lambda a: a.lower())
+ fileList.sort(key=lambda a: a.lower())
+
+ # Merge both lists into the single 'list'
+ list = dirList
+ # Ignore if no sub-directories
+ if (len(list) != 0):
+ list.append('\n')
+
+ list += fileList
+
f = StringIO()
displaypath = cgi.escape(urllib.unquote(self.path))
f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
f.write("<html>\n<title>Directory listing for %s</title>\n" % displaypath)
f.write("<body>\n<h2>Directory listing for %s</h2>\n" % displaypath)
f.write("<hr>\n<ul>\n")
+
for name in list:
fullname = os.path.join(path, name)
displayname = linkname = name
+ flag = True
# Append / for directories or @ for symbolic links
if os.path.isdir(fullname):
displayname = name + "/"
linkname = name + "/"
- if os.path.islink(fullname):
+ elif os.path.islink(fullname):
displayname = name + "@"
# Note: a link to a directory displays with @ and links with /
- f.write('<li><a href="%s">%s</a>\n'
- % (urllib.quote(linkname), cgi.escape(displayname)))
+ elif os.path.isfile(fullname):
+ displayname = name
+ linkname = name
+ else:
+ # check for newlines in array
+ flag = False
+
+ if flag:
+ f.write('<li><a href="%s">%s</a>\n'
+ % (urllib.quote(linkname), cgi.escape(displayname)))
+ # Convert newline in list to line breaks
+ else:
+ f.write('<br /><br />')
+
f.write("</ul>\n<hr>\n</body>\n</html>\n")
length = f.tell()
f.seek(0)