#!/usr/bin/python """PUT Handler for Apache and Wikke. Sean B. Palmer and Adam Wendt, 2004-02. Example .htaccess: DirectoryIndex index.html ErrorDocument 404 /wikke/wikke.php Options -MultiViews RewriteEngine on RewriteBase /wikke RewriteRule ^([A-Za-z]+)$ put.py [L] When using this file, reset the global base variable. """ import sys, os, cgi, urllib desc = {404: 'Not Found', 401: 'Not authorized', 200: 'OK', 201: 'Created', 301: 'Permanent redirect', 302: 'Redirect', 500: 'Internal server error'} base = 'http://jane.no-ip.com/~sbp/wikke/' referer = os.environ.get('HTTP_REFERER') def serve(status, s): sys.stdout.write('Status: %s %s\n' % (status, desc[status])) if status == 302: r = env.get('REQUEST_URI'); i = r.rfind('/'); r = r[i+1:] sys.stdout.write('location: %s%s\n\n' % (base, r)) else: sys.stdout.write('Content-Type: text/html\n\n') sys.stdout.write(s) def main(env=None): if env is None: env = os.environ REQUEST_URI = env.get('REQUEST_URI') assert REQUEST_URI, 'no request URI' i = REQUEST_URI.rfind('/') assert i > 0, '/ not found in the request URI' fn = REQUEST_URI[i+1:] assert fn.isalpha(), 'Filename must be alphabetical %s' % fn if env.get('REQUEST_METHOD') == 'PUT': s = sys.stdin.read() s = s[:5000] # limit file sizes to 5000 bytes open(fn, 'w').write(s) serve(302, 'Done') elif os.path.exists(fn): serve(200, open(fn).read()) else: u = urllib.urlopen(base + 'wikke.php/' + fn) s = u.read() u.close() serve(200, s) if fn == 'env': print `os.environ` # serve(404, 'Not Found: %s' % fn) if __name__=='__main__': try: main() except: cgi.print_exception()