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

使用PySNMP库接收SNMP响应后程序崩溃问题求助

问题:PySNMP接收SNMP响应时触发TypeError错误

在Windows10系统下,使用Python3.10搭配pysnmp 4.4.12编写SNMP GET请求脚本,Wireshark显示请求已发送且收到响应,但程序崩溃,报错TypeError: decodeMessageVersion.<locals>.<lambda>() takes 3 positional arguments but 4 were given。若注释代码中的ContextData(),,请求无法发送,但终端能正常打印start和end。

脚本代码

from pysnmp.hlapi import *

iterator = getCmd(SnmpEngine(),
                  CommunityData('public', mpModel=1),
                  UdpTransportTarget(('172.19.6.41', 161)),
                  ContextData(),
                  ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'))
                  )
print('start')

errorIndication, errorStatus, errorIndex, varBinds = next(iterator)

if errorIndication:  # SNMP engine errors
    print(errorIndication)
else:
    if errorStatus:  # SNMP agent errors
        print('%s at %s' % (errorStatus.prettyPrint(),
                            varBinds[int(errorIndex) - 1] if errorIndex else '?'))
    else:
        for varBind in varBinds:  # SNMP response contents
            print(' = '.join([x.prettyPrint() for x in varBind]))
print('end')

完整报错信息

C:\Users\user\AppData\Local\Programs\Python\Python310\python.exe C:\Projects\pythonProject\OOO\SNMP_examples\PySNMP\04_get_snmp.py 
start
Traceback (most recent call last):
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\carrier\asyncore\dispatch.py", line 45, in runDispatcher
loop(timeout or self.getTimerResolution(),
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 214, in loop
poll_fun(timeout, map)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 157, in poll
read(obj)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 94, in read
obj.handle_error()
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 90, in read
obj.handle_read_event()
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 427, in handle_read_event
self.handle_read()
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\carrier\asyncore\dgram\base.py", line 170, in handle_read
self._cbFun(self, transportAddress, incomingMessage)
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\carrier\base.py", line 84, in _cbFun
self.__recvCallables[recvId](
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\entity\engine.py", line 151, in __receiveMessageCbFun
self.msgAndPduDsp.receiveMessage(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\proto\rfc3412.py", line 291, in receiveMessage
msgVersion = verdec.decodeMessageVersion(wholeMsg)
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\proto\api\verdec.py", line 15, in decodeMessageVersion
seq, wholeMsg = decoder.decode(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pyasn1\codec\ber\decoder.py", line 2003, in __call__
for asn1Object in streamingDecoder:
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pyasn1\codec\ber\decoder.py", line 1918, in __iter__
for asn1Object in self._singleItemDecoder(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pyasn1\codec\ber\decoder.py", line 1778, in __call__
for value in concreteDecoder.valueDecoder(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pyasn1\codec\ber\decoder.py", line 654, in valueDecoder
for chunk in substrateFun(asn1Object, substrate, length, options):
TypeError: decodeMessageVersion.<locals>.<lambda>() takes 3 positional arguments but 4 were given

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Projects\pythonProject\OOO\SNMP_examples\PySNMP\04_get_snmp.py", line 19, in <module>
errorIndication, errorStatus, errorIndex, varBinds = next(iterator)
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\hlapi\asyncore\sync\cmdgen.py", line 113, in getCmd
snmpEngine.transportDispatcher.runDispatcher()
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\carrier\asyncore\dispatch.py", line 50, in runDispatcher
raise PySnmpError('poll error: %s' % ';'.join(format_exception(*exc_info())))
pysnmp.error.PySnmpError: poll error: Traceback (most recent call last):
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\carrier\asyncore\dispatch.py", line 45, in runDispatcher
loop(timeout or self.getTimerResolution(),
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 214, in loop
poll_fun(timeout, map)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 157, in poll
read(obj)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 94, in read
obj.handle_error()
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 90, in read
obj.handle_read_event()
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 427, in handle_read_event
self.handle_read()
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\carrier\asyncore\dgram\base.py", line 170, in handle_read
self._cbFun(self, transportAddress, incomingMessage)
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\carrier\base.py", line 84, in _cbFun
self.__recvCallables[recvId](
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\entity\engine.py", line 151, in __receiveMessageCbFun
self.msgAndPduDsp.receiveMessage(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\proto\rfc3412.py", line 291, in receiveMessage
msgVersion = verdec.decodeMessageVersion(wholeMsg)
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\proto\api\verdec.py", line 15, in decodeMessageVersion
seq, wholeMsg = decoder.decode(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pyasn1\codec\ber\decoder.py", line 2003, in __call__
for asn1Object in streamingDecoder:
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pyasn1\codec\ber\decoder.py", line 1918, in __iter__
for asn1Object in self._singleItemDecoder(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pyasn1\codec\ber\decoder.py", line 1778, in __call__
for value in concreteDecoder.valueDecoder(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pyasn1\codec\ber\decoder.py", line 654, in valueDecoder
for chunk in substrateFun(asn1Object, substrate, length, options): ;TypeError: decodeMessageVersion.<locals>.<lambda>() takes 3 positional arguments but 4 were given
caused by <class 'TypeError'>: decodeMessageVersion.<locals>.<lambda>() takes 3 positional arguments but 4 were given

Process finished with exit code 1

解决方案

  • 问题根源:pysnmp 4.4.12与高版本pyasn1不兼容。该版本的pysnmp依赖的pyasn1接口参数与新版pyasn1的解码器接口不匹配,导致lambda函数接收参数数量错误。
  • 修复步骤:
    1. 卸载当前pyasn1版本:
      pip uninstall pyasn1
      
    2. 安装与pysnmp 4.4.12兼容的pyasn1版本(推荐0.4.8):
      pip install pyasn1==0.4.8
      
  • 关于ContextData():对于SNMPv2c(mpModel=1),ContextData()是可选参数,但无需注释它,在兼容的依赖环境下即可正常运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 16:02:31