qt 网络类,获取网络配置信息.
QtNetwork Module
示例
qtnetwork_test.py
#!/usr/bin/env python
# encoding: utf-8
"""
@version :
@author :
@license :
@contact : jiang_xmin@massclouds.com
@site : http://blog.csdn.net/jxm_csdn
@software: PyCharm
@time : 16-11-12 下午1:28
"""
import sys
from PyQt4.QtGui import QApplication
from PyQt4.QtNetwork import QHostAddress,QAbstractSocket,\
QNetworkInterface,QNetworkAddressEntry,\
QHostInfo,QNetworkConfiguration,QNetworkConfigurationManager
if __name__ == '__main__':
app = QApplication(sys.argv)
address = ""
localHostName = QHostInfo.localHostName()
hostinfo= QHostInfo.fromName(localHostName)
listaddress = hostinfo.addresses()
for address in listaddress:
if QHostAddress(address).protocol() == QAbstractSocket.IPv4Protocol:
address = QHostAddress(address).toString()
print address
confList = QNetworkConfigurationManager().allConfigurations()
print confList.__len__()
for conf in confList:
if str(conf.bearerTypeName()) == "Ethernet":
print ""
print "name : ", QNetworkConfiguration(conf).name()
# print "bearerType : " ,
# if QNetworkConfiguration(conf).bearerType() == QNetworkConfiguration.BearerEthernet:
# print "BearerEthernet"
# if QNetworkConfiguration(conf).bearerType() == QNetworkConfiguration.BearerWLAN:
# print "BearerWLAN"
print QNetworkConfiguration(conf).isValid()
print "bearerType : ", QNetworkConfiguration(conf).bearerTypeName()
list = QNetworkInterface.allInterfaces()
for interface in list:
print ""
print "============================="
# if str(interface.name()) == "lo":
# continue
# if str(interface.name()) == "virbr0":
# continue
print "name : ", interface.name()
print QNetworkInterface(interface).hardwareAddress()
print QNetworkInterface(interface).isValid()
print "---------"
if QNetworkInterface(interface).flags() & QNetworkInterface.IsUp:
print "Interface : is up"
if QNetworkInterface(interface).flags() & QNetworkInterface.IsLoopBack:
print "Interface : is loop back" # 回环地址
if QNetworkInterface(interface).flags() & QNetworkInterface.IsRunning:
print "Interface : is running " # 网络已经启动运行
if interface.flags() & QNetworkInterface.CanMulticast:
print "Interface : CanMulticast" # 多播
if interface.flags() & QNetworkInterface.CanBroadcast:
print "Interface : CanBroadcast"
print "---------"
entryList = QNetworkInterface(interface).addressEntries()
for entry in entryList:
address = entry.ip()
if QHostAddress(address).protocol() == QAbstractSocket.IPv4Protocol:# and \
# str(address.toString()) != "127.0.0.1":
print "IP Address:", QNetworkAddressEntry(entry).ip().toString()
print "Netmask:", QNetworkAddressEntry(entry).netmask().toString()
print "Broadcast:", QNetworkAddressEntry(entry).broadcast().toString()
print "============================="
运行结果
$ python qtnetwork_test.py
192.168.100.182
34
name : virbr0
True
bearerType : Ethernet
name : eth0
True
bearerType : Ethernet
name : wlan0
True
bearerType : Ethernet
=============================
name : lo
00:00:00:00:00:00
True
---------
Interface : is up
Interface : is loop back
Interface : is running
---------
IP Address: 127.0.0.1
Netmask: 255.0.0.0
Broadcast: 127.255.255.255
=============================
=============================
name : eth0
5C:FF:35:0A:D4:EC
True
---------
Interface : is up
Interface : is running
Interface : CanMulticast
Interface : CanBroadcast
---------
IP Address: 192.168.100.182
Netmask: 255.255.255.0
Broadcast: 192.168.100.255
=============================
=============================
name : wlan0
00:23:14:AD:27:D4
True
---------
Interface : is up
Interface : is running
Interface : CanMulticast
Interface : CanBroadcast
---------
IP Address: 10.0.23.102
Netmask: 255.255.255.0
Broadcast: 10.0.23.255
=============================
=============================
name : virbr0
FA:52:2F:B0:56:64
True
---------
Interface : is up
Interface : CanMulticast
Interface : CanBroadcast
---------
IP Address: 192.168.122.1
Netmask: 255.255.255.0
Broadcast: 192.168.122.255
=============================