【问题标题】:Using pysmbc to read files over samba使用 pysmbc 通过 samba 读取文件
【发布时间】:2010-11-02 11:18:51
【问题描述】:

我在 Ubuntu 上使用 python-smbc 库来访问 samba 共享。我可以很好地访问目录结构,但是我不确定如何访问实际文件及其内容。网页(https://fedorahosted.org/pysmbc/)什么都没提,代码是C/C++,文档很少,所以不太清楚怎么用。

我知道的是 Context.open (for files) 需要 uri、flags 和 mode,但我不知道 flags 和 mode 是什么。

有没有人使用过这个库,或者有关于如何使用它读取文件的示例?

理想的情况当然是使用 smbfs 挂载,但是当我使用 smbmount 挂载同一个共享时,所有文件夹都是空的。虽然我可以使用相同的凭据通过 smbclient 浏览它。

【问题讨论】:

    标签: python samba


    【解决方案1】:

    我在使用 smbfs(随机系统锁定和重启)时也遇到了问题,需要快速解答。

    我也尝试过smbc 模块,但无法获得任何数据。我和你一样访问了目录结构。

    时间到了,我不得不交付代码,所以我走捷径:

    我为“smbclient”调用编写了一个小包装器。这是一个 hack,丑陋,真的很丑,但它可以满足我的需求。我工作的公司正在生产中使用它。

    以下是一些用法示例:

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

    我之前的程序员使用带有大量 smbclient 调用的“bash”文件, 所以我认为我的解决方案至少更好。

    我已经上传了here,所以你可以使用它。 Bitbucket 存储库是here。如果您找到更好的解决方案,请告诉我,我也会替换我的代码。

    【讨论】:

    • 您好先生,这是一个很好的模块,但不适用于 Python 2.6.8 及以下版本
    • @RahulPatil 它是如何失败的?我在生产中的 python 2.4 和 2.5 上使用它并且它可以工作。您能否在问题跟踪器上打开一个问题并描述您在此处遇到的错误:bitbucket.org/nosklo/pysmbclient/issues/new
    • 老兄,你的包装纸翻转石头,我喜欢它太棒了!
    【解决方案2】:

    如果您有一个开放的上下文(请参阅此处的单元测试)
    * 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()
    

    要打开一个上下文,只需定义一个身份验证函数

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

    【讨论】:

    • 我只需要一个简单的东西,仅此而已。谢谢
    【解决方案3】:

    如果不知道是否更清楚地说明了这一点,但这是我从该页面收集的内容并从一些额外的谷歌搜索中整理出来的:

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

    【讨论】:

    • 你能解释一下这如何回答这个问题吗?
    【解决方案4】:

    如果您设法获得了目录结构,那么您就有了一个工作上下文。实际访问文件的关键是Context.open 的未记录标志参数。 (我还没弄清楚两者的模式是什么,但似乎没有必要。)

    flags 是您告诉 pysmbc 对您想要的文件的访问类型的方式。您可以通过将 os 模块中的按位 ORing (|) 标志一起传递给它的整数来实现。特别是您想要的标志或以os.O_ 为后缀的标志(请参阅Python 文档here 中的列表)。

    例如,要写入文件,您可以将标志设置为 os.O_WRONLY(相当于使用 "w" 作为内置 open 函数的模式参数)并附加到可能已经存在的文件使用os.O_WRONLY | os.O_APPEND | os.O_CREAT(相当于"a+")。

    然后,该调用将返回一个 file 对象,您可以像使用任何普通的本地文件一样使用该对象。

    【讨论】:

      【解决方案5】:

      我会坚持使用 smbfs。您想要使用 Python 以外的其他工具访问这些共享文件只是时间问题。

      【讨论】:

        猜你喜欢
        • 2021-11-10
        • 1970-01-01
        • 2011-09-04
        • 2014-01-24
        • 1970-01-01
        • 2011-06-12
        • 1970-01-01
        • 2013-10-28
        • 1970-01-01
        相关资源
        最近更新 更多