Skip to content

Instantly share code, notes, and snippets.

@shsdev
Created October 21, 2024 10:16
Show Gist options
  • Save shsdev/ec1457898cc849d423a3a94246640960 to your computer and use it in GitHub Desktop.
Save shsdev/ec1457898cc849d423a3a94246640960 to your computer and use it in GitHub Desktop.
Webdav client for Nextcloud
"""
This Python module sets up a WebDAV client using the 'webdavclient3' library.
Follow these steps to set up the environment and run this module:
1. Create a new directory for the project:
$ mkdir wdclient
2. Navigate into the directory:
$ cd wdclient/
3. Create a Python virtual environment to isolate dependencies:
$ python3 -m venv venv
4. Activate the virtual environment:
$ source venv/bin/activate
5. Install the required 'webdavclient3' package:
$ pip install webdavclient3
6. Replace NEXTCLOUD_INSTANCE with the instance's host
7. Replace YOUR_EARK_NEXTCLOUD_USER in webdav_hostname and webdav_login and YOUR_EARK_NEXTCLOUD_PASSWORD in webdav_password with your credentials.
8. Adapt paths to local and Nextcloud directories for upload and download tests
9. Run the Python module:
$ python3 wdclient.py
"""
from webdav3.client import Client
# Define connection parameters
options = {
'webdav_hostname': "https://NEXTCLOUD_INSTANCE/remote.php/dav/files/YOUR_EARK_NEXTCLOUD_USER/",
'webdav_login': "YOUR_EARK_NEXTCLOUD_USER",
'webdav_password': "YOUR_EARK_NEXTCLOUD_PASSWORD"
}
client = Client(options)
# Upload a file
client.upload(remote_path='/Private/file.txt', local_path='/YOUR_HOME_FOLDER/YOUR_USER/file.txt')
# Download a file
client.download(remote_path='/Private/somefile.docx', local_path='/YOUR_HOME_FOLDER/YOUR_USER/somefile.docx')
# List directory contents
files = client.list('/Private/')
print(files)
# Delete a file (commented out)
#client.clean('/Private/file.txt')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment