#!/usr/bin/env python """ pwiki.py - A Very Spartan PUT Wiki Author: Sean B. Palmer, inamidst.com Save this file as index.cgi in some Apache-writable directory. You'll need something like the following: [[[ RewriteEngine on RewriteRule (.*) index.cgi [L] allow from all ]]] - .htaccess """ import cgitb; cgitb.enable() import sys, os def GET(path): if not path: print "Content-Type: text/html" print print '' elif os.path.exists(path + '.txt'): print "Content-Type: text/plain" print f = open(path + '.txt') for line in f: sys.stdout.write(line) f.close() else: print "Status: 404" print "Content-Type: text/plain" print print "Not Found! (The path <%s> was not found on this server.)" % path def PUT(path): input = sys.stdin.read() if len(input) > 100000: error(path, "Content body too long.") elif '.' in path: error(path, "I don't accept paths with '.' in them.") new = (not os.path.exists(path + '.txt')) f = open(path + '.txt', 'w') f.write(input) f.close() if new: print "Status: 201" print "Content-Type: text/plain" print print "Created the resource" else: print "Status: 200" print "Content-Type: text/plain" print print "Modified the resource" def error(path, msg=None): msg = msg or 'Error: only supports GET and PUT' print "Status: 500" print "Content-Type: text/html" print print "

%s

" % msg sys.exit(0) def main(): method = os.environ.get('REQUEST_METHOD') requesturi = os.environ.get('REQUEST_URI') path = requesturi.split('/').pop() {'GET': GET, 'PUT': PUT}.get(method, error)(path) if __name__=="__main__": main()