Skip to main content
 首页 » 编程设计

python之有比这更好的简单 Python WebDAV 服务器代码片段吗

2025年05月04日44zhwl

有没有人有更好的简单 Python 代码片段 WebDAV服务器?下面的代码(从一些 Google 搜索结果拼凑而成)似乎在 Python 2.6 下工作,但我想知道是否有人有他们以前使用过的东西,一点经过更多测试和完成。我更喜欢 stdlib-only 片段而不是第三方包。它用于某些测试代码,因此不必具有生产值(value)。

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 的重构版本。