WebDAV Client 3

3.14.7 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

Initializes a WebDAV client, lists root directory contents, and demonstrates creating a new directory. Credentials should be provided via environment variables for security, or directly in the `options` dictionary.

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}")

view raw JSON →