我想每秒更新一次变量。我可以使用Thread通过time.sleep(1)做到这一点。我也想在表(from terminaltables import AsciiTable
)中使用变量sys.stdout.write
是无法实现的。如何每秒钟更新一次变量并打印
#-*-coding:utf-8-*-
import os, subprocess
from scapy import *
import time
import sys
import threading
import keyboard
from time import gmtime, strftime
import read_config as readConfig
import curses
from terminaltables import AsciiTable
# Available fields for nmcli
fields = ('NAME', 'SSID', 'SSID-HEX', 'BSSID', 'MODE', 'CHAN', 'SIGNAL', 'DEVICE', 'DBUS-PATH')
def nmcliGathering():
cmd = ['nmcli -f NAME,ACTIVE dev wifi list | awk \'$2 ~ /yes/ {print $1}\'']
addr = subprocess.Popen(cmd, stdout = subprocess.PIPE, shell = True)
(out, err) = a
ddr.communicate()
out = str(out)
out = out.replace('b\'', '')
out = out.replace('\\n', '')
name = out.replace('\'', '')
cmd = ['nmcli -f SSID,ACTIVE dev wifi list | awk \'$2 ~ /yes/ {print $1}\'']
addr = subprocess.Popen(cmd, stdout = subprocess.PIPE, shell = True)
(out, err) = addr.communicate()
out = str(out)
out = out.replace('b\'', '')
out = out.replace('\\n', '')
ssid = out.replace('\'', '')
cmd = ['nmcli -f BSSID,ACTIVE dev wifi list | awk \'$2 ~ /yes/ {print $1}\'']
addr = subprocess.Popen(cmd, stdout = subprocess.PIPE, shell = True)
(out, err) = addr.communicate()
out = str(out)
out = out.replace('b\'', '')
out = out.replace('\\n', '')
bssid = out.replace('\'', '')
cmd = ['nmcli -f MODE,ACTIVE dev wifi list | awk \'$2 ~ /yes/ {print $1}\'']
addr = subprocess.Popen(cmd, stdout = subprocess.PIPE, shell = True)
(out, err) = addr.communicate()
out = str(out)
out = out.replace('b\'', '')
out = out.replace('\\n', '')
mode = out.replace('\'', '')
cmd = ['nmcli -f CHAN,ACTIVE dev wifi list | awk \'$2 ~ /yes/ {print $1}\'']
addr = subprocess.Popen(cmd, stdout = subprocess.PIPE, shell = True)
(out, err) = addr.communicate()
out = str(out)
out = out.replace('b\'', '')
out = out.replace('\\n', '')
chan = out.replace('\'', '')
cmd = ['nmcli -f SIGNAL,ACTIVE dev wifi list | awk \'$2 ~ /yes/ {print $1}\'']
addr = subprocess.Popen(cmd, stdout = subprocess.PIPE, shell = True)
(out, err) = addr.communicate()
out = str(out)
out = out.replace('b\'', '')
out = out.replace('\\n', '')
signal = out.replace('\'', '')
cmd = ['nmcli -f DEVICE,ACTIVE dev wifi list | awk \'$2 ~ /yes/ {print $1}\'']
addr = subprocess.Popen(cmd, stdout = subprocess.PIPE, shell = True)
(out, err) = addr.communicate()
out = str(out)
out = out.replace('b\'', '')
out = out.replace('\\n', '')
device = out.replace('\'', '')
table_data = [['TIME', 'NAME', 'SSID', 'BSSID', 'MODE', 'CHAN', 'SIGNAL', 'DEVICE'],
[strftime("%H:%M:%S", gmtime()), name, ssid, bssid, mode, chan, signal, device]]
table = AsciiTable(table_data)
print(table.table)
if __name__ == '__main__':
nmcliGathering()
更新:
我想做的一个简单例子
import sys, time
i = 0
while i < 5:
sys.stdout.write('\r{0}'.format(i))
sys.stdout.flush()
i = i + 1
time.sleep(1)
在我的代码中,我想像在示例代码中一样打印table.table