1. 写C++封装snmp++库的代码
#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. 结果
