A Software Technologist's Blog (Wanna Email Me?)

smb:// URL Support in pysmb

May 4, 2012 By miketeo

Starting from pysmb version 1.0.4, application developers can utilize smb:// URLs with pysmb and urllib2 python package. One benefit from this is that if your existing application already supports downloads via other protocols like http or ftp, you can “upgrade” your application to support downloads from Windows shared folders or from Samba servers by making small modifications to your existing code.

pysmb tries to follow the smb URL scheme proposed in the IETF draft which is not much different from other standard URL schemes like http and ftp. In short, your smb URL should follow this format – smb://[<user>:<password>@]<host>[:<port>][/<share>/<path>/<filename>]

Sample File Retrieving Code

The following code illustrates a simple file retrieval operation from the remote server at The userID and password are embedded in the URL and will be properly parsed by the SMBHandler for authentication with the remote server.

import urllib2
from smb.SMBHandler import SMBHandler 

director = urllib2.build_opener(SMBHandler)
fh = director.open('smb://myuserID:mypassword@') 

# Process fh like a file-like object and then close it.

The most important line is to register the SMBHandler class with the urllib2 library via the urllib2.build_opener() method. Note that there are other ways to register additional handlers, but this is the most straightforward method (in my opinion).

File Uploading to Remote Servers

In addition to file retrieval/download, pysmb also supports uploads to the remote servers. The following code example helps to illustrate a simple file upload operation.

import urllib2
from smb.SMBHandler import SMBHandler

file_fh = open('local_file.dat', 'rb')

director = urllib2.build_opener(SMBHandler)
fh = director.open('smb://myuserID:mypassword@',
                   data = file_fh)

# Reading from fh will only return an empty string

The URL used is similar to that used in the file retrieval operation. However, observe that the open() method accepts a data parameter pointing to an opened file object. With this parameter, pysmb will perform a file upload operation instead of the usual file retrieval operation.

I hope the above 2 examples provide a quick insight on how to use pysmb and urllib2 in your application. If you need more advanced operations like folder creation and file deletion, pysmb already has built-in support for these via the SMBConnection class (synchronous I/O implementation) and SMBProtocol class (Twisted protocol implementation).

  1. Gravar arquivos em share do samba autenticado | Fred Chevitarese ... Said,

    […] http://miketeo.net/wp/index.php/2012/05/04/smb-url-support-in-pysmb.html […]

  2. gowtham Said,

    please help me
    How to access windows xp Hidden share
    I tried
    director.open(smb://usrname@password/$,data = file_fh)
    c$ can’t working ,please help me urgent($ can’t work)
    Thank you..

  3. miketeo Said,

    The smb:// url can’t be used to list the files in a share or folder. You need to use the SMBConnection class.

    conn = SMBConnection('userID', 'password', 'client', 'server_name', use_ntlm_v2 = True)
    assert conn.connect('', 139)
    conn.listPath('C$', '/')

  4. pnedved Said,

    Hi mike, i need a bit of help !! on which conditions does the “retrieveFile” from the SMBconnection class fail ? I have 2000 files but i can only retrieve half of them.

  5. miketeo Said,

    @pnedved: There could be a few reasons. Have you verified if you have gotten the correct upper/lower case, whitespaces and unicode characters in the filenames? Sometimes, the spaces, commas and periods are not of ASCII characters, but are of unicode characters.

  6. pnedved Said,

    Hi mike thanks, will investigate that ! Do you have any idea on how to regulate bandwith access to smb ?

  7. miketeo Said,

    @pnedved: You can try to regulate at port level (TCP port 139 and 445). But if you are thinking of regulating according to users, I don’t think Sambda and Windows support that.

  8. pnedved Said,

    @miketeo: thanks for the help, but i can’t make it work. can you advise a library ΓΌ or an approach. Thanks

Add A Comment