Resolver#
Superclasses: Object
Subclasses: ThreadedResolver
The object that handles DNS resolution. Use get_default
to get the default resolver.
GResolver
provides cancellable synchronous and asynchronous DNS
resolution, for hostnames (lookup_by_address
,
lookup_by_name
and their async variants) and SRV
(service) records (lookup_service
).
NetworkAddress
and NetworkService
provide wrappers
around GResolver
functionality that also implement
SocketConnectable
, making it easy to connect to a remote
host/service.
The default resolver (see get_default
) has a timeout of
30s set on it since GLib 2.78. Earlier versions of GLib did not support
resolver timeouts.
This is an abstract type; subclasses of it implement different resolvers for different platforms and situations.
Methods#
- class Resolver
- do_lookup_by_address(self, address: InetAddress, cancellable: Cancellable | None = None) str #
- Parameters:
address
cancellable
- do_lookup_by_address_async(self, address: InetAddress, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None #
- Parameters:
address
cancellable
callback
user_data
- do_lookup_by_address_finish(self, result: AsyncResult) str #
- Parameters:
result
- do_lookup_by_name(self, hostname: str, cancellable: Cancellable | None = None) list[InetAddress] #
- Parameters:
hostname
cancellable
- do_lookup_by_name_async(self, hostname: str, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None #
- Parameters:
hostname
cancellable
callback
user_data
- do_lookup_by_name_finish(self, result: AsyncResult) list[InetAddress] #
- Parameters:
result
- do_lookup_by_name_with_flags(self, hostname: str, flags: ResolverNameLookupFlags, cancellable: Cancellable | None = None) list[InetAddress] #
- Parameters:
hostname
flags
cancellable
- do_lookup_by_name_with_flags_async(self, hostname: str, flags: ResolverNameLookupFlags, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None #
- Parameters:
hostname
flags
cancellable
callback
user_data
- do_lookup_by_name_with_flags_finish(self, result: AsyncResult) list[InetAddress] #
- Parameters:
result
- do_lookup_records(self, rrname: str, record_type: ResolverRecordType, cancellable: Cancellable | None = None) list[Variant] #
- Parameters:
rrname
record_type
cancellable
- do_lookup_records_async(self, rrname: str, record_type: ResolverRecordType, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None #
- Parameters:
rrname
record_type
cancellable
callback
user_data
- do_lookup_records_finish(self, result: AsyncResult) list[Variant] #
- Parameters:
result
- do_lookup_service_async(self, rrname: str, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None #
- Parameters:
rrname
cancellable
callback
user_data
- do_lookup_service_finish(self, result: AsyncResult) list[SrvTarget] #
- Parameters:
result
- classmethod get_default() Resolver #
Gets the default
Resolver
. You should unref it when you are done with it.Resolver
may use its reference count as a hint about how many threads it should allocate for concurrent DNS resolutions.Added in version 2.22.
- get_timeout() int #
Get the timeout applied to all resolver lookups. See
Resolver
:timeout.Added in version 2.78.
- lookup_by_address(address: InetAddress, cancellable: Cancellable | None = None) str #
Synchronously reverse-resolves
address
to determine its associated hostname.If the DNS resolution fails,
error
(if non-None
) will be set to a value fromGResolverError
.If
cancellable
is non-None
, it can be used to cancel the operation, in which caseerror
(if non-None
) will be set toCANCELLED
.Added in version 2.22.
- Parameters:
address – the address to reverse-resolve
cancellable – a
Cancellable
, orNone
- lookup_by_address_async(address: InetAddress, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None #
Begins asynchronously reverse-resolving
address
to determine its associated hostname, and eventually callscallback
, which must calllookup_by_address_finish()
to get the final result.Added in version 2.22.
- Parameters:
address – the address to reverse-resolve
cancellable – a
Cancellable
, orNone
callback – callback to call after resolution completes
user_data – data for
callback
- lookup_by_address_finish(result: AsyncResult) str #
Retrieves the result of a previous call to
lookup_by_address_async()
.If the DNS resolution failed,
error
(if non-None
) will be set to a value fromGResolverError
. If the operation was cancelled,error
will be set toCANCELLED
.Added in version 2.22.
- Parameters:
result – the result passed to your
AsyncReadyCallback
- lookup_by_name(hostname: str, cancellable: Cancellable | None = None) list[InetAddress] #
Synchronously resolves
hostname
to determine its associated IP address(es).hostname
may be an ASCII-only or UTF-8 hostname, or the textual form of an IP address (in which case this just becomes a wrapper aroundnew_from_string()
).On success,
lookup_by_name()
will return a non-emptyList
ofInetAddress
, sorted in order of preference and guaranteed to not contain duplicates. That is, if using the result to connect tohostname
, you should attempt to connect to the first address first, then the second if the first fails, etc. If you are using the result to listen on a socket, it is appropriate to add each result using e.g.add_address()
.If the DNS resolution fails,
error
(if non-None
) will be set to a value fromGResolverError
andNone
will be returned.If
cancellable
is non-None
, it can be used to cancel the operation, in which caseerror
(if non-None
) will be set toCANCELLED
.If you are planning to connect to a socket on the resolved IP address, it may be easier to create a
NetworkAddress
and use itsSocketConnectable
interface.Added in version 2.22.
- Parameters:
hostname – the hostname to look up
cancellable – a
Cancellable
, orNone
- lookup_by_name_async(hostname: str, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None #
Begins asynchronously resolving
hostname
to determine its associated IP address(es), and eventually callscallback
, which must calllookup_by_name_finish()
to get the result. Seelookup_by_name()
for more details.Added in version 2.22.
- Parameters:
hostname – the hostname to look up the address of
cancellable – a
Cancellable
, orNone
callback – callback to call after resolution completes
user_data – data for
callback
- lookup_by_name_finish(result: AsyncResult) list[InetAddress] #
Retrieves the result of a call to
lookup_by_name_async()
.If the DNS resolution failed,
error
(if non-None
) will be set to a value fromGResolverError
. If the operation was cancelled,error
will be set toCANCELLED
.Added in version 2.22.
- Parameters:
result – the result passed to your
AsyncReadyCallback
- lookup_by_name_with_flags(hostname: str, flags: ResolverNameLookupFlags, cancellable: Cancellable | None = None) list[InetAddress] #
This differs from
lookup_by_name()
in that you can modify the lookup behavior withflags
. For example this can be used to limit results withIPV4_ONLY
.Added in version 2.60.
- Parameters:
hostname – the hostname to look up
flags – extra
ResolverNameLookupFlags
for the lookupcancellable – a
Cancellable
, orNone
- lookup_by_name_with_flags_async(hostname: str, flags: ResolverNameLookupFlags, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None #
Begins asynchronously resolving
hostname
to determine its associated IP address(es), and eventually callscallback
, which must calllookup_by_name_with_flags_finish()
to get the result. Seelookup_by_name()
for more details.Added in version 2.60.
- Parameters:
hostname – the hostname to look up the address of
flags – extra
ResolverNameLookupFlags
for the lookupcancellable – a
Cancellable
, orNone
callback – callback to call after resolution completes
user_data – data for
callback
- lookup_by_name_with_flags_finish(result: AsyncResult) list[InetAddress] #
Retrieves the result of a call to
lookup_by_name_with_flags_async()
.If the DNS resolution failed,
error
(if non-None
) will be set to a value fromGResolverError
. If the operation was cancelled,error
will be set toCANCELLED
.Added in version 2.60.
- Parameters:
result – the result passed to your
AsyncReadyCallback
- lookup_records(rrname: str, record_type: ResolverRecordType, cancellable: Cancellable | None = None) list[Variant] #
Synchronously performs a DNS record lookup for the given
rrname
and returns a list of records asVariant
tuples. SeeResolverRecordType
for information on what the records contain for eachrecord_type
.If the DNS resolution fails,
error
(if non-None
) will be set to a value fromGResolverError
andNone
will be returned.If
cancellable
is non-None
, it can be used to cancel the operation, in which caseerror
(if non-None
) will be set toCANCELLED
.Added in version 2.34.
- Parameters:
rrname – the DNS name to look up the record for
record_type – the type of DNS record to look up
cancellable – a
Cancellable
, orNone
- lookup_records_async(rrname: str, record_type: ResolverRecordType, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None #
Begins asynchronously performing a DNS lookup for the given
rrname
, and eventually callscallback
, which must calllookup_records_finish()
to get the final result. Seelookup_records()
for more details.Added in version 2.34.
- Parameters:
rrname – the DNS name to look up the record for
record_type – the type of DNS record to look up
cancellable – a
Cancellable
, orNone
callback – callback to call after resolution completes
user_data – data for
callback
- lookup_records_finish(result: AsyncResult) list[Variant] #
Retrieves the result of a previous call to
lookup_records_async()
. Returns a non-empty list of records asVariant
tuples. SeeResolverRecordType
for information on what the records contain.If the DNS resolution failed,
error
(if non-None
) will be set to a value fromGResolverError
. If the operation was cancelled,error
will be set toCANCELLED
.Added in version 2.34.
- Parameters:
result – the result passed to your
AsyncReadyCallback
- lookup_service(service: str, protocol: str, domain: str, cancellable: Cancellable | None = None) list[SrvTarget] #
Synchronously performs a DNS SRV lookup for the given
service
andprotocol
in the givendomain
and returns an array ofSrvTarget
.domain
may be an ASCII-only or UTF-8 hostname. Note also that theservice
andprotocol
arguments do not include the leading underscore that appears in the actual DNS entry.On success,
lookup_service()
will return a non-emptyList
ofSrvTarget
, sorted in order of preference. (That is, you should attempt to connect to the first target first, then the second if the first fails, etc.)If the DNS resolution fails,
error
(if non-None
) will be set to a value fromGResolverError
andNone
will be returned.If
cancellable
is non-None
, it can be used to cancel the operation, in which caseerror
(if non-None
) will be set toCANCELLED
.If you are planning to connect to the service, it is usually easier to create a
NetworkService
and use itsSocketConnectable
interface.Added in version 2.22.
- Parameters:
service – the service type to look up (eg, “ldap”)
protocol – the networking protocol to use for
service
(eg, “tcp”)domain – the DNS domain to look up the service in
cancellable – a
Cancellable
, orNone
- lookup_service_async(service: str, protocol: str, domain: str, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None #
Begins asynchronously performing a DNS SRV lookup for the given
service
andprotocol
in the givendomain
, and eventually callscallback
, which must calllookup_service_finish()
to get the final result. Seelookup_service()
for more details.Added in version 2.22.
- Parameters:
service – the service type to look up (eg, “ldap”)
protocol – the networking protocol to use for
service
(eg, “tcp”)domain – the DNS domain to look up the service in
cancellable – a
Cancellable
, orNone
callback – callback to call after resolution completes
user_data – data for
callback
- lookup_service_finish(result: AsyncResult) list[SrvTarget] #
Retrieves the result of a previous call to
lookup_service_async()
.If the DNS resolution failed,
error
(if non-None
) will be set to a value fromGResolverError
. If the operation was cancelled,error
will be set toCANCELLED
.Added in version 2.22.
- Parameters:
result – the result passed to your
AsyncReadyCallback
- set_default() None #
Sets
resolver
to be the application’s default resolver (reffingresolver
, and unreffing the previous default resolver, if any). Future calls toget_default()
will return this resolver.This can be used if an application wants to perform any sort of DNS caching or “pinning”; it can implement its own
Resolver
that calls the original default resolver for DNS operations, and implements its own cache policies on top of that, and then set itself as the default resolver for all later code to use.Added in version 2.22.
Properties#
Signals#
Virtual Methods#
- class Resolver
- do_lookup_by_address(address: InetAddress, cancellable: Cancellable | None = None) str #
Synchronously reverse-resolves
address
to determine its associated hostname.If the DNS resolution fails,
error
(if non-None
) will be set to a value fromGResolverError
.If
cancellable
is non-None
, it can be used to cancel the operation, in which caseerror
(if non-None
) will be set toCANCELLED
.Added in version 2.22.
- Parameters:
address – the address to reverse-resolve
cancellable – a
Cancellable
, orNone
- do_lookup_by_address_async(address: InetAddress, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None #
Begins asynchronously reverse-resolving
address
to determine its associated hostname, and eventually callscallback
, which must calllookup_by_address_finish()
to get the final result.Added in version 2.22.
- Parameters:
address – the address to reverse-resolve
cancellable – a
Cancellable
, orNone
callback – callback to call after resolution completes
user_data – data for
callback
- do_lookup_by_address_finish(result: AsyncResult) str #
Retrieves the result of a previous call to
lookup_by_address_async()
.If the DNS resolution failed,
error
(if non-None
) will be set to a value fromGResolverError
. If the operation was cancelled,error
will be set toCANCELLED
.Added in version 2.22.
- Parameters:
result – the result passed to your
AsyncReadyCallback
- do_lookup_by_name(hostname: str, cancellable: Cancellable | None = None) list[InetAddress] #
Synchronously resolves
hostname
to determine its associated IP address(es).hostname
may be an ASCII-only or UTF-8 hostname, or the textual form of an IP address (in which case this just becomes a wrapper aroundnew_from_string()
).On success,
lookup_by_name()
will return a non-emptyList
ofInetAddress
, sorted in order of preference and guaranteed to not contain duplicates. That is, if using the result to connect tohostname
, you should attempt to connect to the first address first, then the second if the first fails, etc. If you are using the result to listen on a socket, it is appropriate to add each result using e.g.add_address()
.If the DNS resolution fails,
error
(if non-None
) will be set to a value fromGResolverError
andNone
will be returned.If
cancellable
is non-None
, it can be used to cancel the operation, in which caseerror
(if non-None
) will be set toCANCELLED
.If you are planning to connect to a socket on the resolved IP address, it may be easier to create a
NetworkAddress
and use itsSocketConnectable
interface.Added in version 2.22.
- Parameters:
hostname – the hostname to look up
cancellable – a
Cancellable
, orNone
- do_lookup_by_name_async(hostname: str, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None #
Begins asynchronously resolving
hostname
to determine its associated IP address(es), and eventually callscallback
, which must calllookup_by_name_finish()
to get the result. Seelookup_by_name()
for more details.Added in version 2.22.
- Parameters:
hostname – the hostname to look up the address of
cancellable – a
Cancellable
, orNone
callback – callback to call after resolution completes
user_data – data for
callback
- do_lookup_by_name_finish(result: AsyncResult) list[InetAddress] #
Retrieves the result of a call to
lookup_by_name_async()
.If the DNS resolution failed,
error
(if non-None
) will be set to a value fromGResolverError
. If the operation was cancelled,error
will be set toCANCELLED
.Added in version 2.22.
- Parameters:
result – the result passed to your
AsyncReadyCallback
- do_lookup_by_name_with_flags(hostname: str, flags: ResolverNameLookupFlags, cancellable: Cancellable | None = None) list[InetAddress] #
This differs from
lookup_by_name()
in that you can modify the lookup behavior withflags
. For example this can be used to limit results withIPV4_ONLY
.Added in version 2.60.
- Parameters:
hostname – the hostname to look up
flags – extra
ResolverNameLookupFlags
for the lookupcancellable – a
Cancellable
, orNone
- do_lookup_by_name_with_flags_async(hostname: str, flags: ResolverNameLookupFlags, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None #
Begins asynchronously resolving
hostname
to determine its associated IP address(es), and eventually callscallback
, which must calllookup_by_name_with_flags_finish()
to get the result. Seelookup_by_name()
for more details.Added in version 2.60.
- Parameters:
hostname – the hostname to look up the address of
flags – extra
ResolverNameLookupFlags
for the lookupcancellable – a
Cancellable
, orNone
callback – callback to call after resolution completes
user_data – data for
callback
- do_lookup_by_name_with_flags_finish(result: AsyncResult) list[InetAddress] #
Retrieves the result of a call to
lookup_by_name_with_flags_async()
.If the DNS resolution failed,
error
(if non-None
) will be set to a value fromGResolverError
. If the operation was cancelled,error
will be set toCANCELLED
.Added in version 2.60.
- Parameters:
result – the result passed to your
AsyncReadyCallback
- do_lookup_records(rrname: str, record_type: ResolverRecordType, cancellable: Cancellable | None = None) list[Variant] #
Synchronously performs a DNS record lookup for the given
rrname
and returns a list of records asVariant
tuples. SeeResolverRecordType
for information on what the records contain for eachrecord_type
.If the DNS resolution fails,
error
(if non-None
) will be set to a value fromGResolverError
andNone
will be returned.If
cancellable
is non-None
, it can be used to cancel the operation, in which caseerror
(if non-None
) will be set toCANCELLED
.Added in version 2.34.
- Parameters:
rrname – the DNS name to look up the record for
record_type – the type of DNS record to look up
cancellable – a
Cancellable
, orNone
- do_lookup_records_async(rrname: str, record_type: ResolverRecordType, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None #
Begins asynchronously performing a DNS lookup for the given
rrname
, and eventually callscallback
, which must calllookup_records_finish()
to get the final result. Seelookup_records()
for more details.Added in version 2.34.
- Parameters:
rrname – the DNS name to look up the record for
record_type – the type of DNS record to look up
cancellable – a
Cancellable
, orNone
callback – callback to call after resolution completes
user_data – data for
callback
- do_lookup_records_finish(result: AsyncResult) list[Variant] #
Retrieves the result of a previous call to
lookup_records_async()
. Returns a non-empty list of records asVariant
tuples. SeeResolverRecordType
for information on what the records contain.If the DNS resolution failed,
error
(if non-None
) will be set to a value fromGResolverError
. If the operation was cancelled,error
will be set toCANCELLED
.Added in version 2.34.
- Parameters:
result – the result passed to your
AsyncReadyCallback
- do_lookup_service_async(rrname: str, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None #
- Parameters:
rrname
cancellable
callback
user_data
- do_lookup_service_finish(result: AsyncResult) list[SrvTarget] #
Retrieves the result of a previous call to
lookup_service_async()
.If the DNS resolution failed,
error
(if non-None
) will be set to a value fromGResolverError
. If the operation was cancelled,error
will be set toCANCELLED
.Added in version 2.22.
- Parameters:
result – the result passed to your
AsyncReadyCallback