You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pysnmp获取Windows机器RAM占用时报TypeError错误求助

问题:使用pysnmp获取Windows RAM占用时出现TypeError错误

错误信息

line 38, in getRamUsage
    error_indication, error_status, error_index, var_binds = next(iterator)
TypeError: 'int' object is not an iterator

原代码

from pysnmp.hlapi.asyncore import *

def getRamUsage():
    iterator = getCmd(SnmpEngine(),
                      CommunityData('public'),
                      UdpTransportTarget(('192.168.1.10', 161)),
                      ContextData(),
                      ObjectType(ObjectIdentity('1.3.6.1.2.1.25.2.3.1.6.2')),
                      lexicographicMode=False)
    error_indication, error_status, error_index, var_binds = next(iterator)
    if error_indication:
        print('Error: %s' % error_indication)
    elif error_status:
        print('Error: %s at %s' % (error_status.prettyPrint(), error_index and var_binds[int(error_index) - 1][0] or '?'))
    else:
        for var_bind in var_binds:
            print('RAM usage: %s' % var_bind.prettyPrint())

修复方案

问题根源

你导入的是pysnmp.hlapi.asyncore下的异步版本getCmd,它不返回迭代器,而是需要通过回调函数处理结果,直接用next()调用会报错,因为它实际返回的是一个整数状态码。

方法1:改用同步版本的getCmd

直接从pysnmp.hlapi导入同步实现,它会返回可迭代对象,原代码的next()逻辑可以正常使用:

from pysnmp.hlapi import *

def getRamUsage():
    iterator = getCmd(SnmpEngine(),
                      CommunityData('public'),
                      UdpTransportTarget(('192.168.1.10', 161)),
                      ContextData(),
                      ObjectType(ObjectIdentity('1.3.6.1.2.1.25.2.3.1.6.2')),
                      lexicographicMode=False)
    error_indication, error_status, error_index, var_binds = next(iterator)
    if error_indication:
        print('Error: %s' % error_indication)
    elif error_status:
        print('Error: %s at %s' % (error_status.prettyPrint(), error_index and var_binds[int(error_index) - 1][0] or '?'))
    else:
        for var_bind in var_binds:
            print('RAM usage: %s' % var_bind.prettyPrint())

方法2:如果需要异步调用,改用回调模式

如果必须用异步版本,需要给getCmd传入回调函数来处理SNMP响应,不能用next():

from pysnmp.hlapi.asyncore import *

def cbFun(snmpEngine, sendRequestHandle, errorIndication, errorStatus, errorIndex, varBinds, cbCtx):
    if errorIndication:
        print(errorIndication)
    elif errorStatus:
        print(f'{errorStatus.prettyPrint()} at {errorIndex and varBinds[int(errorIndex)-1][0] or "?"}')
    else:
        for varBind in varBinds:
            print(f'RAM usage: {varBind.prettyPrint()}')

def getRamUsage():
    snmpEngine = SnmpEngine()
    getCmd(snmpEngine,
           CommunityData('public'),
           UdpTransportTarget(('192.168.1.10', 161)),
           ContextData(),
           ObjectType(ObjectIdentity('1.3.6.1.2.1.25.2.3.1.6.2')),
           lexicographicMode=False,
           cbFun=cbFun)
    snmpEngine.transportDispatcher.runDispatcher()

内容的提问来源于stack exchange,提问作者Niklas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 15:27:37