Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> smb = smbclient.SambaClient(server="MYSERVER", share="MYSHARE",
- username='foo', password='bar', domain='baz')
- >>> print smb.listdir(u"/")
- [u'file1.txt', u'file2.txt']
- >>> f = smb.open('/file1.txt')
- >>> data = f.read()
- >>> f.close()
- >>> smb.rename(u'/file1.txt', u'/file1.old')
- suri = 'smb://' + settings.SERVER + '/' + settings.SHARE + '/test.dat'
- dpath = '/tmp/destination.out'
- # open smbc uri
- sfile = ctx.open(suri, os.O_RDONLY)
- # open local target where to copy file
- dfile = open(dpath, 'wb')
- #copy file and flush
- dfile.write(sfile.read())
- dfile.flush()
- #close both files
- sfile.close()
- dfile.close()
- ctx = smbc.Context()
- def auth_fn(server, share, workgroup, username, password):
- return (workgroup, settings.USERNAME, settings.PASSWORD)
- ctx.optionNoAutoAnonymousLogin = True
- ctx.functionAuthData = auth_fn
- def do_auth (server, share, workgroup, username, password):
- return ('MYDOMAIN', 'myacct', 'mypassword')
- # Create the context
- ctx = smbc.Context (auth_fn=do_auth)
- destfile = "myfile.txt"
- source = open('/home/myuser/textfile.txt', 'r')
- # open a SMB/CIFS file and write to it
- file = ctx.open ('smb://server/share/folder/'+destfile, os.O_CREAT | os.O_WRONLY)
- for line in source:
- file.write (line)
- file.close()
- source.close()
- # open a SMB/CIFS file and read it to a local file
- source = ctx.open ('smb://server/share/folder/'+destfile, os.O_RDONLY)
- destfile = "/home/myuser/textfile.txt"
- fle = open(destfile, 'w')
- for line in source:
- file.write (line)
- file.close()
- source.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement