Table Of Contents

Previous topic

QHttp

Next topic

QAbstractNetworkCache

QFtp

Synopsis

Functions

Slots

Signals

Detailed Description

The PySide.QtNetwork.QFtp class provides an implementation of the client side of FTP protocol.

This class provides a direct interface to FTP that allows you to have more control over the requests. However, for new applications, it is recommended to use PySide.QtNetwork.QNetworkAccessManager and PySide.QtNetwork.QNetworkReply , as those classes possess a simpler, yet more powerful API.

The class works asynchronously, so there are no blocking functions. If an operation cannot be executed immediately, the function will still return straight away and the operation will be scheduled for later execution. The results of scheduled operations are reported via signals. This approach depends on the event loop being in operation.

The operations that can be scheduled (they are called “commands” in the rest of the documentation) are the following: PySide.QtNetwork.QFtp.connectToHost() , PySide.QtNetwork.QFtp.login() , PySide.QtNetwork.QFtp.close() , PySide.QtNetwork.QFtp.list() , PySide.QtNetwork.QFtp.cd() , PySide.QtNetwork.QFtp.get() , PySide.QtNetwork.QFtp.put() , PySide.QtNetwork.QFtp.remove() , PySide.QtNetwork.QFtp.mkdir() , PySide.QtNetwork.QFtp.rmdir() , PySide.QtNetwork.QFtp.rename() and PySide.QtNetwork.QFtp.rawCommand() .

All of these commands return a unique identifier that allows you to keep track of the command that is currently being executed. When the execution of a command starts, the PySide.QtNetwork.QFtp.commandStarted() signal with the command’s identifier is emitted. When the command is finished, the PySide.QtNetwork.QFtp.commandFinished() signal is emitted with the command’s identifier and a bool that indicates whether the command finished with an error.

In some cases, you might want to execute a sequence of commands, e.g. if you want to connect and login to a FTP server. This is simply achieved:

ftp = QFtp(parent)
ftp.connectToHost("ftp.trolltech.com")
ftp.login()

In this case two FTP commands have been scheduled. When the last scheduled command has finished, a PySide.QtNetwork.QFtp.done() signal is emitted with a bool argument that tells you whether the sequence finished with an error.

If an error occurs during the execution of one of the commands in a sequence of commands, all the pending commands (i.e. scheduled, but not yet executed commands) are cleared and no signals are emitted for them.

Some commands, e.g. PySide.QtNetwork.QFtp.list() , emit additional signals to report their results.

Example: If you want to download the INSTALL file from the Qt FTP server, you would write this:

ftp.connectToHost("ftp.trolltech.com") # id == 1
ftp.login()                            # id == 2
ftp.cd("qt")                           # id == 3
ftp.get("INSTALL")                     # id == 4
ftp.close()                            # id == 5

For this example the following sequence of signals is emitted (with small variations, depending on network traffic, etc.):

start(1)
stateChanged(HostLookup)
stateChanged(Connecting)
stateChanged(Connected)
finished(1, false)

start(2)
stateChanged(LoggedIn)
finished(2, false)

start(3)
finished(3, false)

start(4)
dataTransferProgress(0, 3798)
dataTransferProgress(2896, 3798)
readyRead()
dataTransferProgress(3798, 3798)
readyRead()
finished(4, false)

start(5)
stateChanged(Closing)
stateChanged(Unconnected)
finished(5, false)

done(false)

The PySide.QtNetwork.QFtp.dataTransferProgress() signal in the above example is useful if you want to show a progress bar to inform the user about the progress of the download. The PySide.QtNetwork.QFtp.readyRead() signal tells you that there is data ready to be read. The amount of data can be queried then with the PySide.QtNetwork.QFtp.bytesAvailable() function and it can be read with the PySide.QtNetwork.QFtp.read() or PySide.QtNetwork.QFtp.readAll() function.

If the login fails for the above example, the signals would look like this:

start(1)
stateChanged(HostLookup)
stateChanged(Connecting)
stateChanged(Connected)
finished(1, false)

start(2)
finished(2, true)

done(true)

You can then get details about the error with the PySide.QtNetwork.QFtp.error() and PySide.QtNetwork.QFtp.errorString() functions.

For file transfer, PySide.QtNetwork.QFtp can use both active or passive mode, and it uses passive file transfer mode by default; see the documentation for PySide.QtNetwork.QFtp.setTransferMode() for more details about this.

Call PySide.QtNetwork.QFtp.setProxy() to make PySide.QtNetwork.QFtp connect via an FTP proxy server.

