从“zope.app.file”模块为文件和图像对象定义WebDAV数据模型。

z3c.davapp.zopeappfile的Python项目详细描述


zope.app.file webdav集成

>>> import z3c.etree
>>> from zope.app.file.file import File
>>> etree = z3c.etree.getEngine()

在根文件夹中创建名为textfile.txt的内容对象。

>>> f = File('%s\n' %('x' * 10) * 5, 'text/plain')
>>> getRootFolder()['testfile.txt'] = f

获取

需要为webdav设置内容对象的默认视图 客户端能够获取符合DAV的资源的数据。

>>> resp = http("""
... GET /testfile.txt HTTP/1.1
... """, handle_errors = False)
>>> resp.getStatus()
200
>>> resp.getHeader('content-type')
'text/plain'
>>> resp.getHeader('content-length')
'55'
>>> print resp.getBody()
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx

放置

是吗???

预查找

查询文件对象的WebDAV数据模型以下属性 当前正在为zope.file构建数据模型:

  • {dav:}创建日期
  • {DAV:}显示名称
  • {DAV:}获取内容长度
  • {dav:}获取内容类型
  • {dav:}getlastmodified
  • {dav:}资源类型

查询测试文件上的所有属性。

>>> resp = webdav("""
... PROPFIND /testfile.txt HTTP/1.1
... """, handle_errors = False)
>>> print resp.getBody() #doctest:+XMLDATA
<multistatus xmlns="DAV:">
  <response>
    <href>http://localhost/testfile.txt</href>
    <propstat>
      <prop>
        <creationdate />
        <displayname />
        <getlastmodified />
        <getcontenttype>text/plain</getcontenttype>
        <getcontentlength>55</getcontentlength>
        <resourcetype />
      </prop>
      <status>HTTP/1.1 200 Ok</status>
    </propstat>
  </response>
</multistatus>

未定义{dav:}getetag{dav:}getcontentlanguage属性 (然而)在文件对象上。

>>> resp.getMSProperty('http://localhost/testfile.txt', '{DAV:}getetag')
Traceback (most recent call last):
...
KeyError: "'{DAV:}getetag' property not found for resource http://localhost/testfile.txt (200)"
>>> resp.getMSProperty(
...    'http://localhost/testfile.txt', '{DAV:}getcontentlanguage')
Traceback (most recent call last):
...
KeyError: "'{DAV:}getcontentlanguage' property not found for resource http://localhost/testfile.txt (200)"

proppatch

我们需要登录才能更新属性。

>>> reqbody = """<propertyupdate xmlns="DAV:">
...  <set>
...   <prop>
...    <displayname>Test title</displayname>
...   </prop>
...  </set>
... </propertyupdate>"""
>>> resp = webdav("""
... PROPPATCH /testfile.txt HTTP/1.1
... Content-Type: application/xml
... Content-Length: %d
...
... %s""" %(len(reqbody), reqbody))
>>> resp.getStatus()
401

现在更新文件的标题。

>>> resp = webdav("""
... PROPPATCH /testfile.txt HTTP/1.1
... Authorization: Basic mgr:mgrpw
... Content-Type: application/xml
... Content-Length: %d
...
... %s""" %(len(reqbody), reqbody), handle_errors = False)
>>> resp.getStatus()
207
>>> resp.getHeader('content-type')
'application/xml'
>>> print resp.getBody() #doctest:+XMLDATA
<multistatus xmlns="DAV:">
  <response>
    <href>http://localhost/testfile.txt</href>
    <propstat>
      <prop>
        <displayname />
      </prop>
      <status>HTTP/1.1 200 Ok</status>
    </propstat>
  </response>
</multistatus>

使用propfind检索displayname属性并注意差异。

>>> reqbody = """<propfind xmlns="DAV:">
...   <prop>
...     <displayname />
...   </prop>
... </propfind>
... """
>>> resp = webdav("""
... PROPFIND /testfile.txt HTTP/1.1
... Content-type: application/xml
... Content-Length: %d
...
... %s""" %(len(reqbody), reqbody), handle_errors = False)
>>> resp.getStatus()
207
>>> resp.getHeader('content-type')
'application/xml'
>>> print resp.getBody() #doctest:+XMLDATA
<multistatus xmlns="DAV:">
  <response>
    <href>http://localhost/testfile.txt</href>
    <propstat>
      <prop>
        <displayname>Test title</displayname>
      </prop>
      <status>HTTP/1.1 200 Ok</status>
    </propstat>
  </response>
</multistatus>

{dav:}getcontentlength属性是一个活动属性,因此我们 无法修改。

>>> reqbody = """<propertyupdate xmlns="DAV:">
...   <set>
...     <prop>
...       <getcontentlength>24</getcontentlength>
...     </prop>
...   </set>
... </propertyupdate>"""
>>> resp = webdav("""
... PROPPATCH /testfile.txt HTTP/1.1
... Authorization: Basic mgr:mgrpw
... Content-type: application/xml
... Content-length: %d
...
... %s""" %(len(reqbody), reqbody))
>>> resp.getStatus()
207
>>> resp.getHeader('content-type')
'application/xml'
>>> print resp.getBody() #doctest:+XMLDATA
<multistatus xmlns="DAV:">
  <response>
    <href>http://localhost/testfile.txt</href>
    <propstat>
      <prop>
        <getcontentlength />
      </prop>
      <status>HTTP/1.1 403 Forbidden</status>
    </propstat>
  </response>
</multistatus>

不透明特性

在我们的资源上设置两个死属性。

