跳到正文

Python调用示例

加载SDK

在程序包名下方引入动态库

lib =cdll.LoadLibrary('libsdk.so') //路径可以修改

示例中的libsdk.so该程序在同一路径下。可根据情况自行指定。

SDK函数的入参是go的string类型,在libsdk.h是GoString.所以在python调用的时候转一下。

class StructPointer(Structure):

\_fields\_ = [(\"p\", c_char_p), (\"n\", c_longlong)]

返回类型是 *C.char。python接收时需要指定返回类型

lib.QueryIOC.restype = c_void_p

调用示例

import ctypes
from _ctypes import Structure
from ctypes import cdll, c_char_p, c_longlong,c_void_p,cast
 
 
class StructPointer(Structure):
    _fields_ = [("p", c_char_p), ("n", c_longlong)]
 
    def __init__(self, str):
        self.p = str
        self.n = len(str)
 
lib = cdll.LoadLibrary('libsdk.so')
 
def query():
        lib.QueryIOC.restype =  c_void_p
        ioc_result = lib.QueryIOC(
            StructPointer(
                str.encode("freescanonline.com")))
        result=cast(ioc_result, c_char_p).value
        print(result)
        lib.FreeMemory(ioc_result) //调用内存释放函数。否则在一个不间断的服务中调用动态库会内存泄漏
lib.OnLineUpgrade.restype =  c_void_p
        ioc_result = lib.OnLineUpgrade(StructPointer(str.encode("lid")),StructPointer(str.encode("sn")), StructPointer(str.encode("key")),StructPointer(str.encode(
"salt")), c_longlong(0))
        result=cast(ioc_result, c_char_p).value
        print(result)
 
if __name__ == '__main__':
    query()