python 结构体赋值_c++调用python系列(1): 结构体作为入参及返回结构体

最近在打算用python作测试用例以便对游戏服务器进行功能测试以及压力测试;

因为服务器是用c++写的,采用的TCP协议,当前的架构是打算用python构造结构体,传送给c++层进行socket发送给游戏服务器,响应消息再交由python进行校验;

开始:

首先是c++调用python这一层需要打通;

幸运的是python自己有一套库提供c/c++进行调用;

下面我贴代码;用的vs2013,python用的2.7

1 //python_c++.cpp : 定义控制台应用程序的入口点。2 //3

4 #include "stdafx.h"

5 #include

6 #pragma comment(lib, "Ws2_32.lib")

7

8 int _tmain(int argc, _TCHAR*argv[])9 {10 //初始化Python

11 Py_Initialize();12 //检查初始化是否成功

13 if (!Py_IsInitialized()) {14 return -1;15 }16 //添加当前路径17 //把输入的字符串作为Python代码直接运行,返回018 //表示成功,-1表示有错。大多时候错误都是因为字符串中有语法错误。

19 PyRun_SimpleString("import sys");20 int result = PyRun_SimpleString("print('----------import sys-------')");21 if (result!=-1){22 printf("test pyhon OK!\n\n");23 }24

25 PyRun_SimpleString("sys.path.append('./')");26

27 //载入名为pytest的脚本

28 PyObject *pName = PyBytes_FromString("pytest");29 PyObject *pModule =PyImport_Import(pName);30 if (!pModule) {31 printf("can't find pytest.py");32 getchar();33 return -1;34 }35

36 PyObject *pDict =PyModule_GetDict(pModule);37 if (!pDict) {38 getchar();39 return -1;40 }41

42 //下面这段是查找函数test 并执行test

43 PyObject *pFunc = PyDict_GetItemString(pDict, "test2");44 if (!pFunc || !PyCallable_Check(pFunc)) {45 printf("can't find function [test2]");46 getchar();47 return -1;48 }49

50 typedef structheader_ {51 intbuf1;52 intbuf2;53 char buf3[11];54 intbuf4;55 }header;56

57 //创建结构体

58 header input;59 memset(&input,0,sizeof(input));60 input.buf1 = 1;61 input.buf2 = 2;62 input.buf4 = 3;63 strcpy_s(input.buf3, "kjac");64

65 //打包成byte*

66 char * byInput = new char(sizeof(input));67 memcpy(byInput, &input, sizeof(input));68

69 //申请python入参

70 PyObject *pArgs = PyTuple_New(1);71 //对python入参进行赋值; s代表char*格式, #代表传入指定长度

72 PyTuple_SetItem(pArgs, 0, Py_BuildValue("s#", byInput, sizeof(input)));73

74 //执行函数

75 PyObject *pResult =PyObject_CallObject(pFunc, pArgs);76

77 char*pRsp;78 //获取返回值

79 PyArg_Parse(pResult, "s", &pRsp);80

81 //转成结构体

82 header* pstRsp = (header*)pRsp;83 printf("\n-----------c++层接收py返回:buf1:%d,buf2:%d,buf3:%s,buf4:%d\n",84 pstRsp->buf1, pstRsp->buf2, pstRsp->buf3, pstRsp->buf4);85

86 //释放

87 Py_DECREF(pName);88 Py_DECREF(pArgs);89 Py_DECREF(pModule);90

91 //关闭Python

92 Py_Finalize();93 getchar();94 return 0;95 }

下面的是python代码

1 importstruct2

3 deftest(a):4 print("----------------------------python 1-----------------------")5 ret = struct.unpack('ii11si', a)6 print("----------------------------python deal-----------------------")7 print("--------------------python receive c++ struct:")8 print("begin unpack:")9 print("")10 print(ret)11 buf1 = ret[0] + 1

12 buf2 = ret[1] + 1

13 buf4 = ret[3] + 1

14 print("--------------------begin pack data and begin send to c++")15 print("")16 bin_buf_all = struct.pack('ii11si', buf1, buf2, "dfds", buf4)17 print("----------------------------python end-----------------------")18 return bin_buf_all

下面附上vs2013的工程属性修改

设置python工程的include路径和libs路径

接下来;就可以编译输出了;

----------import sys-------

test pyhon OK!

----------------------------python 1-----------------------

----------------------------python deal-----------------------

--------------------python receive c++ struct:

begin unpack:

(1, 2, 'kjac\x00\xfe\xfe\xfe\xfe\xfe\xfe', 3)

--------------------begin pack data and begin send to c++

----------------------------python end-----------------------

-----------c++层接收py返回:buf1:2,buf2:3,buf3:dfds,buf4:4

现在c++调用python已经大功告成了;下面就是python和c++之间的框架消息及测试代码的构造

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python调用C++函数并返回结构体,可以通过使用ctypes库来实现。下面是一个示例代码,展示了如何在Python调用一个返回结构体C++函数: 假设我们有一个C++函数,它返回一个结构体类型`MyStruct`: ```c++ #include <iostream> struct MyStruct { int my_int; float my_float; char my_string[256]; }; MyStruct get_struct() { MyStruct s; s.my_int = 123; s.my_float = 3.14; strcpy(s.my_string, "Hello, C++!"); return s; } ``` 现在,我们可以通过使用ctypes库来在Python调用这个函数并获取返回结构体。下面是示例代码: ```python import ctypes # 加载C++编译后的动态链接库 lib = ctypes.cdll.LoadLibrary('./libexample.so') # 定义结构体类型 class MyStruct(ctypes.Structure): _fields_ = [ ("my_int", ctypes.c_int), ("my_float", ctypes.c_float), ("my_string", ctypes.c_char * 256) ] # 设置函数的返回类型为MyStruct类型 lib.get_struct.restype = MyStruct # 调用C++函数并获取返回结构体 result = lib.get_struct() # 输出结构体的成员变量 print(result.my_int) print(result.my_float) print(result.my_string.decode()) ``` 在上面的示例代码中,我们首先使用`cdll.LoadLibrary()`函数加载C++编译后的动态链接库。接着,我们定义了一个结构体类型`MyStruct`,并使用`_fields_`属性来定义结构体的成员变量列表。然后,我们使用`restype`属性将C++函数的返回类型设置为`MyStruct`类型。 最后,我们调用C++函数`get_struct()`并获取返回结构体,将其赋值给变量`result`。我们可以通过访问结构体对象的成员变量来获取它们的值,使用`.decode()`方法将`my_string`成员变量从`bytes`类型转换为字符串类型并输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值