使用web.py和pysnmp进行Python线程创建Web服务

时间:2012-11-20 19:06:21

标签: python json multithreading web.py pysnmp

我正在尝试为我的家庭网络创建一个小型服务,该服务从我的交换机读取路由器所在端口的SNMP值,进行一些计算并返回当前的平均带宽利用率。

我让pysnmp运行良好,每两秒读一次snmp值...然后我决定整合web.py来创建一个非常简单的简单Web服务,返回上传和下载平均值的JSON,以千比特为单位。

要做到这一点,我必须使用线程来保持SNMP轮询在后台进行。我虽然是NOOOB而且它无法正常工作 - 当我在浏览器中加载值时,由于某种原因似乎创建了SNMP进程的第二个线程,我无法弄清楚原因。

以下是代码:

from pysnmp.entity.rfc3413.oneliner import cmdgen
from time import sleep
import threading
import json
import web

LOCK = threading.Lock()

class getBW(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self.data = dict(upload="0",download="0",)
        self.lock = LOCK
        self.start()

    def run(self):

        router_ip = "192.168.2.2"
        snmp_community = "public"
        upload_mib = "1.3.6.1.2.1.2.2.1.16.1"
        download_mib = "1.3.6.1.2.1.2.2.1.10.1"
        read_interval = 2
        up_octets = 0
        dn_octets = 0
        last_up_octets = 0
        last_dn_octets = 0
        up_change = 0
        dn_change = 0
        avg_up =0
        avg_dn =0
        up_hist = [0,0,0,0,0] 
        dn_hist = [0,0,0,0,0]
        i = 0
        cmdGen = cmdgen.CommandGenerator()

        while (1):
            errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
                cmdgen.CommunityData(snmp_community),
                cmdgen.UdpTransportTarget((router_ip, 161)),
                upload_mib,
                download_mib
            )

            # Check for errors and print out results
            if errorIndication:
                print(errorIndication)
            else:
                if errorStatus:
                    print('%s at %s' % (
                        errorStatus.prettyPrint(),
                        errorIndex and varBinds[int(errorIndex)-1] or '?'
                        )
                    )
                else:
                    for name, val in varBinds:
                        if str(name) == upload_mib:
                            if up_octets > 0: 
                                last_up_octets = up_octets
                                up_change = val-last_up_octets
                                # convert to kilobits/s
                                up_change = (((up_change*8)/1000)/read_interval)
                                up_hist[i] = up_change
                                avg_up=(sum(up_hist)/len(up_hist))
                            up_octets = val
                            #print ('%d kilobit per second upload, %d average' % (up_change,avg_up))

                        elif str(name) == download_mib:
                            if dn_octets > 0:
                                last_dn_octets = dn_octets
                                dn_change = val-last_dn_octets
                                # convert to kilobits/s
                                dn_change = (((dn_change*8)/1000)/read_interval)
                                dn_hist[i] = dn_change
                                avg_dn=(sum(dn_hist)/len(dn_hist))
                                i += 1
                                i = i % 5 # if I is 5 reset to 0
                            dn_octets = val
                            #print ('%d kilobit per second download, %d average' % (dn_change,avg_dn))

            #output values to dict
            self.lock.acquire()
            self.data = dict(upload=str(avg_up),download=str(avg_dn),)
            self.lock.release()
            print("UPLOAD: %d change, %d average; DOWNLOAD: %d change, %d average" % (up_change,avg_up,dn_change,avg_dn))
            print("sleeping for %i secs...\r\n" % read_interval)
            sleep(read_interval)

try:
    BW = getBW()
except:
    print "Error: unable to start thread"

urls = ("/.*", "hello")
app = web.application(urls, globals())

class hello:
    def __init__(self):
        self.lock = LOCK
    def GET(self):
        self.lock.acquire()
        thedata = BW.data
        self.lock.release()
        return json.dumps(thedata)

if __name__ == "__main__":
    #start web service
    app.run()

我真的很感谢你的帮助。请记住,我是线程的总菜鸟,只是学习Python。谢谢!

1 个答案:

答案 0 :(得分:1)

您正在使用web.py的内置网络服务器。默认情况下,它启用“模块重新加载”,重新加载器加载主模块两次。由于您在导入时启动线程,因此会运行两个线程。

一个简单的解决方法是将线程创建移动到__main__块,这样它只创建一次:

if __name__ == "__main__":
    BW = getBW()
    #start web service
    app.run()