import io
import cv2
from PIL import Image
from smb.SMBConnection import *
import numpy as np
class SMBClient(object):
"""
smb连接客户端
"""
prot = None
status = False
samba = None
def __init__(self, user_name, passwd, ip, port=139):
self.user_name = user_name
self.passwd = passwd
self.ip = ip
self.port = port
def connect(self):
try:
self.samba = SMBConnection(self.user_name, self.passwd, '', '', use_ntlm_v2=True)
self.samba.connect(self.ip, self.port)
self.status = self.samba.auth_result
except:
self.samba.close()
def disconnect(self):
if self.status:
self.samba.close()
def all_file_names_in_dir(self, service_name, dir_name):
'''
列出文件夹内所有文件名 smb.all_file_names_in_dir("testserver", "/data/zengxiaohui/")
:param service_name:服务器的文件夹路径 eg:testserver
:param dir_name: 相对于上面的路径 eg:/data/
:return:
'''
f_names = list()
for e in self.samba.listPath(service_name, dir_name):
# if len(e.filename) > 3: (会返回一些.的文件,需要过滤)
if e.filename[0] != '.':
f_names.append(e.filename)
return f_names
def upload(self, service_name, dir_name, file_name):
self.samba.storeFile(service_name, dir_name, file_name)
def download(self, f_names, service_name, dir_name, local_dir):
assert isinstance(f_names, list)
for f_name in f_names:
filedata = bytearray()
start=time.time()
self.samba.retrieveFile(service_name, os.path.join(dir_name, f_name), filedata)
print(time.time() - start)
pilImage = cv2.imdecode(np.asarray(filedata, dtype='uint8'), cv2.IMREAD_COLOR)
#pilImage = cv2.imread(imgShowPath)
cv2.imshow("dfa",pilImage)
cv2.waitKey()
if __name__ == '__main__':
smb = SMBClient('testserver', 'testserver', '10.20.200.170')
smb.connect()
smb.download(["2969138231500315.jpg"], "testserver", "/data/29691/", r"C:\Users\zengxh\Documents\workspace\")
需要修改smb\base.py里面的
if isinstance(file_obj,bytearray): # 自己修改判断是否是字节
file_obj.extend(read_message.payload.data)
else:
file_obj.write(read_message.payload.data)