Using pysmbc to read files over samba

后端 未结 5 2185
忘了有多久
忘了有多久 2020-12-30 13:12

I am using the python-smbc library on Ubuntu to access a samba share. I can access the directory structure fine, I am however not sure how to access actual files and their c

5条回答
  •  别那么骄傲
    2020-12-30 14:13

    Provided you have an open context (see the unit tests here)
    * https://github.com/ioggstream/pysmbc/tree/master/tests

    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()
    

    To open a context just define an authentication function

    ctx = smbc.Context()
    
    def auth_fn(server, share, workgroup, username, password):
        return (workgroup, settings.USERNAME, settings.PASSWORD)
    
    ctx.optionNoAutoAnonymousLogin = True
    ctx.functionAuthData = auth_fn
    

提交回复
热议问题