[Python] pysmb 教學

程式語言:Python
Package:pysmb
官方文件

功能:SMB 連接與處理,即是 Window 的網路芳鄰
import socket
from smb.SMBConnection import SMBConnection

ip = '192.168.21.237'

name = socket.gethostbyaddr(ip)
ipGet = socket.gethostbyname(name[0])
print(name, ipGet, sep='\n')


remote_name = name[0]
conn = SMBConnection('username', 'password', 'any_name', remote_name)
assert conn.connect(ip, timeout=3)

for s in conn.listShares():
    print('------------------------------------')
    print('name', s.name)
    print('comments', s.comments)
    print('isSpecial', s.isSpecial)
    print('isTemporary', s.isTemporary)
    
    ''' 
    SharedDevice.DISK_TREE      0x00
    SharedDevice.PRINT_QUEUE    0x01
    SharedDevice.COMM_DEVICE    0x02
    SharedDevice.IPC            0x03
    '''
    print('type', s.type)
    print('')
    print('### FileList ###')
    try:
        for f in conn.listPath(s.name, '/'):
            print(f.filename)
        
        for f in conn.listPath(s.name, '/game'):
            print(f.filename)
    except:
        print('### can not access the resource')
    print('------------------------------------')
    print('')

conn.close()

Class 類別

  • NBNSProtocol Class
    • 非同步取得 ip 或 hostname
  • NetBIOS class
    • 同步取得 ip 或 hostname
  • SMBConnection Class
    • 操作網路磁碟
  • SMbHandler Class
    • 提供 "smb://" 給 urllib2 使用
  • SMBProtocolFactory Class
    • 運用在 Twisted framework
  • SharedDevice Class
    • 網路磁碟屬性定義
  • SharedFile Class
    • 資料夾屬性定義
  • SMB Exceptions
    • 例外處理

class nmb.NetBIOS.NetBIOS(broadcast=True, listen_port=0)

  • 似乎有 bug,建議用 socket 取代
  • __init__(broadcast=True, listen_port=0)
  • close()
  • queryIPForName(ip, port=137, timeout=30)
  • queryName(name, ip='', port=137, timeout=30)
from nmb.NetBIOS import NetBIOS
import socket

n = NetBIOS()
ip = '192.168.21.6'
name = n.queryIPForName(ip)

# pysmb-1.1.19 會有問題
ipGet = n.queryName(name)
print(name, ipGet)
n.close()

name = socket.gethostbyaddr(ip)
ipGet = socket.gethostbyname(name[0])
print(name, ipGet, sep='\n')


class smb.SMBConnection.SMBConnection(username, password, my_name, remote_name, domain='', use_ntlm_v2=True, sign_options=2, is_direct_tcp=False)

  • __init__(username, password, my_name, remote_name, domain='', use_ntlm_v2=True, sign_options=2, is_direct_tcp=False)
    • my_name
      • 任意名字
    • remote_name
      • 對方電腦的名字
  • close()
  • connect(ip, port=139, sock_family=2, timeout=60)
  • createDirectory(service_name, path, timeout=30)
    • service_name
      • share 的名字
    • path
      • 路徑,例:'\' 或 '\folder'
    • timeout
      • 試著連接的最大時間,單位為秒
  • deleteDirectory(service_name, path, timeout=30)
    • service_name
      • share 的名字
    • path
      • 路徑,例:'\' 或 '\folder'
    • timeout
      • 試著連接的最大時間,單位為秒
  • deleteFiles(service_name, path_file_pattern, timeout=30)
    • service_name
      • share 的名字
  • echo(data, timeout=10)
  • getAttributes(service_name, path, timeout=30)
    • service_name
      • share 的名字
    • path
      • 路徑,例:'\' 或 '\folder'
    • timeout
      • 試著連接的最大時間,單位為秒
  • listPath(service_name, path, search=55, pattern='*', timeout=30)
    • 列出路徑下的檔案和資料夾 
    • service_name
      • share 的名字
    • path
      • 路徑,例:'\' 或 '\folder'
    • search
      • SMB_FILE_ATTRIBUTE_READONLY | SMB_FILE_ATTRIBUTE_HIDDEN | SMB_FILE_ATTRIBUTE_SYSTEM | SMB_FILE_ATTRIBUTE_DIRECTORY | SMB_FILE_ATTRIBUTE_ARCHIVE 
    • timeout
      • 試著連接的最大時間,單位為秒
  • listShares(timeout=30)
    • 列出分享的資料夾

留言