Table Of Contents

Previous topic


Next topic






Detailed Description

The PySide.QtNetwork.QLocalSocket class provides a local socket.

On Windows this is a named pipe and on Unix this is a local domain socket.

If an error occurs, socketError() returns the type of error, and PySide.QtCore.QIODevice.errorString() can be called to get a human readable description of what happened.

Although PySide.QtNetwork.QLocalSocket is designed for use with an event loop, it’s possible to use it without one. In that case, you must use PySide.QtNetwork.QLocalSocket.waitForConnected() , PySide.QtNetwork.QLocalSocket.waitForReadyRead() , PySide.QtNetwork.QLocalSocket.waitForBytesWritten() , and PySide.QtNetwork.QLocalSocket.waitForDisconnected() which blocks until the operation is complete or the timeout expires.

Note that this feature is not supported on versions of Windows earlier than Windows XP.

class PySide.QtNetwork.QLocalSocket([parent=None])

Creates a new local socket. The parent argument is passed to PySide.QtCore.QObject ‘s constructor.


This enum describes the different states in which a socket can be.

Constant Description
QLocalSocket.UnconnectedState The socket is not connected.
QLocalSocket.ConnectingState The socket has started establishing a connection.
QLocalSocket.ConnectedState A connection is established.
QLocalSocket.ClosingState The socket is about to close (data may still be waiting to be written).

The LocalServerError enumeration represents the errors that can occur. The most recent error can be retrieved through a call to QLocalSocket.error() .

Constant Description
QLocalSocket.ConnectionRefusedError The connection was refused by the peer (or timed out).
QLocalSocket.PeerClosedError The remote socket closed the connection. Note that the client socket (i.e., this socket) will be closed after the remote close notification has been sent.
QLocalSocket.ServerNotFoundError The local socket name was not found.
QLocalSocket.SocketAccessError The socket operation failed because the application lacked the required privileges.
QLocalSocket.SocketResourceError The local system ran out of resources (e.g., too many sockets).
QLocalSocket.SocketTimeoutError The socket operation timed out.
QLocalSocket.DatagramTooLargeError The datagram was larger than the operating system’s limit (which can be as low as 8192 bytes).
QLocalSocket.ConnectionError An error occurred with the connection.
QLocalSocket.UnsupportedSocketOperationError The requested socket operation is not supported by the local operating system.
QLocalSocket.UnknownSocketError An unidentified error occurred.

Aborts the current connection and resets the socket. Unlike PySide.QtNetwork.QLocalSocket.disconnectFromServer() , this function immediately closes the socket, clearing any pending data in the write buffer.

See also

PySide.QtNetwork.QLocalSocket.disconnectFromServer() PySide.QtNetwork.QLocalSocket.close()

PySide.QtNetwork.QLocalSocket.connectToServer(name[, openMode=QIODevice.ReadWrite])
  • name – unicode
  • openModePySide.QtCore.QIODevice.OpenMode

Attempts to close the socket. If there is pending data waiting to be written, PySide.QtNetwork.QLocalSocket will enter ClosingState and wait until all data has been written. Eventually, it will enter UnconnectedState and emit the disconnectedFromServer() signal.

Return type:PySide.QtNetwork.QLocalSocket.LocalSocketError

Returns the type of error that last occurred.

See also

PySide.QtNetwork.QLocalSocket.state() PySide.QtCore.QIODevice.errorString()

Return type:PySide.QtCore.bool

This function writes as much as possible from the internal write buffer to the socket, without blocking. If any data was written, this function returns true; otherwise false is returned.

Call this function if you need PySide.QtNetwork.QLocalSocket to start sending buffered data immediately. The number of bytes successfully written depends on the operating system. In most cases, you do not need to call this function, because PySide.QtNetwork.QLocalSocket will start sending data automatically once control goes back to the event loop. In the absence of an event loop, call PySide.QtNetwork.QLocalSocket.waitForBytesWritten() instead.

See also

PySide.QtCore.QIODevice.write() PySide.QtNetwork.QLocalSocket.waitForBytesWritten()

Return type:unicode

Returns the server path that the socket is connected to.


The return value of this function is platform specific.

Return type:PySide.QtCore.bool

Returns true if the socket is valid and ready for use; otherwise returns false.


The socket’s state must be ConnectedState before reading and writing can occur.

Return type:PySide.QtCore.qint64

Returns the size of the internal read buffer. This limits the amount of data that the client can receive before you call or PySide.QtCore.QIODevice.readAll() . A read buffer size of 0 (the default) means that the buffer has no size limit, ensuring that no data is lost.

See also


Return type:unicode

Returns the name of the peer as specified by PySide.QtNetwork.QLocalSocket.connectToServer() , or an empty PySide.QtCore.QString if PySide.QtNetwork.QLocalSocket.connectToServer() has not been called or it failed.


Sets the size of PySide.QtNetwork.QLocalSocket ‘s internal read buffer to be size bytes.

If the buffer size is limited to a certain size, PySide.QtNetwork.QLocalSocket won’t buffer more than this size of data. Exceptionally, a buffer size of 0 means that the read buffer is unlimited and all incoming data is buffered. This is the default.

This option is useful if you only read the data at certain points in time (e.g., in a real-time streaming application) or if you want to protect your socket against receiving too much data, which may eventually cause your application to run out of memory.

See also


Return type:PySide.QtNetwork.QLocalSocket.LocalSocketState

Returns the state of the socket.

Return type:PySide.QtCore.bool

Waits until the socket is connected, up to msecs milliseconds. If the connection has been established, this function returns true; otherwise it returns false. In the case where it returns false, you can call PySide.QtNetwork.QLocalSocket.error() to determine the cause of the error.

The following example waits up to one second for a connection to be established:

if (socket.waitForConnected(1000))

If msecs is -1, this function will not time out.

Return type:PySide.QtCore.bool

Waits until the socket has disconnected, up to msecs milliseconds. If the connection has been disconnected, this function returns true; otherwise it returns false. In the case where it returns false, you can call PySide.QtNetwork.QLocalSocket.error() to determine the cause of the error.

The following example waits up to one second for a connection to be closed:

if (socket.waitForDisconnected(1000))

If msecs is -1, this function will not time out.

See also

PySide.QtNetwork.QLocalSocket.disconnectFromServer() PySide.QtNetwork.QLocalSocket.close()