您的位置:首页 > 编程语言 > Python开发

Python中Ctype调用snmp++库

2016-10-07 15:40 155 查看

1. 写C++封装snmp++库的代码

// ctypett.cpp
#include <libsnmp.h>
#include "snmp_pp/snmp_pp.h"

using namespace Snmp_pp;

using namespace std;

class TestSnmp
{
public:
TestSnmp();
~TestSnmp();

void PrintHelloWorld();
};

TestSnmp::TestSnmp()
{
Oid oid("1.2.3.4");
cout << oid.get_printable()<<endl;
}

TestSnmp::~TestSnmp()
{

}

void TestSnmp::PrintHelloWorld()
{
cout <<"hello world!!!" <<endl;
}

extern "C"
{
TestSnmp* TestSnmp_New()
{
return new TestSnmp();
}

void PrintHelloWorld(TestSnmp* obj)
{
obj->PrintHelloWorld();
}
}


2. 使用g++的编译成共享库

g++ -fPIC -shared -o ctypett.so ctypett.cpp -L/usr/local/src/snmp/lib -lsnmp++


3. 写Python 部分代码,测试库

from ctypes import cdll

lib = cdll.LoadLibrary('./ctypett.so')

class Foo(object):
def __init__(self):
self.obj = lib.TestSnmp_New()
def PrintHelloWorld(self):
lib.PrintHelloWorld(self.obj)

tt = Foo()

tt.PrintHelloWorld()


4. 结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ctypes python C++ snmp++