The functions PySide.QtNetwork.QFtp.currentId() and PySide.QtNetwork.QFtp.currentCommand() provide more information about the currently executing command.

The functions PySide.QtNetwork.QFtp.hasPendingCommands() and PySide.QtNetwork.QFtp.clearPendingCommands() allow you to query and clear the list of pending commands.

If you are an experienced network programmer and want to have complete control you can use PySide.QtNetwork.QFtp.rawCommand() to execute arbitrary FTP commands.

Warning

The current version of PySide.QtNetwork.QFtp doesn’t fully support non-Unix FTP servers.

class PySide.QtNetwork.QFtp([parent=None])
Parameters:parentPySide.QtCore.QObject

Constructs a PySide.QtNetwork.QFtp object with the given parent .

PySide.QtNetwork.QFtp.TransferType

This enum identifies the data transfer type used with get and put commands.

Constant Description
QFtp.Binary The data will be transferred in Binary mode.
QFtp.Ascii The data will be transferred in Ascii mode and new line characters will be converted to the local format.
PySide.QtNetwork.QFtp.Error

This enum identifies the error that occurred.

Constant Description
QFtp.NoError No error occurred.
QFtp.HostNotFound The host name lookup failed.
QFtp.ConnectionRefused The server refused the connection.
QFtp.NotConnected Tried to send a command, but there is no connection to a server.
QFtp.UnknownError An error other than those specified above occurred.
PySide.QtNetwork.QFtp.Command

This enum is used as the return value for the PySide.QtNetwork.QFtp.currentCommand() function. This allows you to perform specific actions for particular commands, e.g. in a FTP client, you might want to clear the directory view when a PySide.QtNetwork.QFtp.list() command is started; in this case you can simply check in the slot connected to the start() signal if the PySide.QtNetwork.QFtp.currentCommand() is List .

Constant Description
QFtp.None No command is being executed.
QFtp.SetTransferMode set the transfer mode.
QFtp.SetProxy switch proxying on or off.
QFtp.ConnectToHost PySide.QtNetwork.QFtp.connectToHost() is being executed.
QFtp.Login PySide.QtNetwork.QFtp.login() is being executed.
QFtp.Close PySide.QtNetwork.QFtp.close() is being executed.
QFtp.List PySide.QtNetwork.QFtp.list() is being executed.
QFtp.Cd PySide.QtNetwork.QFtp.cd() is being executed.
QFtp.Get PySide.QtNetwork.QFtp.get() is being executed.
QFtp.Put PySide.QtNetwork.QFtp.put() is being executed.
QFtp.Remove PySide.QtNetwork.QFtp.remove() is being executed.
QFtp.Mkdir PySide.QtNetwork.QFtp.mkdir() is being executed.
QFtp.Rmdir PySide.QtNetwork.QFtp.rmdir() is being executed.
QFtp.Rename PySide.QtNetwork.QFtp.rename() is being executed.
QFtp.RawCommand PySide.QtNetwork.QFtp.rawCommand() is being executed.
PySide.QtNetwork.QFtp.TransferMode

FTP works with two socket connections; one for commands and another for transmitting data. While the command connection is always initiated by the client, the second connection can be initiated by either the client or the server.

This enum defines whether the client (Passive mode) or the server (Active mode) should set up the data connection.

Constant Description
QFtp.Passive The client connects to the server to transmit its data.
QFtp.Active The server connects to the client to transmit its data.
PySide.QtNetwork.QFtp.State

This enum defines the connection state:

Constant Description
QFtp.Unconnected There is no connection to the host.
QFtp.HostLookup A host name lookup is in progress.
QFtp.Connecting An attempt to connect to the host is in progress.
QFtp.Connected Connection to the host has been achieved.
QFtp.LoggedIn Connection and user login have been achieved.
QFtp.Closing The connection is closing down, but it is not yet closed. (The state will be Unconnected when the connection is closed.)
PySide.QtNetwork.QFtp.abort()

Aborts the current command and deletes all scheduled commands.

If there is an unfinished command (i.e. a command for which the PySide.QtNetwork.QFtp.commandStarted() signal has been emitted, but for which the PySide.QtNetwork.QFtp.commandFinished() signal has not been emitted), this function sends an ABORT command to the server. When the server replies that the command is aborted, the PySide.QtNetwork.QFtp.commandFinished() signal with the error argument set to true is emitted for the command. Due to timing issues, it is possible that the command had already finished before the abort request reached the server, in which case, the PySide.QtNetwork.QFtp.commandFinished() signal is emitted with the error argument set to false .

For all other commands that are affected by the PySide.QtNetwork.QFtp.abort() , no signals are emitted.

If you don’t start further FTP commands directly after the PySide.QtNetwork.QFtp.abort() , there won’t be any scheduled commands and the PySide.QtNetwork.QFtp.done() signal is emitted.

Warning

Some FTP servers, for example the BSD FTP daemon (version 0.3), wrongly return a positive reply even when an abort has occurred. For these servers the PySide.QtNetwork.QFtp.commandFinished() signal has its error flag set to false , even though the command did not complete successfully.

PySide.QtNetwork.QFtp.bytesAvailable()
Return type:PySide.QtCore.qint64

Returns the number of bytes that can be read from the data socket at the moment.

PySide.QtNetwork.QFtp.cd(dir)
Parameters:dir – unicode
Return type:PySide.QtCore.int

Changes the working directory of the server to dir .

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by PySide.QtNetwork.QFtp.commandStarted() and PySide.QtNetwork.QFtp.commandFinished() .

When the command is started the PySide.QtNetwork.QFtp.commandStarted() signal is emitted. When it is finished the PySide.QtNetwork.QFtp.commandFinished() signal is emitted.

PySide.QtNetwork.QFtp.clearPendingCommands()

Deletes all pending commands from the list of scheduled commands. This does not affect the command that is being executed. If you want to stop this as well, use PySide.QtNetwork.QFtp.abort() .

PySide.QtNetwork.QFtp.close()
Return type:PySide.QtCore.int

Closes the connection to the FTP server.

The PySide.QtNetwork.QFtp.stateChanged() signal is emitted when the state of the connecting process changes, e.g. to Closing , then Unconnected .

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by PySide.QtNetwork.QFtp.commandStarted() and PySide.QtNetwork.QFtp.commandFinished() .

When the command is started the PySide.QtNetwork.QFtp.commandStarted() signal is emitted. When it is finished the PySide.QtNetwork.QFtp.commandFinished() signal is emitted.

PySide.QtNetwork.QFtp.commandFinished(arg__1, arg__2)
Parameters:
  • arg__1PySide.QtCore.int
  • arg__2PySide.QtCore.bool
PySide.QtNetwork.QFtp.commandStarted(arg__1)
Parameters:arg__1PySide.QtCore.int
PySide.QtNetwork.QFtp.connectToHost(host[, port=21])
Parameters:
  • host – unicode
  • portPySide.QtCore.quint16
Return type:

PySide.QtCore.int

Connects to the FTP server host using port port .

The PySide.QtNetwork.QFtp.stateChanged() signal is emitted when the state of the connecting process changes, e.g. to HostLookup , then Connecting , then Connected .

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by PySide.QtNetwork.QFtp.commandStarted() and PySide.QtNetwork.QFtp.commandFinished() .

When the command is started the PySide.QtNetwork.QFtp.commandStarted() signal is emitted. When it is finished the PySide.QtNetwork.QFtp.commandFinished() signal is emitted.

PySide.QtNetwork.QFtp.currentCommand()
Return type:PySide.QtNetwork.QFtp.Command

Returns the command type of the FTP command being executed or None if there is no command being executed.

PySide.QtNetwork.QFtp.currentDevice()
Return type:PySide.QtCore.QIODevice

Returns the PySide.QtCore.QIODevice pointer that is used by the FTP command to read data from or store data to. If there is no current FTP command being executed or if the command does not use an IO device, this function returns 0.

This function can be used to delete the PySide.QtCore.QIODevice in the slot connected to the PySide.QtNetwork.QFtp.commandFinished() signal.

PySide.QtNetwork.QFtp.currentId()
Return type:PySide.QtCore.int

Returns the identifier of the FTP command that is being executed or 0 if there is no command being executed.

PySide.QtNetwork.QFtp.dataTransferProgress(arg__1, arg__2)
Parameters:
  • arg__1PySide.QtCore.qint64
  • arg__2PySide.QtCore.qint64
PySide.QtNetwork.QFtp.done(arg__1)
Parameters:arg__1PySide.QtCore.bool
PySide.QtNetwork.QFtp.error()
Return type:PySide.QtNetwork.QFtp.Error

Returns the last error that occurred. This is useful to find out what went wrong when receiving a PySide.QtNetwork.QFtp.commandFinished() or a PySide.QtNetwork.QFtp.done() signal with the error argument set to true .

If you start a new command, the error status is reset to NoError .

PySide.QtNetwork.QFtp.errorString()
Return type:unicode

Returns a human-readable description of the last error that occurred. This is useful for presenting a error message to the user when receiving a PySide.QtNetwork.QFtp.commandFinished() or a PySide.QtNetwork.QFtp.done() signal with the error argument set to true .

The error string is often (but not always) the reply from the server, so it is not always possible to translate the string. If the message comes from Qt, the string has already passed through tr() .

PySide.QtNetwork.QFtp.get(file[, dev=None[, type=Binary]])
Parameters:
Return type:

PySide.QtCore.int

Downloads the file file from the server.

If dev is 0, then the PySide.QtNetwork.QFtp.readyRead() signal is emitted when there is data available to read. You can then read the data with the PySide.QtNetwork.QFtp.read() or PySide.QtNetwork.QFtp.readAll() functions.

If dev is not 0, the data is written directly to the device dev . Make sure that the dev pointer is valid for the duration of the operation (it is safe to delete it when the PySide.QtNetwork.QFtp.commandFinished() signal is emitted). In this case the PySide.QtNetwork.QFtp.readyRead() signal is not emitted and you cannot read data with the PySide.QtNetwork.QFtp.read() or PySide.QtNetwork.QFtp.readAll() functions.

If you don’t read the data immediately it becomes available, i.e. when the PySide.QtNetwork.QFtp.readyRead() signal is emitted, it is still available until the next command is started.

For example, if you want to present the data to the user as soon as there is something available, connect to the PySide.QtNetwork.QFtp.readyRead() signal and read the data immediately. On the other hand, if you only want to work with the complete data, you can connect to the PySide.QtNetwork.QFtp.commandFinished() signal and read the data when the PySide.QtNetwork.QFtp.get() command is finished.

The data is transferred as Binary or Ascii depending on the value of type .

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by PySide.QtNetwork.QFtp.commandStarted() and PySide.QtNetwork.QFtp.commandFinished() .

When the command is started the PySide.QtNetwork.QFtp.commandStarted() signal is emitted. When it is finished the PySide.QtNetwork.QFtp.commandFinished() signal is emitted.

PySide.QtNetwork.QFtp.commandFinished()

PySide.QtNetwork.QFtp.hasPendingCommands()
Return type:PySide.QtCore.bool

Returns true if there are any commands scheduled that have not yet been executed; otherwise returns false.

The command that is being executed is not considered as a scheduled command.

PySide.QtNetwork.QFtp.list([dir=""])
Parameters:dir – unicode
Return type:PySide.QtCore.int

Lists the contents of directory dir on the FTP server. If dir is empty, it lists the contents of the current directory.

The PySide.QtNetwork.QFtp.listInfo() signal is emitted for each directory entry found.

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by PySide.QtNetwork.QFtp.commandStarted() and PySide.QtNetwork.QFtp.commandFinished() .

When the command is started the PySide.QtNetwork.QFtp.commandStarted() signal is emitted. When it is finished the PySide.QtNetwork.QFtp.commandFinished() signal is emitted.

PySide.QtNetwork.QFtp.listInfo(arg__1)
Parameters:arg__1PySide.QtNetwork.QUrlInfo
PySide.QtNetwork.QFtp.login([user=""[, password=""]])
Parameters:
  • user – unicode
  • password – unicode
Return type:

PySide.QtCore.int

Logs in to the FTP server with the username user and the password password .

The PySide.QtNetwork.QFtp.stateChanged() signal is emitted when the state of the connecting process changes, e.g. to LoggedIn .

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by PySide.QtNetwork.QFtp.commandStarted() and PySide.QtNetwork.QFtp.commandFinished() .

When the command is started the PySide.QtNetwork.QFtp.commandStarted() signal is emitted. When it is finished the PySide.QtNetwork.QFtp.commandFinished() signal is emitted.

PySide.QtNetwork.QFtp.mkdir(dir)
Parameters:dir – unicode
Return type:PySide.QtCore.int

Creates a directory called dir on the server.

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by PySide.QtNetwork.QFtp.commandStarted() and PySide.QtNetwork.QFtp.commandFinished() .

When the command is started the PySide.QtNetwork.QFtp.commandStarted() signal is emitted. When it is finished the PySide.QtNetwork.QFtp.commandFinished() signal is emitted.

PySide.QtNetwork.QFtp.put(data, file[, type=Binary])
Parameters:
Return type:

PySide.QtCore.int

This is an overloaded function.

Writes a copy of the given data to the file called file on the server. The progress of the upload is reported by the PySide.QtNetwork.QFtp.dataTransferProgress() signal.

The data is transferred as Binary or Ascii depending on the value of type .

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by PySide.QtNetwork.QFtp.commandStarted() and PySide.QtNetwork.QFtp.commandFinished() .

