我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用base.SMB。
def retrieveFile(self, 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*. Use *retrieveFileFromOffset()* method if you need to specify the offset to read from the remote *path* and/or the maximum number of bytes to write to the *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. :param string/unicode service_name: the name of the shared folder for the *path* :param string/unicode path: Path of the file on the remote server. If the file cannot be opened for reading, an :doc:`OperationFailure<smb_exceptions>` will be called in the returned *Deferred* errback. :param 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. :return: 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 written to *file_obj* ). The file attributes is an integer value made up from a bitwise-OR of *SMB_FILE_ATTRIBUTE_xxx* bits (see smb_constants.py) """ return self.retrieveFileFromOffset(service_name, path, file_obj, 0L, -1L, timeout)
def retrieveFileFromOffset(self, service_name, path, file_obj, offset = 0L, max_length = -1L, 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. :param string/unicode service_name: the name of the shared folder for the *path* :param string/unicode path: Path of the file on the remote server. If the file cannot be opened for reading, an :doc:`OperationFailure<smb_exceptions>` will be called in the returned *Deferred* errback. :param 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. :param integer/long offset: the offset in the remote *path* where the first byte will be read and written to *file_obj*. Must be either zero or a positive integer/long value. :param integer/long max_length: maximum number of bytes to read from the remote *path* and write to the *file_obj*. Specify a negative value to read from *offset* to the EOF. If zero, the *Deferred* callback is invoked immediately after the file is opened successfully for reading. :return: 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 written to *file_obj* ). The file attributes is an integer value made up from a bitwise-OR of *SMB_FILE_ATTRIBUTE_xxx* bits (see smb_constants.py) """ if not self.instance: raise NotConnectedError('Not connected to server') d = defer.Deferred() self.instance._retrieveFileFromOffset(service_name, path, file_obj, d.callback, d.errback, offset, max_length, timeout = timeout) return d
def storeFile(self, 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. :param string/unicode service_name: the name of the shared folder for the *path* :param string/unicode path: 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 :doc:`OperationFailure<smb_exceptions>` will be called in the returned *Deferred* errback. :param file_obj: A file-like object that has a *read* method. Data will read continuously from *file_obj* until EOF. :return: A *twisted.internet.defer.Deferred* instance. The callback function will be called with a 2-element tuple of ( *file_obj*, number of bytes uploaded ). """ if not self.instance: raise NotConnectedError('Not connected to server') d = defer.Deferred() self.instance._storeFile(service_name, path, file_obj, d.callback, d.errback, timeout = timeout) return d
def dataReceived(self, data): self.feedData(data) # # SMB (and its superclass) Methods #
def __init__(self, username, password, my_name, remote_name, domain = '', use_ntlm_v2 = True, sign_options = SIGN_WHEN_REQUIRED, is_direct_tcp = False): """ 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 using NetBIOS over TCP/IP is 139. Some newer server installations might also support Direct hosting of SMB over TCP/IP; for these servers, the default TCP port is 445. *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. :param string my_name: 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 ``\/:*?";|+``. :param string remote_name: 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. :param string domain: The network domain. On windows, it is known as the workgroup. Usually, it is safe to leave this parameter as an empty string. :param boolean use_ntlm_v2: 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. :param int sign_options: Determines whether SMB messages will be signed. Default is *SIGN_WHEN_REQUIRED*. If *SIGN_WHEN_REQUIRED* (value=2), SMB messages will only be signed when remote server requires signing. If *SIGN_WHEN_SUPPORTED* (value=1), SMB messages will be signed when remote server supports signing but not requires signing. If *SIGN_NEVER* (value=0), SMB messages will never be signed regardless of remote server's configurations; access errors will occur if the remote server requires signing. :param boolean is_direct_tcp: Controls whether the NetBIOS over TCP/IP (is_direct_tcp=False) or the newer Direct hosting of SMB over TCP/IP (is_direct_tcp=True) will be used for the communication. The default parameter is False which will use NetBIOS over TCP/IP for wider compatibility (TCP port: 139). """ self.username = username self.password = password self.my_name = my_name self.remote_name = remote_name self.domain = domain self.use_ntlm_v2 = use_ntlm_v2 self.sign_options = sign_options self.is_direct_tcp = is_direct_tcp self.instance = None #: The single SMBProtocol instance for each SMBProtocolFactory instance. Usually, you should not need to touch this attribute directly. # # Public Property #
def isReady(self): """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.""" return bool(self.instance and self.instance.has_authenticated)
def isUsingSMB2(self): """A convenient property to return True if the underlying SMB connection is using SMB2 protocol.""" return self.instance and self.instance.is_using_smb2 # # Public Methods for Callbacks #
def onAuthOK(self): """ 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. """ pass
def echo(self, 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*. :param string data: Data to send to the remote server. :param integer/float timeout: Number of seconds that pysmb will wait before raising *SMBTimeout* via the returned *Deferred* instance's *errback* method. :return: A *twisted.internet.defer.Deferred* instance. The callback function will be called with the *data* parameter. """ if not self.instance: raise NotConnectedError('Not connected to server') d = defer.Deferred() self.instance._echo(data, d.callback, d.errback, timeout) return d
def closeConnection(self): """ Disconnect from the remote SMB/CIFS server. The TCP connection will be closed at the earliest opportunity after this method returns. :return: None """ if not self.instance: raise NotConnectedError('Not connected to server') self.instance.transport.loseConnection() # # ClientFactory methods # (Do not touch these unless you know what you are doing) #