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

时间:2023-03-09 05:47:55
c++调用python系列(1): 结构体作为入参及返回结构体

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

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

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

开始:

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

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

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

 // python_c++.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <Python.h>
#pragma comment(lib, "Ws2_32.lib") int _tmain(int argc, _TCHAR* argv[])
{
// 初始化Python
Py_Initialize();
// 检查初始化是否成功
if (!Py_IsInitialized()) {
return -;
}
// 添加当前路径
//把输入的字符串作为Python代码直接运行,返回0
//表示成功,-1表示有错。大多时候错误都是因为字符串中有语法错误。
PyRun_SimpleString("import sys");
int result = PyRun_SimpleString("print('----------import sys-------')");
if (result!=-){
printf("test pyhon OK!\n\n");
} PyRun_SimpleString("sys.path.append('./')"); // 载入名为pytest的脚本
PyObject *pName = PyBytes_FromString("pytest");
PyObject *pModule = PyImport_Import(pName);
if (!pModule) {
printf("can't find pytest.py");
getchar();
return -;
} PyObject *pDict = PyModule_GetDict(pModule);
if (!pDict) {
getchar();
return -;
} //下面这段是查找函数test 并执行test
PyObject *pFunc = PyDict_GetItemString(pDict, "test2");
if (!pFunc || !PyCallable_Check(pFunc)) {
printf("can't find function [test2]");
getchar();
return -;
} typedef struct header_ {
int buf1;
int buf2;
char buf3[];
int buf4;
}header; //创建结构体
header input;
memset(&input,,sizeof(input));
input.buf1 = ;
input.buf2 = ;
input.buf4 = ;
strcpy_s(input.buf3, "kjac"); //打包成byte*
char * byInput = new char(sizeof(input));
memcpy(byInput, &input, sizeof(input)); //申请python入参
PyObject *pArgs = PyTuple_New();
//对python入参进行赋值; s代表char*格式, #代表传入指定长度
PyTuple_SetItem(pArgs, , Py_BuildValue("s#", byInput, sizeof(input))); //执行函数
PyObject *pResult = PyObject_CallObject(pFunc, pArgs); char* pRsp;
//获取返回值
PyArg_Parse(pResult, "s", &pRsp); //转成结构体
header* pstRsp = (header*)pRsp;
printf("\n-----------c++层接收py返回:buf1:%d,buf2:%d,buf3:%s,buf4:%d\n",
pstRsp->buf1, pstRsp->buf2, pstRsp->buf3, pstRsp->buf4); //释放
Py_DECREF(pName);
Py_DECREF(pArgs);
Py_DECREF(pModule); // 关闭Python
Py_Finalize();
getchar();
return ;
}

下面的是python代码

 import struct

 def test(a):
print("----------------------------python 1-----------------------")
ret = struct.unpack('ii11si', a)
print("----------------------------python deal-----------------------")
print("--------------------python receive c++ struct:")
print("begin unpack:")
print("")
print(ret)
buf1 = ret[0] + 1
buf2 = ret[1] + 1
buf4 = ret[3] + 1
print("--------------------begin pack data and begin send to c++")
print("")
bin_buf_all = struct.pack('ii11si', buf1, buf2, "dfds", buf4)
print("----------------------------python end-----------------------")
return bin_buf_all

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

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

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

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

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

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

----------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系列(1): 结构体作为入参及返回结构体

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