Exemplo n.º 1
0
 def get_file(self, file_name):
     client = WebDAVClient("127.0.0.1", 9997)
     client.timeout = 1
     try:
         response = client.get('/%s'%file_name)
     except socket.timeout, err:
         pass
Exemplo n.º 2
0
    def connect(self, ):

        urlparsed = requests.utils.urlparse(self.url)

        self.wc = WebDAVClient(host=urlparsed.netloc,
                               protocol=urlparsed.scheme)

        self.wc.setbasicauth(self.login.encode('utf-8'),
                             self.passwd.encode('utf-8'))
        time_delta = None

        local_time = datetime.datetime.utcnow()

        response = self.wc.options('/').headers.get('date')
        if response is None:
            response = self.wc.options('/').headers.get('Date')

        remote_datetime = \
            rfc822.parsedate(response)

        self.timedelta = time.mktime(local_time.utctimetuple()) \
            - time.mktime(remote_datetime)

        self._check_notes_folder()

        return time_delta
Exemplo n.º 3
0
    def test04_get(self):
        client = WebDAVClient("127.0.0.1", 8080)

        response = client.get('/foo')
        self.assertEqual(response.statusline, 'HTTP/1.1 200 OK')

        response = client.get(FILE_PATH)
        self.assertEqual(response.statusline, 'HTTP/1.1 200 OK')

        self.assertEqual(TestIdepositbox.CHECKSUM, hashlib.sha1(response.content).hexdigest())
Exemplo n.º 4
0
    def test01_create_dir(self):
        print '** connecting to webdav server'
        client = WebDAVClient("127.0.0.1", 8080)
        print '** connected to webdav server'
        response = client.mkcol("/foo")
        print '** mkcol response = %s'%response
        self.assertEqual(response.statusline, 'HTTP/1.1 201 Created')

        with self.assertRaises(HTTPUserError):
            response = client.mkcol("/foo2/test1/test2")
        print '** /foo is created...'
Exemplo n.º 5
0
    def connect(self,):

        urlparsed = requests.utils.urlparse(self.url)

        self.wc = WebDAVClient(host=urlparsed.netloc,
                               protocol=urlparsed.scheme)

        self.wc.setbasicauth(self.login.encode('utf-8'),
                             self.passwd.encode('utf-8'))
        time_delta = None

        local_time = datetime.datetime.utcnow()

        response = self.wc.options('/').headers.get('date')
        if response is None:
            response = self.wc.options('/').headers.get('Date')

        remote_datetime = \
            rfc822.parsedate(response)

        self.timedelta = time.mktime(local_time.utctimetuple()) \
            - time.mktime(remote_datetime)

        self._check_notes_folder()

        return time_delta
Exemplo n.º 6
0
    def test03_put(self):
        print '** creating test file at %s'%TEST_FILE
        fb = open(TEST_FILE, 'wb')
        data = ''.join(random.choice(string.letters) for i in xrange(1024))
        data *= 5*1024
        fb.write(data)
        fb.close()
        print '** file is created!'

        TestIdepositbox.CHECKSUM = hashlib.sha1(data).hexdigest()
        client = WebDAVClient("127.0.0.1", 8080)

        print '** PUT file content to 127.0.0.1:8080!'
        with open(TEST_FILE) as fd:
            response = client.put(FILE_PATH, fd, "text/plain")
            self.assertEqual(response.statusline, 'HTTP/1.1 201 Created')

        response = client.propfind("/foo", depth=1)
        self.assertTrue('displayname>test_с_русскими_буквами.out<' in response.content)

        print '** waiting while file is synchronized!'
        wait_oper_status(TestIdepositbox.CLIENT.get_nibbler().inprocess_operations, FILE_PATH, Transaction.TS_FINISHED)
        print '** file is synchronized!'
Exemplo n.º 7
0
    def test05_delete(self):
        client = WebDAVClient("127.0.0.1", 8080)
        response = client.delete(FILE_PATH)
        self.assertEqual(response.statusline, 'HTTP/1.1 204 No Content')

        with self.assertRaises(HTTPUserError):
            response = client.delete(FILE_PATH)

        response = client.delete('/foo/')
        self.assertEqual(response.statusline, 'HTTP/1.1 204 No Content')

        with self.assertRaises(HTTPUserError):
            client.propfind("/foo")
Exemplo n.º 8
0
 def _get_client(self):
     client = WebDAVClient(self.url, self.port)
     client.setbasicauth(self.user, self.pwd) 
     client.timeout = 60 #set reasonable timeout
     return client
Exemplo n.º 9
0
            return True
        else:
            return False
    else:
        return False


if __name__ == "__main__":
    random.seed(time.time())
    good_put = 0
    error_put = 0
    good_get = 0
    error_get = 0
    total_size = 0
    for c in range(1, 10):
        client = WebDAVClient("192.168.56.103", 6601)
        for r in range(1, 10):
            filename = "/tmp/" + "metis_web_dav_test_" + str(r)
            size = random.randint(1024, 100000)
            (fileobject, crc) = gen_file(filename, size)
            uri = "/1/2/" + str(r) + ".jpg"
            res = upload_file(client, fileobject, uri)
            if res:
                good_put += 1
            else:
                error_put += 1
            res = download_file(client, uri, crc)
            if res:
                good_get += 1
            else:
                error_get += 1
