- class QLocalServer¶
The
QLocalServer
class provides a local socket based server. More…Synopsis¶
Properties¶
socketOptionsᅟ
- The socket options that control how the socket operates
Methods¶
def
__init__()
def
close()
def
errorString()
def
fullServerName()
def
isListening()
def
listen()
def
serverError()
def
serverName()
def
socketOptions()
Virtual methods¶
Signals¶
def
newConnection()
Static functions¶
def
removeServer()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description¶
This class makes it possible to accept incoming local socket connections.
Call
listen()
to have the server start listening for incoming connections on a specified key. ThenewConnection()
signal is then emitted each time a client connects to the server.Call
nextPendingConnection()
to accept the pending connection as a connectedQLocalSocket
. The function returns a pointer to aQLocalSocket
that can be used for communicating with the client.If an error occurs,
serverError()
returns the type of error, anderrorString()
can be called to get a human readable description of what happened.When listening for connections, the name which the server is listening on is available through
serverName()
.Calling
close()
makesQLocalServer
stop listening for incoming connections.Although
QLocalServer
is designed for use with an event loop, it’s possible to use it without one. In that case, you must usewaitForNewConnection()
, which blocks until either a connection is available or a timeout expires.See also
- class SocketOption¶
(inherits
enum.Flag
) This enum describes the possible options that can be used to create the socket. This changes the access permissions on platforms (Linux, Windows) that support access permissions on the socket. Both GroupAccess and OtherAccess may vary slightly in meanings depending on the platform. On Linux and Android it is possible to use sockets with abstract addresses; socket permissions have no meaning for such sockets.Constant
Description
QLocalServer.NoOptions
No access restrictions have been set.
QLocalServer.UserAccessOption
Access is restricted to the same user as the process that created the socket.
QLocalServer.GroupAccessOption
Access is restricted to the same group but not the user that created the socket on Linux. Access is restricted to the primary group of the process on Windows
QLocalServer.OtherAccessOption
Access is available to everyone but the user and group that created the socket on Linux. Access is available to everyone on Windows.
QLocalServer.WorldAccessOption
No access restrictions.
QLocalServer.AbstractNamespaceOption
The listening socket will be created in the abstract namespace. This flag is specific to Linux. In case of other platforms, for the sake of code portability, this flag is equivalent to WorldAccessOption.
See also
Note
Properties can be used directly when
from __feature__ import true_property
is used or via accessor functions otherwise.- property socketOptionsᅟ: Combination of QLocalServer.SocketOption¶
This property holds the socket options that control how the socket operates..
For example, the socket may restrict access to what user ids can connect to the socket.
These options must be set before
listen()
is called.In some cases, such as with Unix domain sockets on Linux, the access to the socket will be determined by file system permissions, and are created based on the umask. Setting the access flags will override this and will restrict or permit access as specified.
Other Unix-based operating systems, such as macOS, do not honor file permissions for Unix domain sockets and by default have WorldAccess and these permission flags will have no effect.
On Windows,
UserAccessOption
is sufficient to allow a non elevated process to connect to a local server created by an elevated process run by the same user.GroupAccessOption
refers to the primary group of the process (see TokenPrimaryGroup in the Windows documentation).OtherAccessOption
refers to the well known “Everyone” group.On Linux platforms it is possible to create a socket in the abstract namespace, which is independent of the filesystem. Using this kind of socket implies ignoring permission options. On other platforms
AbstractNamespaceOption
is equivalent toWorldAccessOption
.By default none of the flags are set, access permissions are the platform default.
See also
- Access functions:
Create a new local socket server with the given
parent
.See also
- addPendingConnection(socket)¶
- Parameters:
socket –
QLocalSocket
This function is called by
incomingConnection()
to add thesocket
to the list of pending incoming connections.Note
Don’t forget to call this member from reimplemented
incomingConnection()
if you do not want to break the Pending Connections mechanism. This function emits thenewConnection()
signal after the socket has been added.See also
- close()¶
Stop listening for incoming connections. Existing connections are not affected, but any new connections will be refused.
See also
- errorString()¶
- Return type:
str
Returns the human-readable message appropriate to the current error reported by
serverError()
. If no suitable string is available, an empty string is returned.See also
- fullServerName()¶
- Return type:
str
Returns the full path that the server is listening on.
Note: This is platform specific
See also
- hasPendingConnections()¶
- Return type:
bool
Returns
true
if the server has a pending connection; otherwise returnsfalse
.- incomingConnection(socketDescriptor)¶
- Parameters:
socketDescriptor –
quintptr
This virtual function is called by
QLocalServer
when a new connection is available.socketDescriptor
is the native socket descriptor for the accepted connection.The base implementation creates a
QLocalSocket
, sets the socket descriptor and then stores theQLocalSocket
in an internal list of pending connections. FinallynewConnection()
is emitted.Reimplement this function to alter the server’s behavior when a connection is available.
- isListening()¶
- Return type:
bool
Returns
true
if the server is listening for incoming connections otherwise false.- listen(name)¶
- Parameters:
name – str
- Return type:
bool
Tells the server to listen for incoming connections on
name
. If the server is currently listening then it will return false. Return true on success otherwise false.name
can be a single name andQLocalServer
will determine the correct platform specific path.serverName()
will return the name that is passed into listen.Usually you would just pass in a name like “foo”, but on Unix this could also be a path such as “/tmp/foo” and on Windows this could be a pipe path such as “\\.\pipe\foo”
Note
On Unix if the server crashes without closing listen will fail with AddressInUseError. To create a new server the file should be removed. On Windows two local servers can listen to the same pipe at the same time, but any connections will go to one of the server.
See also
- listen(socketDescriptor)
- Parameters:
socketDescriptor –
qintptr
- Return type:
bool
Instructs the server to listen for incoming connections on
socketDescriptor
. The property returnsfalse
if the server is currently listening. It returnstrue
on success; otherwise, it returnsfalse
. The socket must be ready to accept new connections with no extra platform-specific functions called. The socket is set into non-blocking mode.serverName()
,fullServerName()
may return a string with a name if this option is supported by the platform; otherwise, they return an empty QString. In particular, the addresses of sockets in the abstract namespace supported by Linux will not yield useful names if they contain unprintable characters.See also
- listenBacklogSize()¶
- Return type:
int
Returns the backlog queue size of to be accepted connections.
See also
- maxPendingConnections()¶
- Return type:
int
Returns the maximum number of pending accepted connections. The default is 30.
- newConnection()¶
This signal is emitted every time a new connection is available.
- nextPendingConnection()¶
- Return type:
Returns the next pending connection as a connected
QLocalSocket
object.The socket is created as a child of the server, which means that it is automatically deleted when the
QLocalServer
object is destroyed. It is still a good idea to delete the object explicitly when you are done with it, to avoid wasting memory.None
is returned if this function is called when there are no pending connections.- static removeServer(name)¶
- Parameters:
name – str
- Return type:
bool
Removes any server instance that might cause a call to
listen()
to fail and returnstrue
if successful; otherwise returnsfalse
. This function is meant to recover from a crash, when the previous server instance has not been cleaned up.On Windows, this function does nothing; on Unix, it removes the socket file given by
name
.Warning
Be careful to avoid removing sockets of running instances.
- serverError()¶
- Return type:
Returns the type of error that occurred last or
NoError
.See also
- serverName()¶
- Return type:
str
Returns the server name if the server is listening for connections; otherwise returns QString()
See also
- setListenBacklogSize(size)¶
- Parameters:
size – int
Sets the backlog queue size of to be accepted connections to
size
. The operating system might reduce or ignore this value. By default, the queue size is 50.- setMaxPendingConnections(numConnections)¶
- Parameters:
numConnections – int
Sets the maximum number of pending accepted connections to
numConnections
.QLocalServer
will accept no more thannumConnections
incoming connections beforenextPendingConnection()
is called.Note: Even though
QLocalServer
will stop accepting new connections after it has reached its maximum number of pending connections, the operating system may still keep them in queue which will result in clients signaling that it is connected.- setSocketOptions(options)¶
- Parameters:
options – Combination of
SocketOption
See also
Setter of property
socketOptionsᅟ
.- socketDescriptor()¶
- Return type:
qintptr
Returns the native socket descriptor the server uses to listen for incoming instructions, or -1 if the server is not listening.
The type of the descriptor depends on the platform:
On Windows, the returned value is a Winsock 2 Socket Handle.
On INTEGRITY, the returned value is the
QTcpServer
socket descriptor and the type is defined bysocketDescriptor
.On all other UNIX-like operating systems, the type is a file descriptor representing a listening socket.
See also
- socketOptions()¶
- Return type:
Combination of
SocketOption
Returns the socket options set on the socket.
See also
Getter of property
socketOptionsᅟ
.- waitForNewConnection(msec)¶
- Parameters:
msec – int
- Return type:
(retval, timedOut)
Waits for at most
msec
milliseconds or until an incoming connection is available. Returnstrue
if a connection is available; otherwise returnsfalse
. If the operation timed out andtimedOut
is notNone
, *timedOut will be set to true.This is a blocking function call. Its use is ill-advised in a single-threaded GUI application, since the whole application will stop responding until the function returns. waitForNewConnection() is mostly useful when there is no event loop available.
The non-blocking alternative is to connect to the
newConnection()
signal.If msec is -1, this function will not time out.