有没有比这个更好的简单Python WebDAV服务器代码片段?
有没有人能提供一个更好的简单Python WebDAV 服务器代码片段?下面的代码(是从一些谷歌搜索结果拼凑起来的)在Python 2.6下似乎能正常工作,但我在想有没有人之前用过的,经过稍微测试过的更完整的代码。我更倾向于只用标准库的代码,而不是第三方的包。这是为了测试代码,所以不需要达到生产环境的标准。
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()