Downloading files with SMB

时间:2017-04-24 17:30:41

标签: python-2.7 smb

I'm trying to download a CSV through my network share using the pysmbclient module, but I'm getting an error (below). The server is Win2003 R2 (DFS), it's a IPC$ share. I'm able to authenticate OK and download the file through Windows Explorer on my main account. The only difference is my main account pulls it with "Trans 2 Request, QUERY_PATH_INFO".

Is there a "pure python" way of doing this over SMB?

import smbclient

userID = 'user'
password = 'password'
server_name = 'usa03'
ip = '10.1.13.211'

try:
    smb = smbclient.SambaClient(server=server_name, ip=ip, share="share", username=userID, password=password, domain='biz')
    f = smb.open('\sas\results\summary.csv')
    data = f.read()
    f.close()
except:
        print "No go"
smb.close()

SMB 286 Open AndX Request, Path: \sas\results\summary.csv

SMB 93 Open AndX Response, Error: STATUS_OBJECT_NAME_INVALID

1 个答案:

答案 0 :(得分:2)

Most likely the problem is that \r is being interpreted as a control character.

Change the path to:

f = smb.open('\\sas\\results\\summary.csv')

You should also, where possible, avoid except: and instead catch specific exceptions.

You could also use a finally: clause here:

try:
    smb = smbclient.SambaClient(server=server_name, ip=ip, share="share", 
                                username=userID, password=password, domain='biz')
    with smb.open('\\sas\\results\\summary.csv') as f:
        data = f.read()
except SomeError: # not literally SomeError but an error you might encounter
    print "No go"
finally:
    smb.close()