>>> reqbody = """<propertyupdate xmlns="DAV:">
...  <set>
...    <prop>
...      <E:prop1 xmlns:E="examplens:">PROPERTY 1</E:prop1>
...      <E:prop2 xmlns:E="examplens:">PROPERTY 2</E:prop2>
...    </prop>
...  </set>
... </propertyupdate>
... """
>>> resp = webdav("""
... PROPPATCH /testfile.txt HTTP/1.1
... Authorization: Basic mgr:mgrpw
... Content-type: application/xml
... Content-length: %d
...
... %s""" %(len(reqbody), reqbody), handle_errors = True)
>>> resp.getStatus()
207
>>> resp.getHeader('content-type')
'application/xml'
>>> print resp.getBody() #doctest:+XMLDATA
<multistatus xmlns="DAV:">
  <response>
    <href>http://localhost/testfile.txt</href>
    <propstat>
      <prop>
        <ns1:prop1 xmlns:ns1="examplens:" />
        <ns1:prop2 xmlns:ns1="examplens:" />
      </prop>
      <status>HTTP/1.1 200 Ok</status>
    </propstat>
  </response>
</multistatus>

查询这些新属性。

>>> reqbody = """<propfind xmlns="DAV:">
...  <prop xmlns:E="examplens:" >
...    <E:prop1 />
...    <E:prop2 />
...  </prop>
... </propfind>
... """
>>> resp = webdav("""
... PROPFIND /testfile.txt HTTP/1.1
... Content-type: application/xml
... Content-length: %d
...
... %s""" %(len(reqbody), reqbody))
>>> resp.getStatus()
207
>>> resp.getHeader('content-type')
'application/xml'
>>> print resp.getBody() #doctest:+XMLDATA
<multistatus xmlns="DAV:">
  <response>
    <href>http://localhost/testfile.txt</href>
    <propstat>
      <prop>
        <E:prop1 xmlns:E="examplens:">PROPERTY 1</E:prop1>
        <E:prop2 xmlns:E="examplens:">PROPERTY 2</E:prop2>
      </prop>
      <status>HTTP/1.1 200 Ok</status>
    </propstat>
  </response>
</multistatus>

更新第一个属性并删除第二个属性

>>> reqbody = """<propertyupdate xmlns="DAV:">
...  <set>
...    <prop>
...      <E:prop1 xmlns:E="examplens:">Only opaque property</E:prop1>
...    </prop>
...  </set>
...  <remove>
...    <prop>
...      <E:prop2 xmlns:E="examplens:" />
...    </prop>
...  </remove>
... </propertyupdate>"""
>>> resp = webdav("""
... PROPPATCH /testfile.txt HTTP/1.1
... Authorization: Basic mgr:mgrpw
... Content-type: application/xml
... Content-length: %d
...
... %s""" %(len(reqbody), reqbody))
>>> resp.getStatus()
207
>>> resp.getHeader('content-type')
'application/xml'
>>> print resp.getBody() #doctest:+XMLDATA
<multistatus xmlns="DAV:">
  <response>
    <href>http://localhost/testfile.txt</href>
    <propstat>
      <prop>
        <E:prop1 xmlns:E="examplens:" />
        <E:prop2 xmlns:E="examplens:" />
      </prop>
      <status>HTTP/1.1 200 Ok</status>
    </propstat>
  </response>
</multistatus>

现在检查不透明属性是否已成功更新

>>> reqbody = """<propfind xmlns="DAV:">
...  <prop xmlns:E="examplens:" >
...    <E:prop1 />
...    <E:prop2 />
...  </prop>
... </propfind>
... """
>>> resp = webdav("""
... PROPFIND /testfile.txt HTTP/1.1
... Content-type: application/xml
... Content-length: %d
...
... %s""" %(len(reqbody), reqbody))
>>> resp.getStatus()
207
>>> resp.getHeader('content-type')
'application/xml'
>>> print resp.getBody() #doctest:+XMLDATA
<multistatus xmlns="DAV:">
  <response>
    <href>http://localhost/testfile.txt</href>
    <propstat>
      <prop>
        <ns1:prop1 xmlns:ns1="examplens:">Only opaque property</ns1:prop1>
      </prop>
      <status>HTTP/1.1 200 Ok</status>
    </propstat>
    <propstat>
      <prop>
        <ns1:prop2 xmlns:ns1="examplens:" />
      </prop>
      <status>HTTP/1.1 404 Not Found</status>
    </propstat>
  </response>
</multistatus>

z3c.davapp.zopeapp文件中的更改

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java通过调试找到日志文件路径?   如何修复这个java。使用selenium的lang.nullpointerException   java无法在规范上下文路径和用户代理提供的URI之间找到匹配项   java是单线程的spring集成通道吗?   java垃圾收集年轻一代扫描   java如何触发关闭Guava AbstractScheduledService?   TagLib端口或类似的Java音频标签读取器   查找最高考试范围的java问题   安卓如何在Kotlin中重写此方法(在Java中工作,但在Kotlin中不工作)   JPA/EclipseLink中的java锁定(Postgres 9.1)   java写入jar中的文件   java根据字符串的字节长度修剪字符串   如何在Java中将十进制时间戳转换为带尾随小数的日期   java查询超过Gerrit REST API中的500限制   使用基本顶点缓冲区和高度数组的opengl Java LWJGL VBO   java为什么会引发此异常以及如何从中恢复?   EclipseMars在Java8更新71之后不会启动   java将变量从表示层传递到逻辑层   使用PHP作为客户端向Java Web服务传递并使用jsessionid