The error occurs because in version 7.x of pysnmp the API has changed to use asynchronous coroutines rather than returning a synchronous iterator.
In your code, calling
Try this:
This means that instead of using
In your code, calling
next(x)
on a coroutine is causing the error since you need to await it.Try this:
import asyncio from pysnmp.hlapi.v3arch import * community_string = 'mystring' host = 'my_host' async def snmp_get(): result = await get_cmd( SnmpEngine(), CommunityData(community_string), UdpTransportTarget.create((host, 161)), ContextData(), ObjectType(ObjectIdentity('1.3.6.1.2.1.1.5.0')), # SysName lookupMib=False, lexicographicMode=False, ) return result if __name__ == '__main__': response = asyncio.run(snmp_get()) print(response)Asynchronous API in pysnmp 7.x,the functions
get_cmd
return a coroutine.This means that instead of using
next()
on a synchronous iterator, you must await the coroutine to get the result.asyncio.run()
to execute it,this is the standard way to run asyncio asynchronous code in Python.