tags:

views:

602

answers:

3

Does anyone have a better code snippet for a trivial Python WebDAV server? The code below (which is cobbled together from some Google search results) appears to work under Python 2.6, but I wonder if someone has something they have used before, a little more tested and complete. I'd prefer a stdlib-only snippet over a third-party package. It is for some test code to hit so does not have to be production-worthy.

import httplib
import BaseHTTPServer

class WebDAV(BaseHTTPServer.BaseHTTPRequestHandler):
    """
    Ultra-simplistic WebDAV server.
    """
    def do_PUT(self):
        path = os.path.normpath(self.path)
        if os.path.isabs(path):
            path = path[1:]    # safe assumption due to normpath above
        directory = os.path.dirname(path)
        if not os.path.isdir(directory):
            os.makedirs(directory)
        content_length = int(self.headers['Content-Length'])
        with open(path, "w") as f:
            f.write(self.rfile.read(content_length))

        self.send_response(httplib.OK)

def server_main(server_class=BaseHTTPServer.HTTPServer, 
                handler_class=WebDAV):
    server_class(('', 9231), handler_class).serve_forever()
A: 

You can try akaDAV. It is a WebDAV module for Twisted that you can get at http://akadav.sourceforge.net/.

I think it is not maintained anymore but I've got it to work and it supports most operations (except locks).

math
A: 

Or try PyFileServer, which I picked up for further develpment by the name WsgiDAV (http://code.google.com/p/wsgidav/)

mar10
A: 

WsgiDAV

Jesse Dhillon