Exemplo n.º 10
0
 def test02_propfind(self):
     client = WebDAVClient("127.0.0.1", 8080)
     response = client.propfind("/foo")
     self.assertEqual(response.statusline, 'HTTP/1.1 207 Multistatus')
     self.assertTrue('displayname>foo<' in response.content)
Exemplo n.º 11
0
class WebdavClient(object):

    def __init__(self,):

        settings = Settings()

        self.url = settings.get('WebDav', 'url')

        self.login = settings.get('WebDav', 'login')
        self.passwd = settings.get('WebDav', 'password')
        self.basepath = requests.utils.urlparse(self.url).path
        self.remotefolder = settings.get('WebDav', 'remoteFolder')
        self.nosslcheck = settings.get('WebDav', 'nosslcheck')
        self.timedelta = None
        self.wc = None
        self.locktoken = None

    def connect(self,):

        urlparsed = requests.utils.urlparse(self.url)

        self.wc = WebDAVClient(host=urlparsed.netloc,
                               protocol=urlparsed.scheme)

        self.wc.setbasicauth(self.login.encode('utf-8'),
                             self.passwd.encode('utf-8'))
        time_delta = None

        local_time = datetime.datetime.utcnow()

        response = self.wc.options('/').headers.get('date')
        if response is None:
            response = self.wc.options('/').headers.get('Date')

        remote_datetime = \
            rfc822.parsedate(response)

        self.timedelta = time.mktime(local_time.utctimetuple()) \
            - time.mktime(remote_datetime)

        self._check_notes_folder()

        return time_delta

    def _check_notes_folder(self,):
        response = self.wc.propfind(uri=self.basepath,
                                    names=True,
                                    depth=1)
        print type(response)
        print dir(response)
        ownnotes_folder_exists = False
        ownnotes_remote_folder = self.get_abspath('')
        if response.real != 207:
            is_connected = False
        else:
            is_connected = True
            for res in response:
                if (res.href == ownnotes_remote_folder):
                    ownnotes_folder_exists = True
            if not ownnotes_folder_exists:
                self.wc.mkcol(ownnotes_remote_folder)
        return is_connected

    def exists_or_create(self, relpath):
        response = self.wc.propfind(uri=self.get_abspath(''),
                                    names=True,
                                    depth=1)

        if response.real != 207:
            return False
        else:
            for res in response:
                if (res.href == self.get_abspath(relpath)):
                    return True

        self.wc.mkcol(self.get_abspath(relpath))

    def upload(self, fh, relpath):
        self.wc.put(self.get_abspath(relpath), fh)

    def download(self, relpath, fh):
        fh.write(self.wc.get(self.get_abspath(relpath)).content)

    def rm(self, relpath):
        self.wc.delete(self.get_abspath(relpath))

    def get_abspath(self, relpath, asFolder=False):
        abspath = self.basepath

        if abspath.endswith('/'):
            abspath += self.remotefolder
        else:
            abspath += '/' + self.remotefolder
        if abspath.endswith('/'):
            abspath += relpath
        else:
            abspath += '/' + relpath
        if asFolder and not abspath.endswith('/'):
            abspath += '/'
        return abspath

    def get_relpath(self, abspath):
        basepath = self.basepath
        if basepath.endswith('/'):
            basepath += self.remotefolder
        else:
            basepath += '/' + self.remotefolder
        return os.path.relpath(abspath, basepath)

    def get_mtime(self, relpath):
        response = self.wc.propfind(uri=self.get_abspath(relpath))
        if response.real != 207:
            raise NetworkError('Can\'t get mtime file on webdav host')
        else:
            for res in response:
                return round(time.mktime(rfc822.parsedate(
                    res.get('getlastmodified').text)))

    def get_files_index(self,):
        index = {}

        abspath = self.get_abspath('', asFolder=True)

        response = self.wc.propfind(uri=abspath,
                                    names=True,
                                    depth='infinity')

        print response.real

        if response.real != 207:
            raise NetworkError('Can\'t list file on webdav host')
        else:
            for res in response:
                print(res.href)
                if len(res.get('resourcetype').getchildren()) == 0:
                    index[self.get_relpath(res.href)] = \
                        round(time.mktime(rfc822.parsedate(
                            res.get('getlastmodified').text)))

        return index

    def move(self, srcrelpath, dstrelpath):
        '''Move/Rename a note on webdav'''
        self.wc.move(self.get_abspath(srcrelpath),
                     self.get_abspath(dstrelpath),
                     depth='infinity',
                     overwrite=True)

    def lock(self, relpath=''):
        abspath = self.get_abspath(relpath)
        if relpath:
            self.locktoken = self.wc.lock(uri=abspath, timeout=60)
        else:
            self.locktoken = self.wc.lock(uri=abspath,
                                          depth='infinity',
                                          timeout=300)

    def unlock(self, relpath=None):
        if self.locktoken:
            self.wc.unlock(uri_or_lock=self.locktoken)
