Python是一種廣泛使用的編程語言,具有強大的監控吞吐量功能。在網絡通信過程中,可以使用Python編程語言監控數據的傳輸速度和數據包的丟失情況。以下是一些使用Python監視吞吐量的示例。
import time import psutil def collect_stat(): stat = psutil.net_io_counters() return (stat.bytes_sent, stat.bytes_recv) def display_stat(send_total, recv_total, start_time): print("Total sent: {}, Total recv: {}, Total time: {}".format( humanize_bytes(send_total), humanize_bytes(recv_total), time.time() - start_time)) def humanize_bytes(bytesize, precision=2): abbrevs = ( (1<< 50, 'PB'), (1<< 40, 'TB'), (1<< 30, 'GB'), (1<< 20, 'MB'), (1<< 10, 'KB'), (1, 'B') ) if bytesize == 1: return '1 B' for factor, suffix in abbrevs: if bytesize >= factor: break return '%.*f %s' % (precision, float(bytesize) / factor, suffix) if __name__ == '__main__': send_last, recv_last = collect_stat() start_time = time.time() while True: time.sleep(1) send_new, recv_new = collect_stat() display_stat((send_new - send_last), (recv_new - recv_last), start_time) send_last, recv_last = send_new, recv_new
這個Python監視工具用psutil模塊來收集網絡輸入/輸出的流量數據,并使用time.sleep方法設置間隔訪問這個網絡數據。每秒鐘檢查一次網絡流量并顯示總吞吐量。
上一篇oracle 0144