access remote files on server with smb protocol python3

Question:

I have a remote server with some files.

smb://ftpsrv/public/

I can be authorized there as an anonymous user. In java I could simply write this code:

SmbFile root = new SmbFile(SMB_ROOT);

And get the ability to work with files inside (it is all I need, one row!), but I can’t find how to manage with this task in Python 3, there are a lot of resources, but I think they are not relevant to my problem, because they are frequently tailored for Python 2, and old other approaches. Is there some simple way, similar to Java code above?
Or can somebody provide a real working solution if, for example, I want to access file fgg.txt in smb://ftpsrv/public/ folder. Is there really a handy lib to tackle this problem?

For example on site:

import tempfile
from smb.SMBConnection import SMBConnection

# There will be some mechanism to capture userID, password, client_machine_name, server_name and server_ip
# client_machine_name can be an arbitary ASCII string
# server_name should match the remote machine name, or else the connection will be rejected
conn = SMBConnection(userID, password, client_machine_name, server_name, use_ntlm_v2 = True)
assert conn.connect(server_ip, 139)

file_obj = tempfile.NamedTemporaryFile()
file_attributes, filesize = conn.retrieveFile('smbtest', '/rfc1001.txt', file_obj)

# Retrieved file contents are inside file_obj
# Do what you need with the file_obj and then close it
# Note that the file obj is positioned at the end-of-file,
# so you might need to perform a file_obj.seek() if you need
# to read from the beginning
file_obj.close()

Do I seriously need to provide all of these details: conn = SMBConnection(userID, password, client_machine_name, server_name, use_ntlm_v2 = True)?

Asked By: Alex

||

Answers:

A simple example of opening a file using urllib and pysmb in Python 3

import urllib
from smb.SMBHandler import SMBHandler
opener = urllib.request.build_opener(SMBHandler)
fh = opener.open('smb://host/share/file.txt')
data = fh.read()
fh.close()

I haven’t got an anonymous SMB share ready to test it with, but this code should work.
urllib2 is the python 2 package, in python 3 it was renamed to just urllib and some stuff got moved around.

Answered By: Rick Rongen

I think you were asking for Linux, but for completeness I’ll share how it works on Windows.

On Windows, it seems that Samba access is supported out of the box with Python’s standard library functions:

import glob, os

with open(r'\USER1-PCUsersPublictest.txt', 'w') as f:
    f.write('hello')    # write a file on a distant Samba share

for f in glob.glob(r'\USER1-PCUsers***', recursive=True):
    print(f)   # glob works too
    if os.path.isfile(f):
        print(os.path.getmtime(f))  # we can get filesystem information
Answered By: Basj

i’m using dash web app, running on Linux.
What is the suggested function to use, to server windows & mac clients using web browser?

Answered By: Bambos
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.