如何使用easywebdav的python连接到owncloud?

2024-06-17 11:08:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用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(".")

我希望在我自己的云实例上找到一个文件列表,但是我得到了

^{pr2}$

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

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

Tags: 实例inpyorgtestclientremoteconnect
3条回答
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

虽然前面的答案中的解决方案确实可以解决这个问题,但是在每个命令上都必须传递路径是不太方便的。在

更深入地说,owncloud似乎根本不支持末尾带有“.”的路径。 easywebdav实际上有一个默认路径“.”,用于像ls()这样的命令网络达夫.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("/")

我用一个个人WebDav服务器进行了测试,发现了一个类似的问题,尽管我认为我的easywebdav版本不同,但我使用v1.0.7,并且不允许使用参数verify_ssl,所以我用“http”进行了测试。在

无论如何,我要重现您的问题,要解决它,请更改连接url并仅使用主机,将路径放入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")

相关问题 更多 >