Advertisement
Guest User

Untitled

a guest
Jun 10th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. >>> smb = smbclient.SambaClient(server="MYSERVER", share="MYSHARE",
  2. username='foo', password='bar', domain='baz')
  3. >>> print smb.listdir(u"/")
  4. [u'file1.txt', u'file2.txt']
  5. >>> f = smb.open('/file1.txt')
  6. >>> data = f.read()
  7. >>> f.close()
  8. >>> smb.rename(u'/file1.txt', u'/file1.old')
  9.  
  10. suri = 'smb://' + settings.SERVER + '/' + settings.SHARE + '/test.dat'
  11. dpath = '/tmp/destination.out'
  12.  
  13. # open smbc uri
  14. sfile = ctx.open(suri, os.O_RDONLY)
  15.  
  16. # open local target where to copy file
  17. dfile = open(dpath, 'wb')
  18.  
  19. #copy file and flush
  20. dfile.write(sfile.read())
  21. dfile.flush()
  22.  
  23. #close both files
  24. sfile.close()
  25. dfile.close()
  26.  
  27. ctx = smbc.Context()
  28.  
  29. def auth_fn(server, share, workgroup, username, password):
  30. return (workgroup, settings.USERNAME, settings.PASSWORD)
  31.  
  32. ctx.optionNoAutoAnonymousLogin = True
  33. ctx.functionAuthData = auth_fn
  34.  
  35. def do_auth (server, share, workgroup, username, password):
  36. return ('MYDOMAIN', 'myacct', 'mypassword')
  37.  
  38.  
  39. # Create the context
  40. ctx = smbc.Context (auth_fn=do_auth)
  41. destfile = "myfile.txt"
  42. source = open('/home/myuser/textfile.txt', 'r')
  43. # open a SMB/CIFS file and write to it
  44. file = ctx.open ('smb://server/share/folder/'+destfile, os.O_CREAT | os.O_WRONLY)
  45. for line in source:
  46. file.write (line)
  47. file.close()
  48. source.close()
  49.  
  50. # open a SMB/CIFS file and read it to a local file
  51. source = ctx.open ('smb://server/share/folder/'+destfile, os.O_RDONLY)
  52. destfile = "/home/myuser/textfile.txt"
  53. fle = open(destfile, 'w')
  54. for line in source:
  55. file.write (line)
  56. file.close()
  57. source.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement