### Navigation

- index
- next |
- previous |
- pysmb 1.0.0 documentation »

# SMBProtocolFactory Class

For those who want to utilize pysmb in Twisted framework, pysmb has a smb.SMBProtocol.SMBProtocol implementation. In most cases, you do not need to touch or import the SMBProtocol directly. All the SMB functionalities are exposed in the SMBProtocolFactory.

In your project,
  1. Create a new class and subclass SMBProtocolFactory.
  2. Override the SMBProtocolFactory.onAuthOK and SMBProtocolFactory.onAuthFailed instance methods to provide your own post-authenthentication handling. Once SMBProtocolFactory.onAuthOK has been called by pymsb internals, your application is ready to communicate with the remote SMB/CIFS service through the SMBProtocolFactory public methods such as SMBProtocolFactory.storeFile, SMBProtocolFactory.retrieveFile, etc.
  3. When you want to disconnect from the remote SMB/CIFS server, just call SMBProtocolFactory.closeConnection method.

All the SMBProtocolFactory public methods that provide file functionlities will return a twisted.internet.defer.Deferred instance. A NotReadyError exception is raised when the underlying SMB is not authenticated. If the underlying SMB connection has been terminated, a NotConnectedError exception is raised.

All the file operation methods in SMBProtocolFactory class accept a timeout parameter. This parameter specifies the time limit where pysmb will wait for the entire file operation (except storeFile and retrieveFile methods) to complete. If the file operation fails to complete within the timeout period, the returned Deferred instance’s errback method will be called with a SMBTimeout exception.

If you are interested in learning the results of the operation or to know when the operation has completed, you should add a handling method to the returned Deferred instance via Deferred.addCallback. If the file operation fails, the Deferred.errback function will be called with an OperationFailure; on timeout, it will be called with a SMBTimeout.

## Caveats

- A new factory instance must be created for each SMB connection to the remote SMB/CIFS service. Avoid reusing the same factory instance for more than one SMB connection.
- The remote SMB/CIFS server usually imposes a limit of the number of concurrent file operations for each client. For example, to transfer a thousand files, you may need to setup a queue in your application and call storeFile or retrieveFile in batches.
- The timeout parameter in the file operation methods are not precise; it is accurate to within 1 second interval, i.e. with a timeout of 0.5 sec, pysmb might raise SMBTimeout exception after 1.5 sec.

