Skip to content

Instantly share code, notes, and snippets.

@zsrinivas
Last active November 15, 2015 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsrinivas/38d382872086e6bf154f to your computer and use it in GitHub Desktop.
Save zsrinivas/38d382872086e6bf154f to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import os
import stat
from os import environ, listdir
from jinja2 import Template
from datetime import datetime
from mimetypes import guess_type
def mylistdir(path):
items = listdir(path)
for item in items:
itemstat = os.lstat(os.path.abspath(path) + '/' + item)
modes = ['-', 'd'][stat.S_ISDIR(itemstat.st_mode)] + ''.join(
['---------', 'rwxrwxrwx'][int(c)][i] for i, c in enumerate(
bin(stat.S_IMODE(itemstat.st_mode))[2:].zfill(9)))
lastmodified = str(datetime.fromtimestamp(int(itemstat.st_mtime)))
itemtype = [guess_type(item)[0] or '-', 'directoy'][stat.S_ISDIR(itemstat.st_mode)]
yield modes, item, lastmodified, itemstat.st_size, itemtype, environ["REQUEST_URI"] + item + '/'
cssstyle = open('/home/eightnoteight/.config/lighty/style.css', 'r').read()
print(Template(open('/home/eightnoteight/.config/lighty/index.jinja2', 'r').read()).render(
requri=environ["REQUEST_URI"],
cssstyle=cssstyle,
dirlist=mylistdir(environ["DOCUMENT_ROOT"] + environ["REQUEST_URI"])
))
print(environ["REQUEST_URI"], listdir())
print()
print()
print()
print()
print(environ)
<html>
<head>
<title>Index of {{ requri }}</title>
<style type="text/css">
{{ cssstyle }}
</style>
<style type="text/css">
th, td { font: 90% monospace; text-align: left; line-height: 1.0;}
.table > thead > tr > th,
.table > tbody > tr > th,
.table > tfoot > tr > th,
.table > thead > tr > td,
.table > tbody > tr > td,
.table > tfoot > tr > td {
padding: 6px;
}
</style>
</head>
<body>
<h2>Index of {{ requri }}</h2>
<div class="list">
<table summary="Directory Listing" class="table">
<thead>
<tr>
<th class="modes">Modes</th>
<th class="name">Name</th>
<th class="modified">Last Modified</th>
<th class="size" style="text-align: right;">Size</th>
<th class="type">Type</th>
</tr>
</thead>
<tbody>
{% for item in dirlist %}
<tr>
<td class="modes">{{ item[0] }}</a></td>
<td class="name"><a href="{{item[5]}}">{{ item[1] }}</a></td>
<td class="modified">{{ item[2] }}</td>
<td class="size" style="text-align: right;">{{ item[3] }}</td>
<td class="type">{{ item[4] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="foot">
hosted by eightnoteight using lighttp
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment