本文整理汇总了Python中PyQt5.QtNetwork.QHostInfo类的典型用法代码示例。如果您正苦于以下问题:Python QHostInfo类的具体用法?Python QHostInfo怎么用?Python QHostInfo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QHostInfo类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _is_url_dns
def _is_url_dns(urlstr):
"""Check if a URL is really a URL via DNS.
Args:
url: The URL to check for as a string.
Return:
True if the URL really is a URL, False otherwise.
"""
url = qurl_from_user_input(urlstr)
assert url.isValid()
if (utils.raises(ValueError, ipaddress.ip_address, urlstr) and
not QHostAddress(urlstr).isNull()):
log.url.debug("Bogus IP URL -> False")
# Qt treats things like "23.42" or "1337" or "0xDEAD" as valid URLs
# which we don't want to.
return False
host = url.host()
if not host:
log.url.debug("URL has no host -> False")
return False
log.url.debug("Doing DNS request for {}".format(host))
info = QHostInfo.fromName(host)
return not info.error()
示例2: nickName
def nickName(self):
"""
Public method to get the nick name.
@return nick name (string)
"""
return "{0}@{1}@{2}".format(
self.__username,
QHostInfo.localHostName(),
self.__servers[0].serverPort()
)
示例3: dnsResolve
def dnsResolve(self, host):
"""Resolve a DNS hostname.
Resolves the given DNS hostname into an IP address, and returns it
in the dot-separated format as a string.
Args:
host: hostname to resolve.
"""
ips = QHostInfo.fromName(host)
if ips.error() != QHostInfo.NoError or not ips.addresses():
err_f = "Failed to resolve host during PAC evaluation: {}"
log.network.info(err_f.format(host))
return QJSValue(QJSValue.NullValue)
else:
return ips.addresses()[0].toString()
示例4: _is_url_dns
def _is_url_dns(url):
"""Check if a URL is really a URL via DNS.
Args:
url: The URL to check for as QUrl, ideally via qurl_from_user_input.
Return:
True if the URL really is a URL, False otherwise.
"""
if not url.isValid():
return False
host = url.host()
log.url.debug("DNS request for {}".format(host))
if not host:
return False
info = QHostInfo.fromName(host)
return not info.error()
示例5: __init__
def __init__(self, parent=None):
super(Client, self).__init__(parent)
self.networkSession = None
self.blockSize = 0
self.currentFortune = ""
hostLabel = QLabel("&Server name:")
portLabel = QLabel("S&erver port:")
self.hostCombo = QComboBox()
self.hostCombo.setEditable(True)
name = QHostInfo.localHostName()
if name != "":
self.hostCombo.addItem(name)
domain = QHostInfo.localDomainName()
if domain != "":
self.hostCombo.addItem(name + "." + domain)
if name != "localhost":
self.hostCombo.addItem("localhost")
ipAddressesList = QNetworkInterface.allAddresses()
for ipAddress in ipAddressesList:
if not ipAddress.isLoopback():
self.hostCombo.addItem(ipAddress.toString())
for ipAddress in ipAddressesList:
if ipAddress.isLoopback():
self.hostCombo.addItem(ipAddress.toString())
self.portLineEdit = QLineEdit()
self.portLineEdit.setValidator(QIntValidator(1, 65535, self))
hostLabel.setBuddy(self.hostCombo)
portLabel.setBuddy(self.portLineEdit)
self.statusLabel = QLabel("This examples requires that you run " "the Fortune Server example as well.")
self.getFortuneButton = QPushButton("Get Fortune")
self.getFortuneButton.setDefault(True)
self.getFortuneButton.setEnabled(False)
quitButton = QPushButton("Quit")
buttonBox = QDialogButtonBox()
buttonBox.addButton(self.getFortuneButton, QDialogButtonBox.ActionRole)
buttonBox.addButton(quitButton, QDialogButtonBox.RejectRole)
self.tcpSocket = QTcpSocket(self)
self.hostCombo.editTextChanged.connect(self.enableGetFortuneButton)
self.portLineEdit.textChanged.connect(self.enableGetFortuneButton)
self.getFortuneButton.clicked.connect(self.requestNewFortune)
quitButton.clicked.connect(self.close)
self.tcpSocket.readyRead.connect(self.readFortune)
self.tcpSocket.error.connect(self.displayError)
mainLayout = QGridLayout()
mainLayout.addWidget(hostLabel, 0, 0)
mainLayout.addWidget(self.hostCombo, 0, 1)
mainLayout.addWidget(portLabel, 1, 0)
mainLayout.addWidget(self.portLineEdit, 1, 1)
mainLayout.addWidget(self.statusLabel, 2, 0, 1, 2)
mainLayout.addWidget(buttonBox, 3, 0, 1, 2)
self.setLayout(mainLayout)
self.setWindowTitle("Fortune Client")
self.portLineEdit.setFocus()
manager = QNetworkConfigurationManager()
if manager.capabilities() & QNetworkConfigurationManager.NetworkSessionRequired:
settings = QSettings(QSettings.UserScope, "QtProject")
settings.beginGroup("QtNetwork")
id = settings.value("DefaultNetworkConfiguration")
settings.endGroup()
config = manager.configurationFromIdentifier(id)
if config.state() & QNetworkConfiguration.Discovered == 0:
config = manager.defaultConfiguration()
self.networkSession = QNetworkSession(config, self)
self.networkSession.opened.connect(self.sessionOpened)
self.getFortuneButton.setEnabled(False)
self.statusLabel.setText("Opening network session.")
self.networkSession.open()
示例6: mock_fromName
def mock_fromName(host):
info = QHostInfo()
if host == "known.domain":
info.setAddresses([QHostAddress("1.2.3.4")])
return info
示例7: __processReadyRead
def __processReadyRead(self):
"""
Private slot to handle the readyRead signal.
"""
if self.__state == Connection.WaitingForGreeting:
if not self.__readProtocolHeader():
return
if self.__currentDataType != Connection.Greeting:
self.abort()
return
self.__state = Connection.ReadingGreeting
if self.__state == Connection.ReadingGreeting:
if not self.__hasEnoughData():
return
self.__buffer = QByteArray(
self.read(self.__numBytesForCurrentDataType))
if self.__buffer.size() != self.__numBytesForCurrentDataType:
self.abort()
return
try:
user, serverPort = \
str(self.__buffer, encoding="utf-8").split(":")
except ValueError:
self.abort()
return
self.__serverPort = int(serverPort)
hostInfo = QHostInfo.fromName(self.peerAddress().toString())
self.__username = "{0}@{1}@{2}".format(
user,
hostInfo.hostName(),
self.peerPort()
)
self.__currentDataType = Connection.Undefined
self.__numBytesForCurrentDataType = 0
self.__buffer.clear()
if not self.isValid():
self.abort()
return
bannedName = "{0}@{1}".format(
user,
hostInfo.hostName(),
)
Preferences.syncPreferences()
if bannedName in Preferences.getCooperation("BannedUsers"):
self.rejected.emit(self.tr(
"* Connection attempted by banned user '{0}'.")
.format(bannedName))
self.abort()
return
if self.__serverPort != self.peerPort() and \
not Preferences.getCooperation("AutoAcceptConnections"):
# don't ask for reverse connections or
# if we shall accept automatically
res = E5MessageBox.yesNo(
None,
self.tr("New Connection"),
self.tr("""<p>Accept connection from """
"""<strong>{0}@{1}</strong>?</p>""").format(
user, hostInfo.hostName()),
yesDefault=True)
if not res:
self.abort()
return
if self.__client is not None:
chatWidget = self.__client.chatWidget()
if chatWidget is not None and not chatWidget.isVisible():
e5App().getObject(
"UserInterface").activateCooperationViewer()
if not self.__isGreetingMessageSent:
self.__sendGreetingMessage()
self.__pingTimer.start()
self.__pongTime.start()
self.__state = Connection.ReadyForUse
self.readyForUse.emit()
while self.bytesAvailable():
if self.__currentDataType == Connection.Undefined:
if not self.__readProtocolHeader():
return
if not self.__hasEnoughData():
return
self.__processData()