smb:// URL Support in pysmb

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://[:@][:][///]

Sample File Retrieving Code

The following code illustrates a simple file retrieval operation from the remote server at 192.168.1.1. 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@192.168.1.1/sharedfolder/rfc1001.txt') 

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

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@192.168.1.1/sharedfolder/upload_file.dat',
                   data = file_fh)

# Reading from fh will only return an empty string
fh.close()

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

pysmb 1.0.0 Released!

About 10 years ago, I have released the first version of pysmb. Since then, Microsoft Windows has made significant changes to the NTLM authentication protocol which causes pysmb to become incompatible with Windows Vista and Windows 7 machines.

Now, with the release of pysmb 1.0.0, you can write Python applications to transfer files on shared folders on Windows XP, Windows Vista and Windows 7, as well as on Unix machines running Samba. pysmb 1.0.0 remains faithful to its clause that it will remain a pure Python SMB/CIFS module without any need for binary compilation. For this to be possible, it has utilized other pure Python modules such as pyDES, MD4 and pyasn1.

In addition to its new compatibilities with the recent Windows OS, pysmb has also been rewritten to support other frameworks. At the moment, a Twisted NetBIOS and SMB/CIFS protocol implementation has been included with pysmb 1.0.0 sources. Please refer to the documentation in the sources for more details.

For more details on pysmb 1.0.0, please go to the pysmb project page to download it, or you can also run: "easy_install pysmb" to install it on your machine.

Python Mutant Tester 0.1.0 Released

I have just released Python Mutant Tester (PyMuTester) version 0.1.0 to the public. More details can be found at the Python Mutant Tester project page.

PyMuTester is a testing tool to facilitate mutant testing in Python applications. By making small (and syntactically correct) modifications to the application's source code and re-run the unit tests over these mutated source code, we can uncover missed checks and loopholes in the test cases.

Currently, PyMuTester is in alpha stage. It has only 2 mutators: If-Condition Negator and Loop Skipping.

  • The If-Condition negator works by negating the original result of the test clause in the if-statements. So, instead of executing the main body of the if-statement, the mutated code will execute the else body of if-statement, and vice versa.
  • The Loop Skipping mutator inserts a statement at the beginning of the bodies of the for- and while-loop code blocks. The inserted statement will skip the body of the loop code block on alternate iteration.

Future releases of PyMuTester may include support for more mutation algorithms, such as superclass mutation, arithmetric mutatioon and return value mutation. PyMuTester may also support other framework testing such as Django and Twisted. For instance, we can mutate Twisted application to skip reading a few bytes or to write a few more/less bytes. The test suite should arrest these corrupted I/O.