本文整理汇总了Python中psutil.net_io_counters方法的典型用法代码示例。如果您正苦于以下问题:Python psutil.net_io_counters方法的具体用法?Python psutil.net_io_counters怎么用?Python psutil.net_io_counters使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类psutil
的用法示例。
在下文中一共展示了psutil.net_io_counters方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getNetworkInfo
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import net_io_counters [as 别名]
def getNetworkInfo(cls):
interfaces = {}
stats = psutil.net_if_stats()
for key, value in six.iteritems(stats):
interfaces[key] = value.__dict__
interfaces[key]['addresses'] = []
interfaces[key]['io_counters'] = None
addresses = psutil.net_if_addrs()
for key, value in six.iteritems(addresses):
if key not in interfaces:
continue
for addr in value:
interfaces[key]['addresses'].append(addr.__dict__)
traffic = psutil.net_io_counters(pernic=True)
for key, value in six.iteritems(traffic):
if key not in interfaces:
continue
interfaces[key]['io_counters'] = value.__dict__
return interfaces
示例2: getSystemInfo
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import net_io_counters [as 别名]
def getSystemInfo(cls):
system = {
'boot_time': psutil.boot_time(),
'cpu_count': psutil.cpu_count(),
'cpu_stats': psutil.cpu_stats().__dict__,
'cpu_times': [k.__dict__ for k in psutil.cpu_times(percpu=True)],
'disk_io_counters': psutil.disk_io_counters().__dict__,
'disk_usage': [],
'net_io_counters': psutil.net_io_counters().__dict__,
'swap_memory': psutil.swap_memory().__dict__,
'virtual_memory': psutil.virtual_memory().__dict__
}
partitions = psutil.disk_partitions()
for p in partitions:
if p.mountpoint in cls.INCLUDED_PARTITIONS:
usage = psutil.disk_usage(p.mountpoint)
system['disk_usage'].append({
'mountpoint': p.mountpoint,
'total': usage.total,
'used': usage.used
})
return system
示例3: net_io_usage
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import net_io_counters [as 别名]
def net_io_usage():
global _last_net_io_meta
net_counters = psutil.net_io_counters()
tst = time.time()
send_bytes = net_counters.bytes_sent
recv_bytes = net_counters.bytes_recv
if _last_net_io_meta is None:
_last_net_io_meta = (send_bytes, recv_bytes, tst)
return None
last_send_bytes, last_recv_bytes, last_time = _last_net_io_meta
delta_time = tst - last_time
recv_speed = (recv_bytes - last_recv_bytes) / delta_time
send_speed = (send_bytes - last_send_bytes) / delta_time
_last_net_io_meta = (send_bytes, recv_bytes, tst)
return recv_speed, send_speed
示例4: test_serialization
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import net_io_counters [as 别名]
def test_serialization(self):
def check(ret):
if json is not None:
json.loads(json.dumps(ret))
a = pickle.dumps(ret)
b = pickle.loads(a)
self.assertEqual(ret, b)
check(psutil.Process().as_dict())
check(psutil.virtual_memory())
check(psutil.swap_memory())
check(psutil.cpu_times())
check(psutil.cpu_times_percent(interval=0))
check(psutil.net_io_counters())
if LINUX and not os.path.exists('/proc/diskstats'):
pass
else:
if not APPVEYOR:
check(psutil.disk_io_counters())
check(psutil.disk_partitions())
check(psutil.disk_usage(os.getcwd()))
check(psutil.users())
示例5: test_cache_clear_public_apis
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import net_io_counters [as 别名]
def test_cache_clear_public_apis(self):
psutil.disk_io_counters()
psutil.net_io_counters()
caches = wrap_numbers.cache_info()
for cache in caches:
self.assertIn('psutil.disk_io_counters', cache)
self.assertIn('psutil.net_io_counters', cache)
psutil.disk_io_counters.cache_clear()
caches = wrap_numbers.cache_info()
for cache in caches:
self.assertIn('psutil.net_io_counters', cache)
self.assertNotIn('psutil.disk_io_counters', cache)
psutil.net_io_counters.cache_clear()
caches = wrap_numbers.cache_info()
self.assertEqual(caches, ({}, {}, {}))
# ===================================================================
# --- Example script tests
# ===================================================================
示例6: test_procfs_path
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import net_io_counters [as 别名]
def test_procfs_path(self):
tdir = tempfile.mkdtemp()
try:
psutil.PROCFS_PATH = tdir
self.assertRaises(IOError, psutil.virtual_memory)
self.assertRaises(IOError, psutil.cpu_times)
self.assertRaises(IOError, psutil.cpu_times, percpu=True)
self.assertRaises(IOError, psutil.boot_time)
# self.assertRaises(IOError, psutil.pids)
self.assertRaises(IOError, psutil.net_connections)
self.assertRaises(IOError, psutil.net_io_counters)
self.assertRaises(IOError, psutil.net_if_stats)
self.assertRaises(IOError, psutil.disk_io_counters)
self.assertRaises(IOError, psutil.disk_partitions)
self.assertRaises(psutil.NoSuchProcess, psutil.Process)
finally:
psutil.PROCFS_PATH = "/proc"
os.rmdir(tdir)
示例7: test_cache_clear_public_apis
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import net_io_counters [as 别名]
def test_cache_clear_public_apis(self):
if not psutil.disk_io_counters() or not psutil.net_io_counters():
return self.skipTest("no disks or NICs available")
psutil.disk_io_counters()
psutil.net_io_counters()
caches = wrap_numbers.cache_info()
for cache in caches:
self.assertIn('psutil.disk_io_counters', cache)
self.assertIn('psutil.net_io_counters', cache)
psutil.disk_io_counters.cache_clear()
caches = wrap_numbers.cache_info()
for cache in caches:
self.assertIn('psutil.net_io_counters', cache)
self.assertNotIn('psutil.disk_io_counters', cache)
psutil.net_io_counters.cache_clear()
caches = wrap_numbers.cache_info()
self.assertEqual(caches, ({}, {}, {}))
# ===================================================================
# --- Example script tests
# ===================================================================
示例8: test_procfs_path
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import net_io_counters [as 别名]
def test_procfs_path(self):
tdir = self.get_testfn()
os.mkdir(tdir)
try:
psutil.PROCFS_PATH = tdir
self.assertRaises(IOError, psutil.virtual_memory)
self.assertRaises(IOError, psutil.cpu_times)
self.assertRaises(IOError, psutil.cpu_times, percpu=True)
self.assertRaises(IOError, psutil.boot_time)
# self.assertRaises(IOError, psutil.pids)
self.assertRaises(IOError, psutil.net_connections)
self.assertRaises(IOError, psutil.net_io_counters)
self.assertRaises(IOError, psutil.net_if_stats)
# self.assertRaises(IOError, psutil.disk_io_counters)
self.assertRaises(IOError, psutil.disk_partitions)
self.assertRaises(psutil.NoSuchProcess, psutil.Process)
finally:
psutil.PROCFS_PATH = "/proc"
示例9: stream_host_stats
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import net_io_counters [as 别名]
def stream_host_stats():
while True:
net = psutil.net_io_counters(pernic=True)
time.sleep(1)
net1 = psutil.net_io_counters(pernic=True)
net_stat_download = {}
net_stat_upload = {}
for k, v in net.items():
for k1, v1 in net1.items():
if k1 == k:
net_stat_download[k] = (v1.bytes_recv - v.bytes_recv) / 1000.
net_stat_upload[k] = (v1.bytes_sent - v.bytes_sent) / 1000.
ds = statvfs('/')
disk_str = {"Used": ((ds.f_blocks - ds.f_bfree) * ds.f_frsize) / 10 ** 9, "Unused": (ds.f_bavail * ds.f_frsize) / 10 ** 9}
yield '[{"cpu":"%s","memory":"%s","memTotal":"%s","net_stats_down":"%s","net_stats_up":"%s","disk":"%s"}],' \
% (psutil.cpu_percent(interval=1), psutil.virtual_memory().used, psutil.virtual_memory().free, \
net_stat_download, net_stat_upload, disk_str)
示例10: get_bandwidth
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import net_io_counters [as 别名]
def get_bandwidth():
# Get net in/out
net1_out = psutil.net_io_counters().bytes_sent
net1_in = psutil.net_io_counters().bytes_recv
time.sleep(1)
# Get new net in/out
net2_out = psutil.net_io_counters().bytes_sent
net2_in = psutil.net_io_counters().bytes_recv
# Compare and get current speed
if net1_in > net2_in:
current_in = 0
else:
current_in = net2_in - net1_in
if net1_out > net2_out:
current_out = 0
else:
current_out = net2_out - net1_out
network = {"traffic_in" : current_in, "traffic_out" : current_out}
return network
示例11: json_charts
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import net_io_counters [as 别名]
def json_charts(self, req):
""" Return charting data.
"""
disk_used, disk_total, disk_detail = 0, 0, []
for disk_usage_path in self.cfg.disk_usage_path.split(os.pathsep):
disk_usage = self.guarded(psutil.disk_usage, os.path.expanduser(disk_usage_path.strip()))
if disk_usage:
disk_used += disk_usage.used
disk_total += disk_usage.total
disk_detail.append((disk_usage.used, disk_usage.total))
data = dict(
engine = self.json_engine(req),
uptime = time.time() - psutil.BOOT_TIME, # pylint: disable=no-member
fqdn = self.guarded(socket.getfqdn),
cpu_usage = self.guarded(psutil.cpu_percent, 0),
ram_usage = self.guarded(psutil.virtual_memory),
swap_usage = self.guarded(psutil.swap_memory),
disk_usage = (disk_used, disk_total, disk_detail) if disk_total else None,
disk_io = self.guarded(psutil.disk_io_counters),
net_io = self.guarded(psutil.net_io_counters),
)
return data
示例12: update
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import net_io_counters [as 别名]
def update(self):
"""Function to update the entire class information."""
self.cpu["percentage"] = psutil.cpu_percent(interval=0.7)
self.boot = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime(
"%Y-%m-%d %H:%M:%S")
virtual_memory = psutil.virtual_memory()
self.memory["used"] = int(virtual_memory.used/1024)
self.memory["free"] = int(virtual_memory.free/1024)
self.memory["cached"] = int(virtual_memory.cached/1024)
net_io_counters = psutil.net_io_counters()
self.network["packet_sent"] = net_io_counters.packets_sent
self.network["packet_recv"] = net_io_counters.packets_recv
disk_usage = psutil.disk_usage('/')
self.disk["total"] = int(disk_usage.total/1024)
self.disk["used"] = int(disk_usage.used/1024)
self.disk["free"] = int(disk_usage.free/1024)
self.timestamp = time.time()
示例13: get_network
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import net_io_counters [as 别名]
def get_network():
network = psutil.net_io_counters(pernic=True)
ifaces = psutil.net_if_addrs()
networks = list()
for k, v in ifaces.items():
ip = v[0].address
data = network[k]
ifnet = dict()
ifnet['ip'] = ip
ifnet['iface'] = k
ifnet['sent'] = '%.2fMB' % (data.bytes_sent/1024/1024)
ifnet['recv'] = '%.2fMB' % (data.bytes_recv/1024/1024)
ifnet['packets_sent'] = data.packets_sent
ifnet['packets_recv'] = data.packets_recv
ifnet['errin'] = data.errin
ifnet['errout'] = data.errout
ifnet['dropin'] = data.dropin
ifnet['dropout'] = data.dropout
networks.append(ifnet)
return networks
示例14: update
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import net_io_counters [as 别名]
def update(self):
for iface, newio in psutil.net_io_counters(True).items():
if not iface.startswith('lo'):
netinfo = netifaces.ifaddresses(iface)
if netinfo.get(netifaces.AF_INET) and not self._is_ignored(iface):
newio = self._net_io_counters(newio)
newio['iface'] = iface
newio.update(netinfo[netifaces.AF_INET][0])
self._deltas(self.nics.get(iface,{}), newio)
self.nics[iface] = newio
elif iface in self.nics:
del self.nics[iface]
self.data['nics'] = sorted(self.nics.values(), key=lambda n:n['iface'])
self.data['total'] = self._deltas(self.data.get('total',{}), self._net_io_counters())
super(Plugin, self).update()
示例15: _net_io_counters
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import net_io_counters [as 别名]
def _net_io_counters(self, io=None):
io = io or psutil.net_io_counters()
return {
'bytes_sent': io.bytes_sent,
'bytes_recv': io.bytes_recv,
'packets_sent': io.packets_sent,
'packets_recv': io.packets_recv,
'errin': io.errin,
'errout': io.errout,
'dropin': io.dropin,
'dropout': io.dropout,
}