Skip to content

Instantly share code, notes, and snippets.

@vampjaz
Last active December 28, 2015 16:50
Show Gist options
  • Save vampjaz/b737d2828cf563458a5b to your computer and use it in GitHub Desktop.
Save vampjaz/b737d2828cf563458a5b to your computer and use it in GitHub Desktop.
Server for easy browsing of textfiles in directories: http://imgur.com/a/CY4G3
### simple server (i hope) for textfiles data
### trying to make it all one file for portability
### by nWx aka red_green
### gplv2 i guess
# throw all the files in the same folder as this script and then start it up
# then go to localhost:5000
####################################
# IMPORTANT: NOT FOR A PUBLIC SERVER, DIRECTORY TRAVERSAL IS NOT SANITIZED AND YOUR DATA CAN BE STOLEN
####################################
# if you haven't read the allcaps comment above, READ IT
from flask import Flask, request
import os,sys,cgi
from urllib import quote_plus
TABLEWIDTH = 10
## i want to use render_template but it's too much work with all the extra files. i'll just define it here:
basepage_html = """
<html>
<head>
<title>Textfiles Browser: {title}</title>
<style type='text/css'>
body {{
color: #00ff00;
background-color: #000000;
padding-left: 40px;
padding-right: 40px;
}}
a {{
color: inherit;
/* text-decoration: inherit; */
}}
pre {{
word-wrap: normal;
/* width: 900px; */
white-space: pre-wrap;
}}
</style>
</head>
<body>
<h1>Python TextFiles Browser v0.1</h1>
<h1>{title}</h1>
<h3><a href='/'>Go home</a></h3>
{page}
</body>
</html>
"""
baselisting_html = """
<center>
{nav}
<br/>
</center>
<table>
{data}
</table>
<center>
{nav}
</center>
"""
baselisting_item = """
<td>
<a href='/?f={uri}'>{fn}</a>
</td>
"""
basefile_html = """
<center>
{nav}
<br/>
<b>{name}</b>
<br/>
</center>
<div>
<pre>
{file}
</pre>
</div>
<center>
{nav}
</center>
"""
baseerror_html = """
there was a error, bro:
<br/>
{err}
"""
def gen_listing(fdir):
title = fdir
#items = [str(i) for i in os.listdir(fdir)]
nav = ''
try:
fn = fdir.split('/')[-1]
fd = '/'.join(fdir.split('/')[:-1])
ui = [str(i) for i in os.listdir(fd) if os.path.isdir(os.path.join(fd,i))] ## to get next and preivous
ui = sorted(ui,key=str.lower)
try:
p = ui.index(fn)
f = ui[p-1]
nav += "<a href='/?f={}'>{}</a>".format(quote_plus(os.path.join(fd,f)),f)
except:
pass
nav += " ~~~~~~~~~~~~ <b><a href='/?f={}'>Go up</a></b> ~~~~~~~~~~~~ ".format(quote_plus(fd))
try:
p = ui.index(fn)
f = ui[p+1]
nav += "<a href='/?f={}'>{}</a>".format(quote_plus(os.path.join(fd,f)),f)
except:
pass
except:
pass
files = [str(i) for i in os.listdir(fdir) if os.path.isfile(os.path.join(fdir,i))]
dirs = [str(i) for i in os.listdir(fdir) if os.path.isdir(os.path.join(fdir,i))]
items = sorted(dirs, key=str.lower) + sorted(files,key=str.lower)
page = ''
while items:
it = items[:TABLEWIDTH]
page += '<tr>'
for i in it:
fn = i.split('/')[-1]
page += baselisting_item.format(fn=fn,uri=quote_plus(os.path.join(fdir,i)))
page += '</tr>'
try:
items = items[TABLEWIDTH:]
except:
items = []
return basepage_html.format(title=title,page=baselisting_html.format(data=page,nav=nav))
def show_file(fdir):
fn = fdir.split('/')[-1]
fd = '/'.join(fdir.split('/')[:-1])
items = [str(i) for i in os.listdir(fd) if os.path.isfile(os.path.join(fd,i))] ## to get next and preivous
items = sorted(items,key=str.lower)
nav = ''
try:
p = items.index(fn)
f = items[p-1]
nav += "<a href='/?f={}'>{}</a>".format(quote_plus(os.path.join(fd,f)),f)
except:
pass
nav += " ~~~~~~~~~~~~ <b><a href='/?f={}'>Go up</a></b> ~~~~~~~~~~~~ ".format(quote_plus(fd))
try:
p = items.index(fn)
f = items[p+1]
nav += "<a href='/?f={}'>{}</a>".format(quote_plus(os.path.join(fd,f)),f)
except:
pass
dat = cgi.escape(open(fdir).read())
return basepage_html.format(title=fn,page=basefile_html.format(nav=nav,file=dat,name=fn))
app = Flask(__name__)
@app.route('/')
def handle(): ## all requests here bc of limitations in flask framework
uri = request.args.get('f',None)
if not uri:
uri = '.'
print uri
if os.path.exists(uri):
if os.path.isdir(uri):
return gen_listing(uri)
elif os.path.isfile(uri):
return show_file(uri)
else:
return baseerror_html.format(err="apparently that isn't a file <em>or</em> a directory")
else:
return baseerror_html.format(err="file doesn't exist")
if __name__ == "__main__":
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment