QHostInfo

The QHostInfo class provides static functions for host name lookups. More

Inheritance diagram of PySide6.QtNetwork.QHostInfo

Synopsis

Functions

Static functions

Detailed Description

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 a QHostInfo 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 calling abortHostLookup() with the lookup ID.

Example:

# To find the IP address of qt-project.org
QHostInfo.lookupHost("qt-project.org", self, SLOT("printResults(QHostInfo)"))

# To find the host name for 4.2.2.1
QHostInfo.lookupHost("4.2.2.1", self, SLOT("printResults(QHostInfo)"))

The slot is invoked when the results are ready. The results are stored in a QHostInfo object. Call addresses() to get the list of IP addresses for the host, and hostName() 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 function.

QHostInfo uses the mechanisms provided by the operating system to perform the lookup. As per {https://tools.ietf.org/html/rfc6724}{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 using lookupHost() 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.

class PySide6.QtNetwork.QHostInfo(d)

PySide6.QtNetwork.QHostInfo([lookupId=-1])

Parameters

Constructs an empty host info object with lookup ID id.

See also

lookupId()

PySide6.QtNetwork.QHostInfo.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

error() setError()

static PySide6.QtNetwork.QHostInfo.abortHostLookup(lookupId)
Parameters

lookupId – int

Aborts the host lookup with the ID id, as returned by lookupHost() .

See also

lookupHost() lookupId()

PySide6.QtNetwork.QHostInfo.addresses()
Return type

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
PySide6.QtNetwork.QHostInfo.error()
Return type

HostInfoError

Returns the type of error that occurred if the host name lookup failed; otherwise returns NoError .

PySide6.QtNetwork.QHostInfo.errorString()
Return type

str

If the lookup failed, this function returns a human readable description of the error; otherwise “Unknown error” is returned.

static PySide6.QtNetwork.QHostInfo.fromName(name)
Parameters

name – str

Return type

PySide6.QtNetwork.QHostInfo

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 a QHostInfo 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 returned QHostInfo will contain both the resolved domain name and IP addresses for the host name.

See also

lookupHost()

PySide6.QtNetwork.QHostInfo.hostName()
Return type

str

Returns the name of the host whose IP addresses were looked up.

static PySide6.QtNetwork.QHostInfo.localDomainName()
Return type

str

static PySide6.QtNetwork.QHostInfo.localHostName()
Return type

str

PySide6.QtNetwork.QHostInfo.lookupId()
Return type

int

Returns the ID of this lookup.

PySide6.QtNetwork.QHostInfo.setAddresses(addresses)
Parameters

addresses

Sets the list of addresses in this QHostInfo to addresses.

See also

addresses()

PySide6.QtNetwork.QHostInfo.setError(error)
Parameters

errorHostInfoError

Sets the error type of this QHostInfo to error.

PySide6.QtNetwork.QHostInfo.setErrorString(errorString)
Parameters

errorString – str

Sets the human readable description of the error that occurred to str if the lookup failed.

PySide6.QtNetwork.QHostInfo.setHostName(name)
Parameters

name – str

Sets the host name of this QHostInfo to hostName.

See also

hostName()

PySide6.QtNetwork.QHostInfo.setLookupId(id)
Parameters

id – int

Sets the ID of this lookup to id.

See also

lookupId() lookupHost()

PySide6.QtNetwork.QHostInfo.swap(other)
Parameters

otherPySide6.QtNetwork.QHostInfo