- import httplib, urllib2, base64, sys
- from lxml import etree
- PROPFIND = u'''<?xml version="1.0" encoding="utf-8"?>
- <propfind xmlns="DAV:">
- <prop>
- <getetag/>
- </prop>
- </propfind>'''
- SERVER_HOST = '127.0.0.1'
- SERVER_PORT = 8080
- SERVER_PATH = '/dav/Contacts'
- CLIENT_AGENT = 'Whitemice rules/so very true'
- auth_string = 'Basic {0}'.format(base64.encodestring('adam:fred123')[:-1])
- urllib2.install_opener(urllib2.build_opener(urllib2.HTTPHandler()))
- connection = httplib.HTTPConnection(SERVER_HOST, SERVER_PORT)
- connection.putrequest('PROPFIND', SERVER_PATH)
- connection.putheader('Authorization', auth_string)
- connection.putheader('User-Agent', CLIENT_AGENT)
- connection.putheader('Depth', 1)
- connection.putheader('Content-Length', str(len(PROPFIND)))
- connection.endheaders()
- connection.send(PROPFIND)
- response = connection.getresponse()
- if response.status == 207:
- data = response.read()
- connection.close()
- response = None
- namespace_prefix_map = { 'D' : 'DAV:' }
- document = etree.fromstring(data)
- for path in document.xpath('/D:multistatus/D:response/D:href/text()',
- namespaces=namespace_prefix_map):
- if (path != SERVER_PATH):
- connection = httplib.HTTPConnection('127.0.0.1', 8080)
- connection.putrequest('GET', path)
- connection.putheader('Authorization', auth_string)
- connection.putheader('User-Agent', CLIENT_AGENT)
- connection.endheaders()
- response = connection.getresponse()
- if response.status != 200:
- print 'Error retrieving {0}'.format(path)
- sys.exit(1)
This will make the PROPFIND for all the items in the collection, requesting the getetag property, and then from the response perform an HTTP GET for every item. Even better would be if it requested isCollection and knew better than to perform GETs on collections. I hope this simple example will be of use to someone.