WebDAV4 Client Library

0.11.0 · active · verified Thu Apr 16

webdav4 is a modern Python library for interacting with WebDAV servers, providing both a low-level client (`WebdavClient`) and an `fsspec`-compatible filesystem (`WebdavFS`). It is currently at version 0.11.0 and is actively maintained, with regular releases addressing features and bug fixes. It aims to offer a robust and user-friendly way to manage files on WebDAV resources.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `WebdavFS`, list directory contents, create a new directory, and write/read a file. Remember to replace placeholder credentials with your actual WebDAV server details or set them as environment variables. SSL verification might need to be disabled for servers with self-signed certificates.

import os
from webdav4.fs import WebdavFS

# Replace with your WebDAV server details or set as environment variables
WEBDAV_URL = os.environ.get('WEBDAV_URL', 'http://localhost:8000/webdav/')
WEBDAV_USER = os.environ.get('WEBDAV_USER', 'user')
WEBDAV_PASS = os.environ.get('WEBDAV_PASS', 'password')

if not all([WEBDAV_URL, WEBDAV_USER, WEBDAV_PASS]):
    print("Please set WEBDAV_URL, WEBDAV_USER, WEBDAV_PASS environment variables or update the script.")
    exit(1)

try:
    # Initialize WebdavFS
    # For self-signed certificates or HTTP, you might need to add verify=False
    fs = WebdavFS(
        url=WEBDAV_URL,
        username=WEBDAV_USER,
        password=WEBDAV_PASS,
        # verify=False # Uncomment if you have SSL certificate issues
    )

    print(f"Connecting to WebDAV server: {WEBDAV_URL}")

    # List contents of the root directory
    print(f"Listing contents of '/':")
    for item in fs.ls('/', detail=True):
        print(f"  - {item['name']} (Type: {item['type']}, Size: {item.get('size', 'N/A')})")

    # Example: Create a directory
    test_dir_name = 'my_test_dir_webdav4'
    if not fs.exists(test_dir_name):
        fs.mkdir(test_dir_name)
        print(f"Successfully created directory: {test_dir_name}")
    else:
        print(f"Directory '{test_dir_name}' already exists.")

    # Example: Write and read a file
    test_file_path = f'{test_dir_name}/hello.txt'
    file_content = 'Hello, WebDAV4!'

    with fs.open(test_file_path, 'wb') as f:
        f.write(file_content.encode('utf-8'))
    print(f"Wrote content to {test_file_path}")

    with fs.open(test_file_path, 'rb') as f:
        read_content = f.read().decode('utf-8')
    print(f"Read content from {test_file_path}: '{read_content}'")

    # Clean up (optional)
    # fs.rm(test_file_path)
    # print(f"Removed file: {test_file_path}")
    # fs.rmdir(test_dir_name)
    # print(f"Removed directory: {test_dir_name}")

except Exception as e:
    print(f"\nAn error occurred: {e}")
    print("Please ensure the WebDAV server is running and accessible, ")
    print("and that WEBDAV_URL, WEBDAV_USER, WEBDAV_PASS environment variables are set correctly.")
    print("If you are facing SSL issues, try adding `verify=False` to the WebdavFS constructor.")

view raw JSON →