[PYTHON] I want to see a list of WebDAV files in the Requests module

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.

reference

Advanced Usage (Requests official)

Recommended Posts

I want to see a list of WebDAV files in the Requests module
I want to sort a list in the order of other lists
I want to set a life cycle in the task definition of ECS
How to get a list of files in the same directory with python
NikuGan ~ I want to see a lot of delicious meat! !!
I want to make the Dictionary type in the List unique
I want to print in a comprehension
I want to see the graph in 3D! I can make such a dream come true.
I want to know the population of each country in the world.
A story that didn't work when I tried to log in with the Python requests module
If you want a singleton in python, think of the module as a singleton
[Linux] Command to get a list of commands executed in the past
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
I want to color a part of an Excel string in Python
I want to leave an arbitrary command in the command history of Shell
I made a program to check the size of a file in Python
I tried to display the altitude value of DTM in a graph
Python: I want to measure the processing time of a function neatly
I made a function to see the movement of a two-dimensional array (Python)
I want to create a window in Python
Make a copy of the list in Python
I want to store DB information in list
I want to customize the appearance of zabbix
I want to display the progress in Python!
I want to use complicated four arithmetic operations in the IF statement of the Django template! → Use a custom template
How to pass the execution result of a shell command in a list in Python
The story of IPv6 address that I want to keep at a minimum
I want to use Python in the environment of pyenv + pipenv on Windows 10
I made an appdo command to execute a command in the context of the app
I want to add silence to the beginning of a wav file for 1 second
I want to store the result of% time, %% time, etc. in an object (variable)
I want to see the file name from DataLoader
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to transition with a button in flask
I want to grep the execution result of strace
I want to write in Python! (2) Let's write a test
Even in JavaScript, I want to see Python `range ()`!
[python] Get the list of classes defined in the module
I want to randomly sample a file in Python
I want to fully understand the basics of Bokeh
I want to work with a robot in python.
I want to install a package of Php Redis
[Python] I want to make a nested list a tuple
I want to write in Python! (3) Utilize the mock
I want to use the R dataset in python
I want to increase the security of ssh connections
I wanted to know the number of lines in multiple files, so I tried to get it with a command
How to identify the element with the smallest number of characters in a Python list?
How to list files under the specified directory in a list (multiple conditions / subdirectory search)
The story of Linux that I want to teach myself half a year ago
I want to take a screenshot of the site on Docker using any font
I want to use a network defined by myself in PPO2 of Stable Baselines
How to check in Python if one of the elements of a list is in another list
[Command] Command to get a list of files containing double-byte characters
I want to start a lot of processes from python
Get the number of specific elements in a python list
I want to use only the normalization process of SudachiPy
I want to get the operation information of yahoo route
I made a function to check the model of DCGAN