- class QHostInfo#
The
QHostInfo
class provides static functions for host name lookups. More…Synopsis#
Methods#
def
__init__()
def
addresses()
def
error()
def
errorString()
def
hostName()
def
lookupHost()
def
lookupId()
def
setAddresses()
def
setError()
def
setErrorString()
def
setHostName()
def
setLookupId()
def
swap()
Static functions#
def
fromName()
def
localHostName()
def
lookupHost()
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#
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
QHostInfo
finds the IP address(es) associated with a host name, or the host name associated with an IP address. The class provides two static convenience functions: one that works asynchronously and emits a signal once the host is found, and one that blocks and returns aQHostInfo
object.To look up a host’s IP addresses asynchronously, call
lookupHost()
, which takes the host name or IP address, a receiver object, and a slot signature as arguments and returns an ID. You can abort the lookup by callingabortHostLookup()
with the lookup ID.Example:
# To find the IP address of qt-project.org QHostInfo::lookupHost("qt-project.org", self.printResults) # To find the host name for 4.2.2.1 QHostInfo::lookupHost("4.2.2.1", self.printResults)
The slot is invoked when the results are ready. The results are stored in a
QHostInfo
object. Calladdresses()
to get the list of IP addresses for the host, andhostName()
to get the host name that was looked up.If the lookup failed,
error()
returns the type of error that occurred.errorString()
gives a human-readable description of the lookup error.If you want a blocking lookup, use the
fromName()
function:info = QHostInfo.fromName("qt-project.org")
QHostInfo
supports Internationalized Domain Names (IDNs) through the IDNA and Punycode standards.To retrieve the name of the local host, use the static
localHostName()
function.QHostInfo
uses the mechanisms provided by the operating system to perform the lookup. As per RFC 6724 there is no guarantee that all IP addresses registered for a domain or host will be returned.Note
Since Qt 4.6.1
QHostInfo
is using multiple threads for DNS lookup instead of one dedicated DNS thread. This improves performance, but also changes the order of signal emissions when usinglookupHost()
compared to previous versions of Qt.Note
Since Qt 4.6.3
QHostInfo
is using a small internal 60 second DNS cache for performance improvements.See also
QAbstractSocket
RFC 3492RFC 6724
- class HostInfoError#
This enum describes the various errors that can occur when trying to resolve a host name.
Constant
Description
QHostInfo.NoError
The lookup was successful.
QHostInfo.HostNotFound
No IP addresses were found for the host.
QHostInfo.UnknownError
An unknown error occurred.
See also
- __init__([lookupId=-1])#
- Parameters:
lookupId – int
Constructs an empty host info object with lookup ID
id
.See also
- __init__(d)
- Parameters:
d –
QHostInfo
Constructs a copy of
other
.- static abortHostLookup(lookupId)#
- Parameters:
lookupId – int
Aborts the host lookup with the ID
id
, as returned bylookupHost()
.See also
- addresses()#
- Return type:
.list of QHostAddress
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Returns the list of IP addresses associated with
hostName()
. This list may be empty.Example:
info = QHostInfo() ... if not info.addresses().isEmpty(): address = info.addresses().first() # use the first IP address
See also
- error()#
- Return type:
Returns the type of error that occurred if the host name lookup failed; otherwise returns
NoError
.See also
- errorString()#
- Return type:
str
If the lookup failed, this function returns a human readable description of the error; otherwise “Unknown error” is returned.
See also
Looks up the IP address(es) for the given host
name
. The function blocks during the lookup which means that execution of the program is suspended until the results of the lookup are ready. Returns the result of the lookup in aQHostInfo
object.If you pass a literal IP address to
name
instead of a host name,QHostInfo
will search for the domain name for the IP (i.e.,QHostInfo
will perform a reverse lookup). On success, the returnedQHostInfo
will contain both the resolved domain name and IP addresses for the host name.See also
- hostName()#
- Return type:
str
Returns the name of the host whose IP addresses were looked up.
See also
- static localDomainName()#
- Return type:
str
Returns the DNS domain of this machine.
- static localHostName()#
- Return type:
str
Returns this machine’s host name, if one is configured. Note that hostnames are not guaranteed to be globally unique, especially if they were configured automatically.
This function does not guarantee the returned host name is a Fully Qualified Domain Name (FQDN). For that, use
fromName()
to resolve the returned name to an FQDN.This function returns the same as QSysInfo::machineHostName().
See also
- lookupHost(arg__1, arg__2)#
- Parameters:
arg__1 – str
arg__2 –
PyCallable
- static lookupHost(name, receiver, member)
- Parameters:
name – str
receiver –
QObject
member – str
- Return type:
int
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Looks up the IP address(es) associated with host name
name
, and returns an ID for the lookup. When the result of the lookup is ready, the slot or signalmember
inreceiver
is called with aQHostInfo
argument. TheQHostInfo
object can then be inspected to get the results of the lookup.The lookup is performed by a single function call, for example:
QHostInfo::lookupHost("www.kde.org", self.lookedUp)
The implementation of the slot prints basic information about the addresses returned by the lookup, or reports an error if it failed:
def lookedUp(self, host): if host.error() != QHostInfo.NoError: print("Lookup failed:", host.errorString()) return addresses = host.addresses() for address in addresses: print("Found address:", address.toString())
If you pass a literal IP address to
name
instead of a host name,QHostInfo
will search for the domain name for the IP (i.e.,QHostInfo
will perform a reverse lookup). On success, the resultingQHostInfo
will contain both the resolved domain name and IP addresses for the host name. Example:QHostInfo::lookupHost("4.2.2.1", self.lookedUp)
Note
There is no guarantee on the order the signals will be emitted if you start multiple requests with lookupHost().
Note
In Qt versions prior to 6.7, this function took
receiver
as (non-const)QObject*
.See also
- lookupId()#
- Return type:
int
Returns the ID of this lookup.
See also
- setAddresses(addresses)#
- Parameters:
addresses – .list of QHostAddress
Sets the list of addresses in this
QHostInfo
toaddresses
.See also
- setError(error)#
- Parameters:
error –
HostInfoError
Sets the error type of this
QHostInfo
toerror
.See also
- setErrorString(errorString)#
- Parameters:
errorString – str
Sets the human readable description of the error that occurred to
str
if the lookup failed.See also
- setHostName(name)#
- Parameters:
name – str
Sets the host name of this
QHostInfo
tohostName
.See also
- setLookupId(id)#
- Parameters:
id – int
Sets the ID of this lookup to
id
.See also
Swaps host-info
other
with this host-info. This operation is very fast and never fails.