如何用easywebdav通过python连接owncloud?

5 投票
3 回答
6701 浏览
提问于 2025-04-17 23:16

我正在尝试用Python连接到一个owncloud实例。 我发现了easywebdav这个工具,它应该可以让我通过webdav轻松连接,但在尝试连接时,我收到了“404 未找到”的错误。

import easywebdav
webdav = easywebdav.connect('test.org/owncloud/remote.php/webdav/', username='user', password='pass', protocol='https', port=443, verify_ssl=False)
print webdav.ls(".")

我本来期待能看到我owncloud实例中的文件列表,但实际上我得到了

python ./test.py 
Traceback (most recent call last):
File "./test.py", line 8, in <module>
    print webdav.ls(".")
File "/usr/lib/python2.7/site-packages/easywebdav-1.0.7-py2.7.egg/easywebdav/client.py", line 131, in ls
    response = self._send('PROPFIND', remote_path, (207, 301), headers=headers)
File "/usr/lib/python2.7/site-packages/easywebdav-1.0.7-py2.7.egg/easywebdav/client.py", line 81, in _send
    raise OperationFailed(method, path, expected_code, response.status_code)
easywebdav.client.OperationFailed: Failed to list directory ".".
Operation     :  PROPFIND .
Expected code :  207 UNKNOWN, 301 Moved Permanently
Actual code   :  404 Not Found

我觉得奇怪的是,如果我连接到一个无效的路径,使用

webdav = easywebdav.connect('test.org/owncloud-not-existent/', ......)

我得到的结果是

Traceback (most recent call last):
File "./test.py", line 8, in <module>
    print webdav.ls(".")
File "/usr/lib/python2.7/site-packages/easywebdav-1.0.7-py2.7.egg/easywebdav/client.py", line 131, in ls
    response = self._send('PROPFIND', remote_path, (207, 301), headers=headers)
File "/usr/lib/python2.7/site-packages/easywebdav-1.0.7-py2.7.egg/easywebdav/client.py", line 81, in _send
    raise OperationFailed(method, path, expected_code, response.status_code)
easywebdav.client.OperationFailed: Failed to list directory ".".
Operation     :  PROPFIND .
Expected code :  207 UNKNOWN, 301 Moved Permanently
Actual code   :  405 Method Not Allowed

3 个回答

-1
connection = easywebdav.connect( 'something.tld',
                    username   = 'your_username',
                    password   = 'your_password',
                    protocol   = 'https',
                    path       = 'owncloud/remote.php/webdav',
                    verify_ssl = False) #not commended
connection.cd("my_folder")
connection.ls("") # Seafile's seafdav will return a 404 if you use the "." or ".." directory

当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。

1

虽然之前的答案解决了这个问题,但每次都要在命令中输入路径其实挺麻烦的。

深入了解后发现,owncloud 不支持以 '.' 结尾的路径。easywebdav 的默认路径是 '.',所以像 ls() 这样的命令本来应该打印当前目录,但实际上却出现了错误。

不过,如果用完整的路径来调用 ls 命令,就可以正常工作,这也是为什么上面的建议能奏效。

对于最初的问题,有一个更简单的解决方案:

import easywebdav
webdav = easywebdav.connect('test.org/owncloud/remote.php/webdav/', username='user', password='pass', protocol='https', port=443, verify_ssl=False)
print webdav.ls("/")
3

我在自己的WebDav服务器上测试过,发现了类似的问题。虽然我觉得我用的easywebdav版本可能不同,我用的是v1.0.7,而且参数verify_ssl是不允许的,所以我测试的时候用了“http”。

无论如何,我成功重现了你的问题。要解决这个问题,改变连接的地址,只用主机名,把路径放在ls()命令里:

import easywebdav
webdav = easywebdav.connect('test.org', username='user', password='pass', protocol='https', port=443, verify_ssl=False)
print webdav.ls("/owncloud/remote.php/webdav")

撰写回答