class `smb.SMBProtocol.``SMBProtocolFactory`(username, password, my_name, remote_name, domain='', use_ntlm_v2=True)
  `__init__`(username, password, my_name, remote_name, domain='', use_ntlm_v2=True)

    Create a new SMBProtocolFactory instance. You will pass this instance to reactor.connectTCP() which will then instantiate the TCP connection to the remote SMB/CIFS server. Note that the default TCP port for most SMB/CIFS servers is 139.

    username and password are the user credentials required to authenticate the underlying SMB connection with the remote server. File operations can only be proceeded after the connection has been authenticated successfully.

    Parameters: | my_name (string) – The local NetBIOS machine name that will identify where this connection is originating from. You can freely choose a name as long as it contains a maximum of 15 alphanumeric characters and does not contain spaces and any of `\/:*?";|+` remote_name (string) – The NetBIOS machine name of the remote server. On windows, you can find out the machine name by right-clicking on the “My Computer” and selecting “Properties”. This parameter must be the same as what has been configured on the remote server, or else the connection will be rejected. domain (string) – The network domain. On windows, it is known as the workgroup. Usually, it is safe to leave this parameter as an empty string. use_ntlm_v2 (boolean) – Indicates whether pysmb should be NTLMv1 or NTLMv2 authentication algorithm for authentication. The choice of NTLMv1 and NTLMv2 is configured on the remote server, and there is no mechanism to auto-detect which algorithm has been configured. Hence, we can only “guess” or try both algorithms. On Sambda, Windows Vista and Windows 7, NTLMv2 is enabled by default. On Windows XP, we can use NTLMv1 before NTLMv2.

  `closeConnection`()

    Disconnect from the remote SMB/CIFS server. The TCP connection will be closed at the earliest opportunity after this method returns.

    Returns: | None

  `createDirectory`(service_name, path)

    Creates a new directory path on the service_name.

    Parameters: | service_name (string/unicode) – Contains the name of the shared folder. path (string/unicode) – The path of the new folder (relative to) the shared folder. If the path contains non-English characters, an unicode string must be used to pass in the path. timeout (integer/float) – Number of seconds that pysmb will wait before raising SMBTimeout via the returned Deferred instance’s errback method.
    Returns: | A twisted.internet.defer.Deferred instance. The callback function will be called with the path parameter.

  `deleteDirectory`(service_name, path)

    Delete the empty folder at path on service_name

    Parameters: | service_name (string/unicode) – Contains the name of the shared folder. path (string/unicode) – The path of the to-be-deleted folder (relative to) the shared folder. If the path contains non-English characters, an unicode string must be used to pass in the path. timeout (integer/float) – Number of seconds that pysmb will wait before raising SMBTimeout via the returned Deferred instance’s errback method.
    Returns: | A twisted.internet.defer.Deferred instance. The callback function will be called with the path parameter.

  `deleteFiles`(service_name, path_file_pattern, timeout=30)

    Delete one or more regular files. It supports the use of wildcards in file names, allowing for deletion of multiple files in a single request.

    Parameters: | service_name (string/unicode) – Contains the name of the shared folder. path_file_pattern (string/unicode) – The pathname of the file(s) to be deleted, relative to the service_name. Wildcards may be used in th filename component of the path. If your path/filename contains non-English characters, you must pass in an unicode string. timeout (integer/float) – Number of seconds that pysmb will wait before raising SMBTimeout via the returned Deferred instance’s errback method.
    Returns: | A twisted.internet.defer.Deferred instance. The callback function will be called with the path_file_pattern parameter.

  `echo`(data, timeout=10)

    Send an echo command containing data to the remote SMB/CIFS server. The remote SMB/CIFS will reply with the same data.

    Parameters: | data (string) – Data to send to the remote server. timeout (integer/float) – Number of seconds that pysmb will wait before raising SMBTimeout via the returned Deferred instance’s errback method.
    Returns: | A twisted.internet.defer.Deferred instance. The callback function will be called with the data parameter.

  `listPath`(service_name, path, search=55, pattern='*', timeout=30)

    Retrieve a directory listing of files/folders at path

    Parameters: | service_name (string/unicode) – the name of the shared folder for the path path (string/unicode) – path relative to the service_name where we are interested to learn about its files/sub-folders. search (integer) – integer value made up from a bitwise-OR of SMB_FILE_ATTRIBUTE_xxx bits (see smb_constants.py). The default search value will query for all read-only, hidden, system, archive files and directories. pattern (string/unicode) – the filter to apply to the results before returning to the client. timeout (integer/float) – Number of seconds that pysmb will wait before raising SMBTimeout via the returned Deferred instance’s errback method.
    Returns: | A twisted.internet.defer.Deferred instance. The callback function will be called with a list of smb.base.SharedFile instances.

  `listShares`(timeout=30)

    Retrieve a list of shared resources on remote server.

    Parameters: | timeout (integer/float) – Number of seconds that pysmb will wait before raising SMBTimeout via the returned Deferred instance’s errback method.
    Returns: | A twisted.internet.defer.Deferred instance. The callback function will be called with a list of smb.base.SharedDevice instances.

  `onAuthFailed`()

    Override this method in your SMBProtocolFactory subclass to add in post-authentication handling. This method will be called when the server has replied that the SMB connection has been successfully authenticated.

    If you want to retry authenticating from this method,
      1. Disconnect the underlying SMB connection (call `self.instance.transport.loseConnection()`)
      2. Create a new SMBProtocolFactory subclass instance with different user credientials or different NTLM algorithm flag.
      3. Call `reactor.connectTCP` with the new instance to re-establish the SMB connection
  `onAuthOK`()

    Override this method in your SMBProtocolFactory subclass to add in post-authentication handling. This method will be called when the server has replied that the SMB connection has been successfully authenticated. File operations can proceed when this method has been called.

  `rename`(service_name, old_path, new_path)

    Rename a file or folder at old_path to new_path shared at service_name. Note that this method cannot be used to rename file/folder across different shared folders

    old_path and new_path are string/unicode referring to the old and new path of the renamed resources (relative to) the shared folder. If the path contains non-English characters, an unicode string must be used to pass in the path.

    Parameters: | service_name (string/unicode) – Contains the name of the shared folder. timeout (integer/float) – Number of seconds that pysmb will wait before raising SMBTimeout via the returned Deferred instance’s errback method.
    Returns: | A twisted.internet.defer.Deferred instance. The callback function will be called with a 2-element tuple of ( old_path, new_path ).

  `retrieveFile`(service_name, path, file_obj, timeout=30)

    Retrieve the contents of the file at path on the service_name and write these contents to the provided file_obj.

    The meaning of the timeout parameter will be different from other file operation methods. As the downloaded file usually exceeeds the maximum size of each SMB/CIFS data message, it will be packetized into a series of request messages (each message will request about about 60kBytes). The timeout parameter is an integer/float value that specifies the timeout interval for these individual SMB/CIFS message to be transmitted and downloaded from the remote SMB/CIFS server.

    Parameters: | service_name (string/unicode) – the name of the shared folder for the path path (string/unicode) – Path of the file on the remote server. If the file cannot be opened for reading, an OperationFailure will be called in the returned Deferred errback. file_obj – A file-like object that has a write method. Data will be written continuously to file_obj until EOF is received from the remote service.
    Returns: | A twisted.internet.defer.Deferred instance. The callback function will be called with a 3-element tuple of ( file_obj, file attributes of the file on server, number of bytes retrieved ). The file attributes is an integer value made up from a bitwise-OR of SMB_FILE_ATTRIBUTE_xxx bits (see smb_constants.py)

  `storeFile`(service_name, path, file_obj, timeout=30)

    Store the contents of the file_obj at path on the service_name.

    The meaning of the timeout parameter will be different from other file operation methods. As the uploaded file usually exceeeds the maximum size of each SMB/CIFS data message, it will be packetized into a series of messages (usually about 60kBytes). The timeout parameter is an integer/float value that specifies the timeout interval for these individual SMB/CIFS message to be transmitted and acknowledged by the remote SMB/CIFS server.

    Parameters: | service_name (string/unicode) – the name of the shared folder for the path path (string/unicode) – Path of the file on the remote server. If the file at path does not exist, it will be created. Otherwise, it will be overwritten. If the path refers to a folder or the file cannot be opened for writing, an OperationFailure will be called in the returned Deferred errback. file_obj – A file-like object that has a read method. Data will read continuously from file_obj until EOF.
    Returns: | A twisted.internet.defer.Deferred instance. The callback function will be called with a 2-element tuple of ( file_obj, number of bytes uploaded ).

  `instance` = None

    The single SMBProtocol instance for each SMBProtocolFactory instance. Usually, you should not need to touch this attribute directly.

  `isReady`

    A convenient property to return True if the underlying SMB connection has connected to remote server, has successfully authenticated itself and is ready for file operations.

### Navigation

- index
- next |
- previous |
- pysmb 1.0.0 documentation »