Using pysmbc to read files over samba
Asked Answered
S

5

7

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.

Spangle answered 12/6, 2009 at 13:48 Comment(0)
W
12

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.

Wingo answered 12/6, 2009 at 22:56 Comment(4)
Hi sir, it's good module , but not working in Python 2.6.8 and belowInvincible
@RahulPatil how is it failing? I'm using it on python 2.4 and 2.5 here in production and it works. Can you open a issue at the issue tracker and describe the error you're getting here: bitbucket.org/nosklo/pysmbclient/issues/newWingo
dude your wrapper flipping rocks i love it awesome!Wildeyed
@Wingo can you give me access to your repository please?Blare
L
3

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
Laynelayney answered 9/7, 2012 at 10:27 Comment(1)
I just needed a simple thing and this is just it. Thank youVerlinevermeer
C
2

If don't know if this is more clearly stated, but here's what I gleaned from this page and sorted out from a little extra Google-ing:

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()
Conferral answered 10/6, 2016 at 19:6 Comment(1)
Can you explain more how this answers the question?Figurant
D
1

If you've managed to get the directory structure then you have a working context. The key to actually accessing files is the undocumented flags argument of Context.open. (I haven't figured out what mode is for either but it doesn't seem necessary.)

flags is how you tell pysmbc what type of access to the file you want. You do that by passing it an integer made by bitwise ORing (|) flags from the os module together. Specifically the flags you want or suffixed with os.O_ (see a list in the Python documentation here).

For example, to write to a file you would set flags to os.O_WRONLY (equiavlent to using "w" as the mode parameter of the built in open function) and to append to a file that might already exist use os.O_WRONLY | os.O_APPEND | os.O_CREAT (equivalent to "a+").

That call will then return a file object which you can use like any normal, local file.

Death answered 19/11, 2012 at 15:50 Comment(0)
A
-3

I would stick with smbfs. It's only a matter of time before you want to access those shared files with something other than Python.

Alvera answered 12/6, 2009 at 20:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.