SocketListener#
Added in version 2.22.
Superclasses: Object
Subclasses: SocketService
A GSocketListener is an object that keeps track of a set
of server sockets and helps you accept sockets from any of the
socket, either sync or async.
Add addresses and ports to listen on using
add_address and
add_inet_port. These will be listened on until
close is called. Dropping your final reference to
the GSocketListener will not cause close to be
called implicitly, as some references to the GSocketListener may be held
internally.
If you want to implement a network server, also look at
SocketService and ThreadedSocketService which are
subclasses of GSocketListener that make this even easier.
Constructors#
- class SocketListener
- classmethod new() SocketListener#
Creates a new
SocketListenerwith no sockets to listen for. New listeners can be added with e.g.add_address()oradd_inet_port().Added in version 2.22.
Methods#
- class SocketListener
- accept(cancellable: Cancellable | None = None) tuple[SocketConnection, Object | None]#
Blocks waiting for a client to connect to any of the sockets added to the listener. Returns a
SocketConnectionfor the socket that was accepted.If
source_objectis notNoneit will be filled out with the source object specified when the corresponding socket or address was added to the listener.If
cancellableis notNone, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorCANCELLEDwill be returned.Added in version 2.22.
- Parameters:
cancellable – optional
Cancellableobject,Noneto ignore.
- async accept_async(self) tuple[SocketConnection, Object | None]#
This is the awaitable version of
accept_async().Added in version 2.22.
- accept_async(cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None#
This is the asynchronous version of
accept().When the operation is finished
callbackwill be called. You can then callaccept_finish()to get the result of the operation.Added in version 2.22.
- Parameters:
cancellable – a
Cancellable, orNonecallback – a
AsyncReadyCallbackuser_data – user data for the callback
- accept_finish(result: AsyncResult) tuple[SocketConnection, Object | None]#
Finishes an async accept operation. See
accept_async()Added in version 2.22.
- Parameters:
result – a
AsyncResult.
- accept_socket(cancellable: Cancellable | None = None) tuple[Socket, Object | None]#
Blocks waiting for a client to connect to any of the sockets added to the listener. Returns the
Socketthat was accepted.If you want to accept the high-level
SocketConnection, not aSocket, which is often the case, then you should useaccept()instead.If
source_objectis notNoneit will be filled out with the source object specified when the corresponding socket or address was added to the listener.If
cancellableis notNone, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorCANCELLEDwill be returned.Added in version 2.22.
- Parameters:
cancellable – optional
Cancellableobject,Noneto ignore.
- async accept_socket_async(self) tuple[Socket, Object | None]#
This is the awaitable version of
accept_socket_async().Added in version 2.22.
- accept_socket_async(cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None#
This is the asynchronous version of
accept_socket().When the operation is finished
callbackwill be called. You can then callaccept_socket_finish()to get the result of the operation.Added in version 2.22.
- Parameters:
cancellable – a
Cancellable, orNonecallback – a
AsyncReadyCallbackuser_data – user data for the callback
- accept_socket_finish(result: AsyncResult) tuple[Socket, Object | None]#
Finishes an async accept operation. See
accept_socket_async()Added in version 2.22.
- Parameters:
result – a
AsyncResult.
- add_address(address: SocketAddress, type: SocketType, protocol: SocketProtocol, source_object: Object | None = None) tuple[bool, SocketAddress]#
Creates a socket of type
typeand protocolprotocol, binds it toaddressand adds it to the set of sockets we’re accepting sockets from.Note that adding an IPv6 address, depending on the platform, may or may not result in a listener that also accepts IPv4 connections. For more deterministic behavior, see
add_inet_port().source_objectwill be passed out in the various calls to accept to identify this particular source, which is useful if you’re listening on multiple addresses and do different things depending on what address is connected to.If successful and
effective_addressis non-Nonethen it will be set to the address that the binding actually occurred at. This is helpful for determining the port number that was used for when requesting a binding to port 0 (ie: “any port”). This address, if requested, belongs to the caller and must be freed.Call
close()to stop listening onaddress; this will not be done automatically when you drop your final reference tolistener, as references may be held internally.Added in version 2.22.
- Parameters:
address – a
SocketAddresstype – a
SocketTypeprotocol – a
SocketProtocolsource_object – Optional
Objectidentifying this source
- add_any_inet_port(source_object: Object | None = None) int#
Listens for TCP connections on any available port number for both IPv6 and IPv4 (if each is available).
This is useful if you need to have a socket for incoming connections but don’t care about the specific port number.
source_objectwill be passed out in the various calls to accept to identify this particular source, which is useful if you’re listening on multiple addresses and do different things depending on what address is connected to.Added in version 2.24.
- Parameters:
source_object – Optional
Objectidentifying this source
- add_inet_port(port: int, source_object: Object | None = None) bool#
Helper function for
add_address()that creates a TCP/IP socket listening on IPv4 and IPv6 (if supported) on the specified port on all interfaces.source_objectwill be passed out in the various calls to accept to identify this particular source, which is useful if you’re listening on multiple addresses and do different things depending on what address is connected to.Call
close()to stop listening onport; this will not be done automatically when you drop your final reference tolistener, as references may be held internally.Added in version 2.22.
- Parameters:
port – an IP port number (non-zero)
source_object – Optional
Objectidentifying this source
- add_socket(socket: Socket, source_object: Object | None = None) bool#
Adds
socketto the set of sockets that we try to accept new clients from. The socket must be bound to a local address and listened to.source_objectwill be passed out in the various calls to accept to identify this particular source, which is useful if you’re listening on multiple addresses and do different things depending on what address is connected to.The
socketwill not be automatically closed when thelisteneris finalized unless the listener held the final reference to the socket. Before GLib 2.42, thesocketwas automatically closed on finalization of thelistener, even if references to it were held elsewhere.Added in version 2.22.
- do_event(self, event: SocketListenerEvent, socket: Socket) None#
- Parameters:
event
socket
- set_backlog(listen_backlog: int) None#
Sets the listen backlog on the sockets in the listener. This must be called before adding any sockets, addresses or ports to the
SocketListener(for example, by callingadd_inet_port()) to be effective.See
set_listen_backlog()for detailsAdded in version 2.22.
- Parameters:
listen_backlog – an integer
Properties#
Signals#
- class SocketListener.signals
- event(event: SocketListenerEvent, socket: Socket) None#
The type of the None singleton.
Added in version 2.46.
- Parameters:
event – the event that is occurring
socket – the
Socketthe event is occurring on