Using pysmbc to read files over samba

Staale picture Staale · Jun 12, 2009 · Viewed 25.1k times · Source

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 content. The webpage (https://fedorahosted.org/pysmbc/) doesn't mention any thing, the code is in C/C++, with little documentation, so I am not quite sure how to use it.

What I know is that Context.open (for files) takes uri, flags and mode, but what flags and mode are, I don't know.

Has anyone used this library, or have examples off how to read files using it?

The ideal situation had of course been to use smbfs mounts, but when I mount the same share using smbmount, all folders are empty. Though I can browse it with smbclient fine using the same credentials.

Answer

nosklo picture nosklo · Jun 13, 2009

I also have had trouble using smbfs (random system lockdowns and reboots) and needed a quick answer.

I've also tried the smbc module but couldn't get any data with it. I went just as far as accessing the directory structure, just like you.

Time was up and I had to deliver the code, so I took a shortcut:

I wrote a small wrapper around a "smbclient" call. It is a hack, ugly, really ugly, but it works for my needs. It is being used in production on the company I work.

Here's some example usage:

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

The programmer before me was using a "bash" file with lots of smbclient calls, so I think my solution is at least better.

I have uploaded it here, so you can use it if you want. Bitbucket repository is here. If you find a better solution please tell me and I'll replace my code too.