对于普通的Python WebDAV服务器,没有人有更好的代码段吗?下面的代码(从一些Google搜索结果中整理而来)似乎可以在python 2.6下工作,但是我想知道是否有人使用过某些东西,并且经过了更多测试和完善。与第三方软件包相比,我更喜欢仅使用stdlib的代码段。它是要击中某些测试代码的,因此不必具有生产价值。

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

最佳答案

或者尝试WsgiDAV,它是PyFileServer的重构版本。

10-07 19:17
查看更多