When the command is started the PySide.QtNetwork.QFtp.commandStarted() signal is emitted. When it is finished the PySide.QtNetwork.QFtp.commandFinished() signal is emitted.

Since this function takes a copy of the data , you can discard your own copy when this function returns.

PySide.QtNetwork.QFtp.put(dev, file[, type=Binary])
Parameters:
Return type:

PySide.QtCore.int

Reads the data from the IO device dev , and writes it to the file called file on the server. The data is read in chunks from the IO device, so this overload allows you to transmit large amounts of data without the need to read all the data into memory at once.

The data is transferred as Binary or Ascii depending on the value of type .

Make sure that the dev pointer is valid for the duration of the operation (it is safe to delete it when the PySide.QtNetwork.QFtp.commandFinished() is emitted).

PySide.QtNetwork.QFtp.rawCommand(command)
Parameters:command – unicode
Return type:PySide.QtCore.int

Sends the raw FTP command command to the FTP server. This is useful for low-level FTP access. If the operation you wish to perform has an equivalent PySide.QtNetwork.QFtp function, we recommend using the function instead of raw FTP commands since the functions are easier and safer.

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by PySide.QtNetwork.QFtp.commandStarted() and PySide.QtNetwork.QFtp.commandFinished() .

When the command is started the PySide.QtNetwork.QFtp.commandStarted() signal is emitted. When it is finished the PySide.QtNetwork.QFtp.commandFinished() signal is emitted.

PySide.QtNetwork.QFtp.rawCommandReply(arg__1, arg__2)
Parameters:
  • arg__1PySide.QtCore.int
  • arg__2 – unicode
PySide.QtNetwork.QFtp.read(maxlen)
Parameters:maxlenPySide.QtCore.qint64
Return type:QByteArray

Reads maxlen bytes from the data socket into data and returns the number of bytes read. Returns -1 if an error occurred.

PySide.QtNetwork.QFtp.readAll()
Return type:PySide.QtCore.QByteArray

Reads all the bytes available from the data socket and returns them.

PySide.QtNetwork.QFtp.readyRead()
PySide.QtNetwork.QFtp.remove(file)
Parameters:file – unicode
Return type:PySide.QtCore.int

Deletes the file called file from the server.

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by PySide.QtNetwork.QFtp.commandStarted() and PySide.QtNetwork.QFtp.commandFinished() .

When the command is started the PySide.QtNetwork.QFtp.commandStarted() signal is emitted. When it is finished the PySide.QtNetwork.QFtp.commandFinished() signal is emitted.

PySide.QtNetwork.QFtp.rename(oldname, newname)
Parameters:
  • oldname – unicode
  • newname – unicode
Return type:

PySide.QtCore.int

Renames the file called oldname to newname on the server.

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by PySide.QtNetwork.QFtp.commandStarted() and PySide.QtNetwork.QFtp.commandFinished() .

When the command is started the PySide.QtNetwork.QFtp.commandStarted() signal is emitted. When it is finished the PySide.QtNetwork.QFtp.commandFinished() signal is emitted.

PySide.QtNetwork.QFtp.rmdir(dir)
Parameters:dir – unicode
Return type:PySide.QtCore.int

Removes the directory called dir from the server.

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by PySide.QtNetwork.QFtp.commandStarted() and PySide.QtNetwork.QFtp.commandFinished() .

When the command is started the PySide.QtNetwork.QFtp.commandStarted() signal is emitted. When it is finished the PySide.QtNetwork.QFtp.commandFinished() signal is emitted.

PySide.QtNetwork.QFtp.setProxy(host, port)
Parameters:
  • host – unicode
  • portPySide.QtCore.quint16
Return type:

PySide.QtCore.int

Enables use of the FTP proxy on host host and port port . Calling this function with host empty disables proxying.

PySide.QtNetwork.QFtp does not support FTP-over-HTTP proxy servers. Use PySide.QtNetwork.QNetworkAccessManager for this.

PySide.QtNetwork.QFtp.setTransferMode(mode)
Parameters:modePySide.QtNetwork.QFtp.TransferMode
Return type:PySide.QtCore.int

Sets the current FTP transfer mode to mode . The default is QFtp.Passive .

See also

QFtp.TransferMode

PySide.QtNetwork.QFtp.state()
Return type:PySide.QtNetwork.QFtp.State

Returns the current state of the object. When the state changes, the PySide.QtNetwork.QFtp.stateChanged() signal is emitted.

PySide.QtNetwork.QFtp.stateChanged(arg__1)
Parameters:arg__1PySide.QtCore.int