def main(): args = create_parser().parse_args() webdav_url = args.webdav_url webdav_path = args.webdav_path pattern = args.pattern login = args.login password = args.password options = { 'webdav_hostname': webdav_url, 'webdav_login': login, 'webdav_password': password } client = Client(options) client.verify = False not_matched_files = {} for dir in client.list(webdav_path): new_path = '/'.join([webdav_path, dir]) files = client.list(new_path) for file in files: if not re.match(pattern, file): not_matched_files[''.join([new_path, file])] = 'NOT MATCHED' print('Running on the options {}'.format({ 'webdav_url': webdav_url, 'webdav_path': webdav_path, 'pattern': pattern })) print( tabulate(not_matched_files.items(), headers=['FILENAME', 'WARNING'], tablefmt="grid"))
def webdav_client(self, webdav_options: dict = None): # We localize the import of webdav3 here so it is an optional dependency. Only users who want to use webdav will # need to pip install webdavclient3 from webdav3.client import Client options = { "webdav_hostname": self.base_url, } if self._api_key is not None: options["webdav_login"] = "******" options["webdav_password"] = f"apikey|{self._api_key}" if webdav_options is not None: options = { **options, **webdav_options, } client = Client(options) if self._verify_ssl is False: client.verify = False # Set verify to false if using localhost without HTTPS return client
def put_file(filename_local, filename_remote): from webdav3.client import Client options = { 'webdav_hostname': WEBDAV_HOSTNAME, 'webdav_login': WEBDAV_LOGIN, 'webdav_password': WEBDAV_PASSWORD } print(options) client = Client(options) client.verify = False # To not check SSL certificates (Default = True) client.upload_sync(remote_path="/Documents/esp_frame/%s" % filename_remote, local_path=filename_local)
def upload_webdav(fn, url, login, pwd): logger.debug("Uploading file to webdav") #logger.debug("URL: "+ url) #logger.debug("Filename: "+ fn) options = { 'webdav_hostname': url, 'webdav_login': login, 'webdav_password': pwd } client = Client(options) client.verify = False client.upload_sync(remote_path=basename(fn), local_path=fn) return True
def upload_webdav(): options = { 'webdav_hostname': parse_config('WEBDAV', 'WEBDAV_UPLOAD_URL'), 'webdav_login': parse_config('WEBDAV', 'WEBDAV_UPLOAD_USER'), 'webdav_password': parse_config('WEBDAV', 'WEBDAV_UPLOAD_PW') } remotePath = parse_config('WEBDAV', 'WEBDAV_UPLOAD_PATH') client = Client(options) client.verify = False today = datetime.date.today() year, week_num, day_of_week = today.isocalendar() path = str(year) + "_cw" + str(week_num) if not client.check(remotePath + "/" + path): client.mkdir(remotePath + "/" + path) kwargs = { 'remote_path': remotePath + "/" + path + "/", 'local_path': targetPath + "/" + path + "/" } client.upload_async(**kwargs)
def salvar(caminho_arquivo, nome_arquivo='UFs.xlsx'): cfg = configparser.ConfigParser(interpolation=None) with io.open(str(config.arquivo_config_webdav), mode='r', encoding='utf-8') as fp: cfg.read_file(fp) options = { 'webdav_hostname': cfg['webdav']['hostname'], 'webdav_login': cfg['webdav']['login'], 'webdav_password': cfg['webdav']['password'] } pasta_virtual = cfg['webdav']['pasta_virtual'] cliente = Client(options) cliente.verify = False cliente.upload_sync(remote_path=pasta_virtual + '/' + nome_arquivo, local_path=caminho_arquivo) print('Upload concluído. Listando conteúdo do diretório remoto...') print(cliente.list(pasta_virtual))
def on_event(self, event, payload): if event == "plugin_backup_backup_created": # Helper function for human readable sizes def _convert_size(size_bytes): if size_bytes == 0: return "0B" size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") i = int(math.floor(math.log(size_bytes, 1024))) p = math.pow(1024, i) s = round(size_bytes / p, 2) return "%s %s" % (s, size_name[i]) now = datetime.now() davoptions = { 'webdav_hostname': self._settings.get(["server"]), 'webdav_login': self._settings.get(["username"]), 'webdav_password': self._settings.get(["password"]), 'webdav_timeout': self._settings.get(["timeout"]), } backup_path = payload["path"] backup_name = payload["name"] self._logger.info("Backup " + backup_path + " created, will now attempt to upload to " + davoptions["webdav_hostname"]) davclient = Client(davoptions) davclient.verify = self._settings.get(["verify_certificate"]) check_space = self._settings.get(["check_space"]) upload_path = now.strftime(self._settings.get(["upload_path"])) upload_path = ospath.join("/", upload_path) if self._settings.get(["upload_name"]): upload_name = now.strftime(self._settings.get( ["upload_name"])) + ospath.splitext(backup_path)[1] else: upload_name = backup_name self._logger.debug("Filename for upload: " + upload_name) upload_file = ospath.join("/", upload_path, upload_name) upload_temp = ospath.join("/", upload_path, upload_name + ".tmp") self._logger.debug("Upload location: " + upload_file) # Check actual connection to the WebDAV server as the check command will not do this. if check_space: self._logger.debug("Attempting to check free space.") try: # If the resource was not found dav_free = davclient.free() if dav_free < 0: # If we get a negative free size, this server is not returning correct value. check_space = False self._logger.warning( "Free space on server: " + _convert_size(dav_free) + ", it appears your server does not support reporting size correctly but it's still a proper way to check connectivity." ) else: self._logger.info("Free space on server: " + _convert_size(dav_free)) except RemoteResourceNotFound as exception: self._logger.error( "Resource was not found, something is probably wrong with your settings." ) return except ResponseErrorCode as exception: # Write error and exit function status = HTTPStatus(exception.code) error_switcher = { 400: "Bad request", 401: "Unauthorized", 403: "Forbidden", 404: "Not found", 405: "Method not allowed", 408: "Request timeout", 500: "Internal error", 501: "Not implemented", 502: "Bad gateway", 503: "Service unavailable", 504: "Gateway timeout", 508: "Loop detected", } if (exception.code == 401): http_error = "HTTP error 401 encountered, your credentials are most likely wrong." else: http_error = "HTTP error encountered: " + str( status.value) + " " + error_switcher.get( exception.code, status.phrase) self._logger.error(http_error) return except WebDavException as exception: self._logger.error( "An unexpected WebDAV error was encountered: " + exception.args) raise else: self._logger.debug( "Not checking free space, just try to check the WebDAV root." ) # Not as proper of a check as retrieving size, but it's something. if davclient.check("/"): self._logger.debug("Server returned WebDAV root.") else: self._logger.error( "Server did not return WebDAV root, something is probably wronkg with your settings." ) return backup_size = ospath.getsize(backup_path) self._logger.info("Backup file size: " + _convert_size(backup_size)) if check_space and (backup_size > dav_free): self._logger.error("Unable to upload, size is" + _convert_size(backup_size) + ", free space is " + _convert_size(dav_free)) return else: # Helper function to recursively create paths def _recursive_create_path(path): # Append leading / for preventing abspath issues path = ospath.join("/", path) if davclient.check(path): self._logger.debug("Directory " + path + " was found.") return True else: if path != "/": self._logger.debug( "Directory " + path + " was not found, checking parent.") if _recursive_create_path( ospath.abspath(ospath.join(path, ".."))): davclient.mkdir(path) self._logger.debug("Directory " + path + " has been created.") return True else: self._logger.error( "Could not find WebDAV root, something is probably wrong with your settings." ) return False if _recursive_create_path(upload_path): self._logger.debug("Uploading " + backup_path + " to " + upload_temp) davclient.upload_sync(remote_path=upload_temp, local_path=backup_path) self._logger.debug("Moving " + upload_temp + " to " + upload_file) davclient.move(remote_path_from=upload_temp, remote_path_to=upload_file) self._logger.info( "Backup has been uploaded successfully to " + davoptions["webdav_hostname"] + " as " + upload_file) else: self._logger.error( "Something went wrong trying to check/create the upload path." )
from requests.models import Response from webdav3.client import Client as NCClient import requests import settings import xmltodict import glob import os options = { 'webdav_hostname': settings.NC_URL, 'webdav_login': settings.NC_NAME, 'webdav_password': settings.NC_PASS } client = NCClient(options) client.verify = False def uploadFile(file_name): file_name = [ os.path.basename(p) for p in glob.glob('./tmp/' + file_name + "*", recursive=True) if os.path.isfile(p) ] # file_name = glob.glob('./tmp/' + file_name + "*") file_name = file_name[0] file_path = "youtube_dl/test/" + file_name file_local_path = "./tmp/" + file_name client.upload_sync(remote_path=file_path, local_path=file_local_path)
def test_webdav(self): server_2_ip = '172.16.3.155' error, server_id = self.mm['webdavalpine'].run("add_server", ip_addr=server_2_ip, fqdn='webdav.test') self.assertTrue(error == None, msg=error) time.sleep(10) webdav_url = "https://{}/files/".format(server_2_ip) full_path = parentdir + "/work/webdavalpine/1/webdav/admin.pass" self.assertTrue(os.path.exists(full_path), msg="{} does not exist".format(full_path)) admin_pass = open(full_path, "r").read().strip() resp = requests.get("https://{}/".format(server_2_ip), verify=False) self.assertTrue(resp.status_code == 200) options = { 'webdav_hostname': webdav_url, 'webdav_login': "******", 'webdav_password': admin_pass } client = Client(options) client.verify = False result = client.check("public") self.assertTrue(result == True) result = client.list() self.assertTrue(len(result) > 0) subprocess.check_output(["touch /tmp/testdata"], shell=True) client.upload_sync("public/testdata", "/tmp/testdata") result = client.check("public/testdata") self.assertTrue(result == True) unauth_client = Client({ 'webdav_hostname': webdav_url }) unauth_client.verify = False public_list = unauth_client.list("public/") self.assertTrue(len(public_list) > 0) try: unauth_client.upload_sync("public/testdata2", "/tmp/testdata") self.fail() except: pass error, _ = self.mm['webdavalpine'].run("stop_server", id=server_id) self.assertTrue(error == None, msg=error) time.sleep(2) error, _ = self.mm['webdavalpine'].run("start_server", id=server_id) self.assertTrue(error == None, msg=error) time.sleep(10) resp = requests.get("https://{}/".format(server_2_ip), verify=False) self.assertTrue(resp.status_code == 200) options = { 'webdav_hostname': webdav_url, 'webdav_login': "******", 'webdav_password': admin_pass } client2 = Client(options) client2.verify = False public_list = client2.list("public/") self.assertTrue(len(public_list) > 0) self.assertTrue(public_list[1] == "testdata") error, _ = self.mm['webdavalpine'].run("remove_server", id=server_id) self.assertTrue(error == None, msg=error)
config.read(CONFIG_FILE) # download files log('downloading files:') conn_options = None basepath = None client = None contents_zip = None if config.has_section('WEBDAV'): conn_options = { 'webdav_hostname': config['WEBDAV']['hostname'], 'webdav_login': config['WEBDAV']['username'], 'webdav_password': config['WEBDAV']['sync_pass'] } client = Client(conn_options) client.verify = True log('- downloading keystore') client.download_sync(remote_path=add_basepath('OmniFocus.ofocus/encrypted'), local_path='encrypted') log('- finding database') files = client.list(add_basepath('OmniFocus.ofocus/')) contents_zip = [file for file in files if file.endswith('.zip') and file.startswith('00')][0] # contents_zip = [file for file in files if file.endswith('.zip') and not file.startswith('00')][0] log('- downloading database') client.download_sync(remote_path=add_basepath('OmniFocus.ofocus/{}'.format(contents_zip)), local_path=contents_zip) # decrypt them log('decrypting database') dec_contents_zip = None if config.has_section('ENC') and 'is_encrypted' in config['ENC'] and config['ENC'].getboolean('is_encrypted') and contents_zip is not None: dec_contents_zip = 'dec_{}'.format(contents_zip) enc_pass = config['ENC']['enc_pass']
def upload_dav(): try: options = { 'webdav_hostname': "https://192.168.1.6/remote.php/dav/files/clouduser/", 'webdav_login': "******", 'webdav_password': "******", 'verbose':True } conn=False client = Client(options) client.verify = False try: if(client.list()): print('\n' * 4) print("Connection Successfull") conn=True print('\n' * 3) except: print('\n' * 4) print("Connection error. Check credentials") if conn: print("##############################################################") opt=int(input("Choose action to be performed:\n1:Show all files\n2:Make Directory\n3:Delete\n4:Download file\n5:Upload File\n")) if opt==1: files=client.list() print(files) elif opt==2: d=input("Enter directory name\n") if client.mkdir(d): print("Created Successfully") elif opt==3: d=input("Enter directory or file name\n") client.clean(d) print("Deleted") elif opt==4: file=input("Enter file to download with public key and signature\n") path_1=file+'.encrypted' path_2=file+'.sign' path_3='public_key.pem' s_time=time.time() client.download_sync(path_1, path_1) client.download_sync(path_2, path_2) client.download_sync(path_3, path_3) e_time=time.time() t=e_time-s_time print("Downloaded, Time taken is",e_time-s_time) elif opt==5: file=input("Enter file to upload with public key and signature\n") path_1=file+'.encrypted' path_2=file+'.sign' path_3='public_key.pem' s_time=time.time() client.upload_sync(path_1, path_1) client.upload_sync(path_2, path_2) client.upload_sync(path_3, path_3) e_time=time.time() print("Uplaoded, Time taken is",e_time-s_time) else: None except WebDavException as exception: print("\n\n",exception,"\n\n")