我试图用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(".")
我希望在我自己的云实例上找到的文件列表,但我得到
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
我已经测试了个人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")
虽然前面的答案中的解决方案确实解决了这个问题,但是必须在每个命令中传递路径不太方便。
深入挖掘,owncloud似乎根本不支持带有'。’在最后。Easywebdav实际上有一个默认路径'。'对于像ls()这样的命令,执行webdav.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("/")
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