Created
November 12, 2012 23:11
-
-
Save omaraboumrad/4062691 to your computer and use it in GitHub Desktop.
lists files based on requested path (wsgiref playground)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from wsgiref import simple_server | |
def format_html(entries): | |
html = """<html><body><ul>%s</ul></body></html""" | |
return html % entries | |
def format_entries(requested_dir, children): | |
result = [] | |
a = '<li><a href="%s">%s</a></li>' | |
non_a = '<li>%s</li>' | |
for child in children: | |
file_path = os.path.join(requested_dir, child) | |
if os.path.isdir(file_path): | |
result.append(a % (file_path, child)) | |
else: | |
result.append(non_a % child ) | |
formatted = ''.join(result) | |
return format_html(formatted) | |
def req(env, res): | |
result = None | |
requested_dir = env['PATH_INFO'] | |
status = '200 OK' | |
try: | |
children = os.listdir(requested_dir) | |
result = format_entries(requested_dir, children) | |
except: | |
result = 'Error!' | |
status = '404 NotFound' | |
headers = [('Content-Type', 'text/html')] | |
res(status, headers) | |
return [str(result)] | |
httpd = simple_server.make_server('', 8000, req) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment