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()