And I put this in site-packages
# -*- coding: utf-8 -*-
# based on https://github.com/humberry/smb-example/blob/master/smb-test.py
# smb+nmb from https://github.com/miketeo/pysmb/tree/master/python3
# pyasn1 from https://github.com/etingof/pyasn1
from io import BytesIO
from smb.SMBConnection import SMBConnection
from smb import smb_structs
from nmb.NetBIOS import NetBIOS
import os
import sys
from socket import gethostname
class SMB_client():
def __init__(self,username=None,password=None,smb_name=None):
self.username = username
self.password = password
self.smb_name = smb_name
self.smb_ip = None
self.conn = None
self.service_name = None
self.my_name = None
self.tree = []
def getBIOSName(self, remote_smb_ip, timeout=5): # unused if dynamic IP
# ip -> smb name
try:
bios = NetBIOS()
srv_name = bios.queryIPForName(remote_smb_ip, timeout=timeout)
return srv_name[0]
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
return None
def getIP(self):
# smb name -> ip
try:
bios = NetBIOS()
ip = bios.queryName(self.smb_name)
return ip[0]
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
return None
def connect(self):
try:
self.my_name = gethostname() # iDevice name
self.smb_ip = self.getIP()
smb_structs.SUPPORT_SMB2 = True
self.conn = SMBConnection(self.username, self.password, self.my_name, self.smb_name, use_ntlm_v2 = True)
self.conn.connect(self.smb_ip, 139) #139=NetBIOS / 445=TCP
if self.conn:
shares = self.conn.listShares()
for share in shares:
if share.type == 0: # 0 = DISK_TREE
self.service_name = share.name
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
def close(self):
try:
self.conn.close()
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
def getRemoteDir(self, path, pattern):
try:
files = self.conn.listPath(self.service_name, path, pattern=pattern)
return files
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
return None
def getRemoteTree(self,path=''):
try:
if path == '':
w = ''
else:
w = path+'/'
files = self.getRemoteDir(path, '*')
if files:
for file in files:
if file.filename[0] == '.':
continue
self.tree.append({'name':w+file.filename, 'isdir':file.isDirectory, 'size':file.file_size})
if file.isDirectory:
self.getRemoteTree(path=w+file.filename)
return self.tree
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
return None
def download(self, path, filename):
try:
print('Download = ' + path + filename)
attr = self.conn.getAttributes(self.service_name, path+filename)
print('Size = %.1f kB' % (attr.file_size / 1024.0))
print('start download')
file_obj = BytesIO()
file_attributes, filesize = self.conn.retrieveFile(self.service_name, path+filename, file_obj)
fw = open(filename, 'wb')
file_obj.seek(0)
for line in file_obj:
fw.write(line)
fw.close()
print('download finished')
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
def upload(self, path, filename):
try:
print('Upload = ' + path + filename)
print('Size = %.1f kB' % (os.path.getsize(filename) / 1024.0))
print('start upload')
with open(filename, 'rb') as file_obj:
filesize = self.conn.storeFile(self.service_name, path+filename, file_obj)
print('upload finished')
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
def delete_remote_file(self,path, filename):
try:
self.conn.deleteFiles(self.service_name, path+filename)
print('Remotefile ' + path + filename + ' deleted')
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
def createRemoteDir(self, path):
try:
self.conn.createDirectory(self.service_name, path)
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
def removeRemoteDir(self,path):
try:
self.conn.deleteDirectory(self.service_name, path)
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
def renameRemoteFileOrDir(self,old_path, new_path):
try:
self.conn.rename(self.service_name, old_path, new_path)
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
Use like
from SMB_client import SMB_client
my_smb = SMB_client(username='xxxxx',password='yuyy',smb_name='zzzzzz')
my_smb.connect()
tree = my_smb.getRemoteTree()
for elem in tree:
print(elem)
#my_smb.download(path, filename)
#my_smb.upload(path, filename)
#my_smb.delete_remote_file(path, filename)
#my_smb.createRemoteDir(path)
#my_smb.removeRemoteDir(path)
#my_smb.renameRemoteFileOrDir(path, new_path)
my_smb.close()