# Written by retoor@molodetz.nl
# This script interacts with a WebDAV server to list directories and files, and download files, maintaining statistics on size, number of files, and directories.
# External import: aiohttp for asynchronous HTTP client
# MIT License
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import asyncio
import xml.etree.ElementTree as ET
import aiohttp
WEBDAV_URL = "http://localhost:8095"
USERNAME = "retoor"
PASSWORD = "retoor"
HEADERS = {"Depth": "1", "Content-Type": "application/xml"}
sem = asyncio.Semaphore(10)
total_size = 0
total_files = 0
total_dirs = 0
async def list_webdav_directory(session, path="/"):
url = WEBDAV_URL.rstrip("/") + path
async with (
sem,
session.request(
"PROPFIND", url, headers=HEADERS, auth=aiohttp.BasicAuth(USERNAME, PASSWORD)
) as response,
):
if response.status not in [200, 207]:
print(f"Error {response.status}: Failed to list {url}")
return []
content = await response.text()
root = ET.fromstring(content)
namespaces = {"D": "DAV:"}
entries = []
for resp in root.findall("D:response", namespaces):
href = resp.find("D:href", namespaces).text
resource_type = resp.find(".//D:resourcetype", namespaces)
is_directory = (
resource_type is not None
and resource_type.find("D:collection", namespaces) is not None
)
entries.append((href, is_directory))
return entries
async def read_webdav_file(session, file_path):
url = WEBDAV_URL.rstrip("/") + file_path
global total_size
global total_files
async with (
sem,
session.get(url, auth=aiohttp.BasicAuth(USERNAME, PASSWORD)) as response,
):
if response.status == 200:
content = await response.read()
total_size += len(content)
total_files += 1
else:
print(f"Error {response.status}: Cannot read {file_path}")
fetched = []
async def traverse_and_read(session, path="/"):
global total_dirs
entries = await list_webdav_directory(session, path)
total_dirs += 1
tasks = []
for entry, is_directory in entries:
if entry == "/":
continue
if entry in fetched:
continue
fetched.append(entry)
if is_directory:
tasks.append(traverse_and_read(session, entry))
else:
tasks.append(read_webdav_file(session, entry))
await asyncio.gather(*tasks)
async def main():
import time
start = time.time()
async with aiohttp.ClientSession() as session:
await traverse_and_read(session, "/")
duration = time.time() - start
factor = 1 / duration
mbps = total_size * factor / (1024 * 1024)
print(mbps, "mbps")
print(total_size / (1024 * 1024), "mb")
print(total_files, "files")
print(total_dirs, "dirs")
print(total_files + total_dirs, "nodes")
asyncio.run(main())