WebDAV Client 3
WebDAV Client 3 is a Python library designed for interacting with WebDAV servers. It provides an easy-to-use interface for common WebDAV operations such as listing, uploading, downloading, copying, moving, and deleting files and directories. This package is a continuation of the original `designerror/webdav-client-python` but leverages the popular `requests` library for HTTP communication instead of `PyCURL`. It is actively maintained, with the current version being `3.14.7`.
Warnings
- gotcha In versions prior to `3.14.7`, inconsistent path quoting for `webdav.root` and URN handling could lead to `RemoteResourceNotFound` errors or incorrect file operations. Ensure your `webdav_hostname` is consistently formatted, especially regarding trailing slashes.
- gotcha Older versions of `webdavclient3` could raise a `KeyError` if the HTTP response from the WebDAV server was missing the `Content-Length` header, which might occur with certain server implementations or empty files.
- gotcha Frequent `RemoteResourceNotFound` errors can occur, particularly with services like Nextcloud or when the `webdav_hostname` is not precisely configured. Sometimes the base URL needs to include specific paths (e.g., `remote.php/dav/files/<username>`), or API calls like `client.list()` might require the username in the path (e.g., `client.list('/username/')`). Disabling checks (`client.webdav.disable_check = True`) might work as a workaround but should be used cautiously.
- gotcha If your WebDAV server requires HTTP Digest Authentication, using `webdavclient3` with default options (which rely on `requests`'s basic authentication) may result in `401 Unauthorized` errors. `requests` does not handle Digest Auth automatically without explicit configuration.
- gotcha Setting `client.verify = False` disables SSL certificate verification. While useful for testing with self-signed certificates, this can expose your application to man-in-the-middle attacks in production environments.
Install
-
pip install webdavclient3
Imports
- Client
from webdav3.client import Client
- WebDavException
from webdav3.client import WebDavException
Quickstart
import os
from webdav3.client import Client
# Configure these environment variables or replace with actual values
WEBDAV_HOSTNAME = os.environ.get('WEBDAV_HOSTNAME', 'https://webdav.example.com')
WEBDAV_LOGIN = os.environ.get('WEBDAV_LOGIN', 'your_username')
WEBDAV_PASSWORD = os.environ.get('WEBDAV_PASSWORD', 'your_password')
options = {
'webdav_hostname': WEBDAV_HOSTNAME,
'webdav_login': WEBDAV_LOGIN,
'webdav_password': WEBDAV_PASSWORD
}
try:
client = Client(options)
# Optional: To skip SSL certificate verification (USE WITH CAUTION IN PROD)
# client.verify = False
# Example 1: List contents of the root directory
print(f"Listing contents of {WEBDAV_HOSTNAME}:")
items = client.list()
if items:
for item in items:
print(f"- {item.name} (type: {item.type}, size: {item.size})")
else:
print("No items found or directory is empty.")
# Example 2: Create a directory
new_dir = 'test_directory_from_python'
if not client.check(new_dir):
client.mkdir(new_dir)
print(f"Directory '{new_dir}' created.")
else:
print(f"Directory '{new_dir}' already exists.")
# Example 3: Upload a file (assuming 'local_file.txt' exists)
# with open('local_file.txt', 'rb') as f:
# client.upload_file('remote_file.txt', f)
# print("File uploaded.")
except Exception as e:
print(f"An error occurred: {e}")