获取网络带宽并使用子进程写入数据 (Python : v3.8.x)
Get Network Bandwidth and write the data using subprocess (Python : v3.8.x)
我正在尝试使用任何命令监控网络带宽,运行 它使用子进程,然后以 json 格式为我的 API 加载数据。
我目前遇到了 3 个终端命令。
1- vnstat
2- 快
3- nload
快速提供实时网络带宽更好。但 nload 也以清晰的方式提供实时数据。如果我使用子进程,我如何在这两种情况下获取数据,这样我的 API 就不会遇到请求超时错误。??
我基本上是在尝试实现命令的子进程输出并以 json 格式编写。
cmd=['vnstat']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
output = p.stdout.read().decode('utf-8')
p.stdout.close()
status = p.returncode
if status is None:
network_bandwidth = {"network_bandwidth": {}}
for line in output.splitlines():
each_line = line.split()
network_bandwidth["network_bandwidth"] = {"key":"Here i did the processing part where i extracted the received and transmitted data and prepared my json response"}
else:
logger.error("Error in executing the command '{}'".format(cmd))
return network_bandwidth
我正在尝试使用任何命令监控网络带宽,运行 它使用子进程,然后以 json 格式为我的 API 加载数据。 我目前遇到了 3 个终端命令。 1- vnstat 2- 快 3- nload
快速提供实时网络带宽更好。但 nload 也以清晰的方式提供实时数据。如果我使用子进程,我如何在这两种情况下获取数据,这样我的 API 就不会遇到请求超时错误。??
我基本上是在尝试实现命令的子进程输出并以 json 格式编写。
cmd=['vnstat']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
output = p.stdout.read().decode('utf-8')
p.stdout.close()
status = p.returncode
if status is None:
network_bandwidth = {"network_bandwidth": {}}
for line in output.splitlines():
each_line = line.split()
network_bandwidth["network_bandwidth"] = {"key":"Here i did the processing part where i extracted the received and transmitted data and prepared my json response"}
else:
logger.error("Error in executing the command '{}'".format(cmd))
return network_bandwidth