简体   繁体   English

使用python从owncloud下载受密码保护的文件

[英]download password protected file from owncloud with python

How can I download a password protected file inside python ?如何在python中下载受密码保护的文件?

The file is shared via Owncloud and the access is protected with a password by Owncloud .该文件通过Owncloud共享,访问受Owncloud密码保护。

I know it works with curl by using:我知道它可以通过以下方式与curl一起使用:

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.字段文件 id FileId是从共享链接中提取的。

There are web pages which can convert curl command to many different languages and modules - even to Python and requests - ie.有些网页可以将curl命令转换为许多不同的语言和模块 - 甚至是Pythonrequests - 即。 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 )

You can nest the command into a system call with os module您可以使用 os 模块将命令嵌套到系统调用中


system_object = os.system('your command')

or fork a new process and use the subprocess run或 fork 一个新进程并使用子进程运行


myProcess = subprocess.run()

requests module allows you to use http commands requests 模块允许您使用 http 命令


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重要的部分是您将对象变量分配给实例方法,以便您可以使用文件对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM