python 獲取共享檔案
阿新 • • 發佈:2021-07-13
- smbclient方法
def check_smb_img(): smbclient.register_session("1.1.1.1", username="name", password="password") img_path = smbclient.listdir(r"\\1.1.1.1\test") if not len(img_path): return False file_path = r"\\10.101.35.241\int4\amize\redtag-img" + '\\' + img_path[0] file_name = os.path.basename(file_path) # res = path.rsplit('.',1)[0].split('_')[1] res = re.search(r'(0x\w+)\.tar', file_path) file_checksum = res.group(1) return file_path, file_name, file_checksum def read_in_chunks(filePath, chunk_size=1024 * 1024): file_object = smbclient.open_file(filePath, mode='rb') while True: chunk_data = file_object.read(chunk_size) if not chunk_data: break yield chunk_data def verify_file_checksum(file, checksum): with open(file, 'rb') as f: if zlib.adler32(f.read()) == int(checksum, 16): print('...%s downloaded successfully' % file) return True print('checksum verification fail') return False def download_smb_file(file_path, file_name, file_checksum): print('download file:', file_path) if os.path.exists(file_name): os.remove(file_name) for chunk in read_in_chunks(file_path): with open(file_name, mode='ab') as fw: fw.write(chunk) verify_file_checksum(file_name, file_checksum)
- os直接操作
DOWNLOAD_LINK=r'\\1.1.1.1\file\
all_file = os.listdir(DOWNLOAD_LINK)
os.system('copy DOWNLOAD_LINK\\test .\\')