我想用 raspberry pi 创建一个 wlan,它将是 运行 nodejs express 框架来在浏览器中播放视频

I want to create a wlan with raspberry pi which will be running nodejs express framework to play videos in browser

如何跟踪 WLAN 上每个 IP 的当前网络速度? 我正在做一个项目,用 raspberry pi 创建一个 WLAN,它将是 运行 nodejs express 框架,可以在连接到它的智能手机的浏览器中播放视频,但我需要知道我的 raspberry pi 之间的网络速度]e 服务器和每个客户端 ip,以便我可以根据网络速度更改视频质量。

我建议您使用像 NFDUMP 这样的 Netflow/IPFIX 监视器来记录 clients/servers 之间的吞吐量数据。然后,您可以在本地处理此数据或将数据发送到远程收集器进行处理。

您还可以在链中放置一个代理(nginx 或 squid)以允许更严格的节流。

除此之外,您还需要启用 kernel/iptables 设置以允许 IP 转发。

如果 RasPi 负担重,此解决方案应该能够允许将工作卸载到外部服务器,并且还允许扩展(许多 pis)。

使用 iptables 创建一个新链,并将其附加到 iptables.Create 规则的输出链中,用于 arp table 中的每个 ip 地址,并使用 iptables -L custom_chain_name -vnx 查看使用的数据,然后通过测量 time.Look 一定时间间隔后使用的数据来计算速度,下面的 python 代码会为你做所有事情。

def reset_iptables():
 call("iptables -t filter -D OUTPUT -j ltgauge",shell=True)
 call("iptables --flush ltgauge",shell=True)
 call("iptables -X ltgauge",shell=True)
#this function fetches devices information from arp_table
def fetch_arptable():
 with open(r"/proc/net/arp","r") as arp_table:
     arp_table_data=arp_table.readlines()

bucket=[]      
size=len(arp_table_data)
for i in range(1,size):
    bucket.append(arp_table_data[i].split())

devices=[]
for item in bucket:
        device_detail={"IP address":item[0],"HW address":item[3],"network_device":item[5]}
        devices.append(device_detail)

return devices
def getConnectedDeviceMacIP(interface):
 active_devices=[]
 for device in ARPTABLE:
     if device["network_device"]==interface:
         active_devices.append([device["HW address"],device["IP address"]])
 return active_devices


def createIptablerule():
    for item in macIP_list:
       status=call("iptables -C ltgauge -d "+item[1]+" -j RETURN > /dev/null",shell=True)
       if status==1:
           call("iptables -A ltgauge -d "+item[1]+" -j RETURN > /dev/null",shell=True)
           print "rule created \n"

 def fetch_data():
    info={}
    for item in macIP_list:
        command=check_output("iptables -L ltgauge -vnx | grep "+item[1],shell=True)
        edata=[]
        edata=command.split()
        info[item[1]]=edata[1]
   return info`

from subprocess import call,check_output
import datetime
from time import sleep

interface="wlp2s0"
final_db={}
data={}
reset_iptables()

call("iptables -t filter -N ltgauge",shell=True)
call("iptables -t filter -A OUTPUT -j ltgauge",shell=True)

while(True):
    call("tput reset",shell=True)
    ARPTABLE=fetch_arptable()
    macIP_list=getConnectedDeviceMacIP(interface)
    createIptablerule()
    data=fetch_data()
    check=datetime.datetime.now()
    for item in macIP_list:
        mac=item[0]
        ip=item[1]
        byte=int(data[ip])
        if item[0] in final_db:
            total_data = final_db[mac]["total"]+byte
            final_db[mac]["total"] = total_data
            last_update_time = final_db[mac]["updation_time"]
            current_time = datetime.datetime.now()
            time_gap = (current_time-final_db[mac]["updation_time"]).total_seconds()
            speed = byte/time_gap
            final_db[mac]["speed"] = speed
            final_db[mac]["updation_time"] = current_time
         else:
            new_device={"total":byte,"updation_time":datetime.datetime.now(),"speed":0}
        final_db[mac]=new_device

     print "\tmac \t   current_IP\t Total \t speed"

    for item in macIP_list:
        print item[0]+"  "+item[1]+"\t"+str(final_db[item[0]]["total"])+" \t "+str(final_db[item[0]]["speed"])
    sleep(1)