Home > Mobile >  download password protected file from owncloud with python
download password protected file from owncloud with python

Time:05-24

How can I download a password protected file inside python?

The file is shared via Owncloud and the access is protected with a password by Owncloud.

I know it works with curl by using:

curl -u "FileId:FilePw" -H 'X-Requested-With: XMLHttpRequest' "https://exampledomain.com/public.php/webdav/" >output_file

The field file id FileId is extracted from the shared link.

CodePudding user response:

There are web pages which can convert curl command to many different languages and modules - even to Python and requests - ie. Curl Converter

import requests

headers = {
    'X-Requested-With': 'XMLHttpRequest',
}

response = requests.get('https://exampledomain.com/public.php/webdav/', 
                        headers=headers, 
                        auth=('FileId', 'FilePw'))

And this needs only to save response in binary mode

with open('filename.ext', 'wb') as fh:
   fh.write( response.content )

CodePudding user response:

You can nest the command into a system call with os module


system_object = os.system('your command')

or fork a new process and use the subprocess run


myProcess = subprocess.run()

requests module allows you to use http commands


import requests
headers = {}
response = requests.method(params)

the important part is that you assign an object variable to the instance method so that you can work with the file object

  • Related