使用 nameko 为 Pysnmp get_request--Attempted "nameko_entrypoints" 操作在 ASN.1 模式对象上创建服务时出错

Error while creating a service using nameko for Pysnmp get_request--Attempted "nameko_entrypoints" operation on ASN.1 schema object

我是 pysnmp 和 nameko 的新手。我一直在分配一项工作,在 nameko 框架中创建一个服务,以使用 pysnmp 库执行 snmp get_request。

下面是我试过的代码

from pysnmp.hlapi import *
from nameko.rpc import rpc


class GreetingService(object):
    name = "greeting_service"

    @rpc
    def getFunc(oid):
        errorIndication, errorStatus, errorIndex, varBinds = next(
            getCmd(SnmpEngine(),
                   CommunityData('public', mpModel=0),
                   UdpTransportTarget(('snmp.live.gambitcommunications.com', 161)),
                   ContextData(),
                   ObjectType(ObjectIdentity('SNMPv2-MIB', oid, 0)))
        )

        if errorIndication:
            print(errorIndication)
        elif errorStatus:
            print('%s at %s' % (errorStatus.prettyPrint(),
                                errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
        else:
            for varBind in varBinds:
                print(' = '.join([x.prettyPrint() for x in varBind]))

    if __name__ == "__main__":
        getFunc('sysName')

当我尝试通过以下命令使用终端启动服务时

$ nameko run helloworld

我收到以下错误消息。


syed@syed-ThinkPad-E480:~/Pysnmp$ nameko run helloworld
Traceback (most recent call last):
  File "/home/syed/.local/bin/nameko", line 11, in <module>
    sys.exit(main())
  File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/main.py", line 112, in main
    args.main(args)
  File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/commands.py", line 110, in main
    main(args)
  File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/run.py", line 181, in main
    import_service(path)
  File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/run.py", line 71, in import_service
    if inspect.getmembers(potential_service, is_entrypoint):
  File "/usr/lib/python3.7/inspect.py", line 354, in getmembers
    if not predicate or predicate(value):
  File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/run.py", line 35, in is_entrypoint
    return hasattr(method, ENTRYPOINT_EXTENSIONS_ATTR)
  File "/home/syed/.local/lib/python3.7/site-packages/pyasn1/type/base.py", line 221, in __getattr__
    raise error.PyAsn1Error('Attempted "%s" operation on ASN.1 schema object' % attr)
pyasn1.error.PyAsn1Error: Attempted "nameko_entrypoints" operation on ASN.1 schema object
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
    import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
    import apt
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
  File "/home/syed/.local/bin/nameko", line 11, in <module>
    sys.exit(main())
  File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/main.py", line 112, in main
    args.main(args)
  File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/commands.py", line 110, in main
    main(args)
  File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/run.py", line 181, in main
    import_service(path)
  File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/run.py", line 71, in import_service
    if inspect.getmembers(potential_service, is_entrypoint):
  File "/usr/lib/python3.7/inspect.py", line 354, in getmembers
    if not predicate or predicate(value):
  File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/run.py", line 35, in is_entrypoint
    return hasattr(method, ENTRYPOINT_EXTENSIONS_ATTR)
  File "/home/syed/.local/lib/python3.7/site-packages/pyasn1/type/base.py", line 221, in __getattr__
    raise error.PyAsn1Error('Attempted "%s" operation on ASN.1 schema object' % attr)
pyasn1.error.PyAsn1Error: Attempted "nameko_entrypoints" operation on ASN.1 schema object

请帮助我了解天气,我尝试的方法是正确的还是错误的。如果是这样如何纠正错误。

任何帮助都将不胜感激。

提前致谢

这与 nameko 连接代码的方式有关...

它似乎尝试查找它可以在您的模块中找到的所有对象的 nameko_entrypoints 属性,最终 运行 到 ASN.1 模式对象(这是神圣的,不应该用于任何其他而非蓝图目的)。

我的建议是将 from pysnmp.hlapi import * 替换为您在代码中使用的特定导入的 pysnmp classes/functions。这应该有望将脆弱的碎片隐藏在 nameko 的视线之外。