1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/chenyanclyz-curspeed

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Клонировать/Скачать
curspeed.py 1.2 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
chenyan Отправлено 28.05.2015 18:01 77e7fa5
#coding:utf8
'''python3 code
author's email: chenyan@feling.net
通过统计ifconfig命令的输出,计算当前网速
'''
import logging
logging.basicConfig(level=logging.INFO,
format='%(message)s',
#filename='speed',
#filemod='w'
)
import os, sys, time
import re
def get_total_tx_bytes(interface, isCN):
grep = '发送字节' if isCN else '"TX bytes"'
r = os.popen('ifconfig '+interface+' | grep '+grep).read()
total_bytes = re.sub('(.+:)| \(.+','',r)
return int(total_bytes)
def get_total_rx_bytes(interface, isCN):
grep = '接收字节' if isCN else '"RX bytes"'
r = os.popen('ifconfig '+interface+' | grep '+grep).read()
total_bytes = re.sub(' \(.+','',r)
total_bytes = re.sub('.+?:','',total_bytes)
return int(total_bytes)
if __name__=='__main__':
interface = sys.argv[1]
get_total_bytes = get_total_tx_bytes if sys.argv[2]=='tx' else get_total_rx_bytes
isCN = True if sys.argv[3]=='cn' else False
freq = int(sys.argv[4])
while True:
last = get_total_bytes(interface, isCN)
time.sleep(freq)
increase = get_total_bytes(interface, isCN) - last
logging.info(str(increase/freq/1000))

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://api.gitlife.ru/oschina-mirror/chenyanclyz-curspeed.git
git@api.gitlife.ru:oschina-mirror/chenyanclyz-curspeed.git
oschina-mirror
chenyanclyz-curspeed
chenyanclyz-curspeed
master