[Python] pysmb 教學

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

功能:SMB 連接與處理,即是 Window 的網路芳鄰
  1. import socket
  2. from smb.SMBConnection import SMBConnection
  3.  
  4. ip = '192.168.21.237'
  5.  
  6. name = socket.gethostbyaddr(ip)
  7. ipGet = socket.gethostbyname(name[0])
  8. print(name, ipGet, sep='\n')
  9.  
  10.  
  11. remote_name = name[0]
  12. conn = SMBConnection('username', 'password', 'any_name', remote_name)
  13. assert conn.connect(ip, timeout=3)
  14.  
  15. for s in conn.listShares():
  16. print('------------------------------------')
  17. print('name', s.name)
  18. print('comments', s.comments)
  19. print('isSpecial', s.isSpecial)
  20. print('isTemporary', s.isTemporary)
  21. '''
  22. SharedDevice.DISK_TREE 0x00
  23. SharedDevice.PRINT_QUEUE 0x01
  24. SharedDevice.COMM_DEVICE 0x02
  25. SharedDevice.IPC 0x03
  26. '''
  27. print('type', s.type)
  28. print('')
  29. print('### FileList ###')
  30. try:
  31. for f in conn.listPath(s.name, '/'):
  32. print(f.filename)
  33. for f in conn.listPath(s.name, '/game'):
  34. print(f.filename)
  35. except:
  36. print('### can not access the resource')
  37. print('------------------------------------')
  38. print('')
  39.  
  40. 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)
  1. from nmb.NetBIOS import NetBIOS
  2. import socket
  3.  
  4. n = NetBIOS()
  5. ip = '192.168.21.6'
  6. name = n.queryIPForName(ip)
  7.  
  8. # pysmb-1.1.19 會有問題
  9. ipGet = n.queryName(name)
  10. print(name, ipGet)
  11. n.close()
  12.  
  13. name = socket.gethostbyaddr(ip)
  14. ipGet = socket.gethostbyname(name[0])
  15. print(name, ipGet, sep='\n')
  16.  

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)
    • 列出分享的資料夾

留言