Exemplo n.º 12
0
 def _get_client(self):
     client = WebDAVClient(self.url, self.port)
     client.setbasicauth(self.user, self.pwd)
     client.timeout = 60  #set reasonable timeout
     return client
Exemplo n.º 13
0
 def send_file(self, file_name, file_len):
     client = WebDAVClient("127.0.0.1", 9997)
     response = client.put('/%s'%file_name, RANDOM(file_len), "text/plain")
     self.assertEqual(response.statusline, 'HTTP/1.1 201 Created')
Exemplo n.º 14
0
class WebdavClient(object):
    def __init__(self, ):

        settings = Settings()

        self.url = settings.get('WebDav', 'url')

        self.login = settings.get('WebDav', 'login')
        self.passwd = settings.get('WebDav', 'password')
        self.basepath = requests.utils.urlparse(self.url).path
        self.remotefolder = settings.get('WebDav', 'remoteFolder')
        self.nosslcheck = settings.get('WebDav', 'nosslcheck')
        self.timedelta = None
        self.wc = None
        self.locktoken = None

    def connect(self, ):

        urlparsed = requests.utils.urlparse(self.url)

        self.wc = WebDAVClient(host=urlparsed.netloc,
                               protocol=urlparsed.scheme)

        self.wc.setbasicauth(self.login.encode('utf-8'),
                             self.passwd.encode('utf-8'))
        time_delta = None

        local_time = datetime.datetime.utcnow()

        response = self.wc.options('/').headers.get('date')
        if response is None:
            response = self.wc.options('/').headers.get('Date')

        remote_datetime = \
            rfc822.parsedate(response)

        self.timedelta = time.mktime(local_time.utctimetuple()) \
            - time.mktime(remote_datetime)

        self._check_notes_folder()

        return time_delta

    def _check_notes_folder(self, ):
        response = self.wc.propfind(uri=self.basepath, names=True, depth=1)
        print type(response)
        print dir(response)
        ownnotes_folder_exists = False
        ownnotes_remote_folder = self.get_abspath('')
        if response.real != 207:
            is_connected = False
        else:
            is_connected = True
            for res in response:
                if (res.href == ownnotes_remote_folder):
                    ownnotes_folder_exists = True
            if not ownnotes_folder_exists:
                self.wc.mkcol(ownnotes_remote_folder)
        return is_connected

    def exists_or_create(self, relpath):
        response = self.wc.propfind(uri=self.get_abspath(''),
                                    names=True,
                                    depth=1)

        if response.real != 207:
            return False
        else:
            for res in response:
                if (res.href == self.get_abspath(relpath)):
                    return True

        self.wc.mkcol(self.get_abspath(relpath))

    def upload(self, fh, relpath):
        self.wc.put(self.get_abspath(relpath), fh)

    def download(self, relpath, fh):
        fh.write(self.wc.get(self.get_abspath(relpath)).content)

    def rm(self, relpath):
        self.wc.delete(self.get_abspath(relpath))

    def get_abspath(self, relpath, asFolder=False):
        abspath = self.basepath

        if abspath.endswith('/'):
            abspath += self.remotefolder
        else:
            abspath += '/' + self.remotefolder
        if abspath.endswith('/'):
            abspath += relpath
        else:
            abspath += '/' + relpath
        if asFolder and not abspath.endswith('/'):
            abspath += '/'
        return abspath

    def get_relpath(self, abspath):
        basepath = self.basepath
        if basepath.endswith('/'):
            basepath += self.remotefolder
        else:
            basepath += '/' + self.remotefolder
        return os.path.relpath(abspath, basepath)

    def get_mtime(self, relpath):
        response = self.wc.propfind(uri=self.get_abspath(relpath))
        if response.real != 207:
            raise NetworkError('Can\'t get mtime file on webdav host')
        else:
            for res in response:
                return round(
                    time.mktime(
                        rfc822.parsedate(res.get('getlastmodified').text)))

    def get_files_index(self, ):
        index = {}

        abspath = self.get_abspath('', asFolder=True)

        response = self.wc.propfind(uri=abspath, names=True, depth='infinity')

        print response.real

        if response.real != 207:
            raise NetworkError('Can\'t list file on webdav host')
        else:
            for res in response:
                print(res.href)
                if len(res.get('resourcetype').getchildren()) == 0:
                    index[self.get_relpath(res.href)] = \
                        round(time.mktime(rfc822.parsedate(
                            res.get('getlastmodified').text)))

        return index

    def move(self, srcrelpath, dstrelpath):
        '''Move/Rename a note on webdav'''
        self.wc.move(self.get_abspath(srcrelpath),
                     self.get_abspath(dstrelpath),
                     depth='infinity',
                     overwrite=True)

    def lock(self, relpath=''):
        abspath = self.get_abspath(relpath)
        if relpath:
            self.locktoken = self.wc.lock(uri=abspath, timeout=60)
        else:
            self.locktoken = self.wc.lock(uri=abspath,
                                          depth='infinity',
                                          timeout=300)

    def unlock(self, relpath=None):
        if self.locktoken:
            self.wc.unlock(uri_or_lock=self.locktoken)