Requests is convenient, isn't it? I think it's the most used python module. I wish it was installed as standard instead of urllib2.
Now, in order to get the WebDAV file list, you have to throw a method called PROPFIND, but unfortunately requests does not support this.
requests.propfind('https://example.com')
If you want to do something like that, it will brilliantly return ʻAttribute Error: module'requests' has no attribute'propfind'`.
Instead, let's use the Requests low-level APIs Request and Session to throw the PROPFIND method.
import requests
def propfind(url):
    
    req = requests.Request('PROPFIND', url, headers={'Depth': '1'})
    prepped = req.prepare()
    
    s = requests.Session()
    resp = s.send(prepped)
    return resp.text
The reason why headers = {'Depth': '1'} is that if you get the entire list in WebDAV, it will put a load on the server side, so only the list directly under the specified URL is fetched.
The response is XML, so let's follow it with ʻElementTree. Just get the {DAV:} hrefattribute.  Let's pass the XML obtained by thepropfind (url) created earlier to the next xml_to_list (xml) `.
import xml.etree.ElementTree as ET
def xml_to_list(xml):
    root = ET.fromstring(xml)
    
    files = [ f.text for f in root.findall('.//{DAV:}href') ]
    
    if (files):
        return files
    else:
        None
The notation in root.findall () is XPath.
This will return the WebDAV file list as a list.
Advanced Usage (Requests official)
Recommended Posts