C调用python
来源:互联网 发布:淘宝优惠券兑换链接 编辑:程序博客网 时间:2024/06/05 07:12
针对c调用python,做个了小demo
python模块:demo.py
def print_arg(str):
print str
def add(a,b):
print
'a='
, a
print
'b='
, b
return
a + b
class
Class_A:
def __init__(self):
print
"init"
def fun(self, str):
print
'hello'
, str
return
str
class
dedecms_get_webshell:
def __init__(self):
''
'
''
'
self._run = True
#must rewrite function
def check(self,site,port):
''
'
''
'
print
"Exploiting Host:%s,Port:(%d)......"
% (site,port)
flag =
1
if
flag:
content={
"flag"
:
1
,
"content"
:
"POSThttps://www.baidu.com/shell.php (cmd)"
}
else
:
content={
"flag"
:
0
,
"content"
:
"POSThttps://www.baidu.com/shell.php (cmd)"
}
return
content
if
__name__==
"__main__"
:
site=
"www.baidu.com"
port=
80
obj = dedecms_get_webshell()
ret=obj.check(site,port)
print ret
分析:
1. print_arg定义了一个传参的函数
2. add 定义了一个传如多个参数,且有返回值的函数
3. Class_A定义了一个类及类的一个方法fun(传参数,有返回值)
4. dedecms_get_webshell定一个了类及类的一个方法check(传多个参数,返回值是个元组)
下面使用c语言调用demo.py文件中的函数。
测试函数:
#include <Python.h>
int
main(
int
argc,
char
* argv[])
{
test();
test1();
test2();
test3();
test4();
return
0
;
}
逐个分析:
//导出当前环境变量
void
getcurrent()
{
PyRun_SimpleString(
"import sys"
);
PyRun_SimpleString(
"sys.path.append('./')"
);
return
;
}
1. 一个最基本的调用方式
void
test()
{
Py_Initialize();
//初始化python
PyRun_SimpleString(
"print 'hello python'"
);
//直接运行python代码
Py_Finalize();
//释放python
return
;
}
分析:直接运行python 代码,在调用的时候必须先做初始化操作(Py_Initialize),调用完后做清理工作(Py_Finalize)
2. 调用模块中的一个普通函数
void
test1()
{
Py_Initialize();
//初始化python
getcurrent();
PyObject *pModule = NULL, *pFunc = NULL, *pArg = NULL;
pModule = PyImport_ImportModule(
"demo"
);
//引入模块
pFunc = PyObject_GetAttrString(pModule,
"print_arg"
);
//直接获取模块中的函数
pArg = Py_BuildValue(
"(s)"
,
"hello_python"
);
//参数类型转换,传递一个字符串。将c/c++类型的字符串转换为python类型,元组中的python类型查看python文档
PyEval_CallObject(pFunc, pArg);
//调用直接获得的函数,并传递参数
Py_Finalize();
//释放python
return
;
}
分析:先引用模块(PyImport_ImportModule),然后获取模块中的函数(PyObject_GetAttrString),对c传入python 的参数做类型转换(Py_BuildValue("(s)","hello_python")),最后直接调用函数并传递参数(PyEval_CallObject)。
3. 调用模块中的一个函数(多参数,带返回值)
void
test2()
{
Py_Initialize();
getcurrent();
PyObject *pModule = NULL, *pDict = NULL, *pFunc = NULL, *pArg = NULL, *result = NULL;
pModule = PyImport_ImportModule(
"demo"
);
//引入模块
pDict = PyModule_GetDict(pModule);
//获取模块字典属性 //相当于Python模块对象的__dict__ 属性,得到模块名称空间下的字典对象
pFunc = PyDict_GetItemString(pDict,
"add"
);
//从字典属性中获取函数
pArg = Py_BuildValue(
"(i, i)"
,
1
,
2
);
//参数类型转换,传递两个整型参数
result = PyEval_CallObject(pFunc, pArg);
//调用函数,并得到python类型的返回值
int
sum;
PyArg_Parse(result,
"i"
, &sum);
//将python类型的返回值转换为c/c++类型
printf(
"sum=%d\n"
, sum);
Py_Finalize();
}
4. 调用模块中简单的一个类(单个返回值)
void
test3()
{
Py_Initialize();
getcurrent();
PyObject *pModule = NULL, *pDict = NULL, *pClass = NULL, *pInstance = NULL, *result = NULL;
pModule = PyImport_ImportModule(
"demo"
);
//引入模块
pDict = PyModule_GetDict(pModule);
//获取模块字典属性
pClass = PyDict_GetItemString(pDict,
"Class_A"
);
//通过字典属性获取模块中的类
pInstance = PyInstance_New(pClass, NULL, NULL);
//实例化获取的类
result = PyObject_CallMethod(pInstance,
"fun"
,
"(s)"
,
"python_000"
);
//调用类的方法
char
* name=NULL;
PyArg_Parse(result,
"s"
, &name);
//将python类型的返回值转换为c/c++类型
printf(
"%s\n"
, name);
Py_Finalize();
}
5. 调用模块中一个简单的类(返回值是个元组)
void test4()
{
Py_Initialize();
getcurrent();
PyObject *pModule = NULL, *pDict = NULL,*pClass = NULL, *pInstance = NULL, *result = NULL;
pModule =PyImport_ImportModule("demo"); //引入模块
pDict = PyModule_GetDict(pModule); //获取模块字典属性
pClass = PyDict_GetItemString(pDict,"dedecms_get_webshell"); //通过字典属性获取模块中的类
pInstance = PyInstance_New(pClass, NULL,NULL);
result = PyObject_CallMethod(pInstance,"check", "(s,i)", "www.baidu.com", 80);
int flag;
char*content = NULL;
PyObject *obj_content =PyDict_GetItemString(result, "content");
content = PyString_AsString(obj_content);
PyObject *obj_flag =PyDict_GetItemString(result, "flag");
flag = PyInt_AsLong(obj_flag);
printf("content: %s, flag: %d\n",content, flag);
Py_Finalize();
}
Makefile书写:
all: test
test: pytest.o
gcc -L/usr/lib/python2.
7
/ -lpython2.
7
-ldl pytest.o -o test
pytest.o: pytest.c
gcc -g -std=gnu99 -Wall -c pytest.c -I/usr/include/python2.
7
/
clean:
@rm
-rf *.o *.pyc test
- BCB(C++)调用Python
- C中调用PYTHON
- C调用python
- C调用python
- Python 调用 C函数
- python调用c
- Python 调用C
- c中调用python
- Python调用c/c++
- c 调用python
- python 调用 c 函数
- python调用C函数库
- Python调用C
- Python调用C/C++
- Python调用C程序
- python调用c/c++
- python调用c
- c调用python
- Linux 基础
- Android 体系架构
- Robot Framework自动化测试(五)--- 开发系统关键字
- OSGI学习札记
- 每日AC--leetCode--PalindromePartitioning--回溯法 递归实现
- C调用python
- ServletContext与Web应用以及Spring容器启动
- 2018校招web前端开发方向总结
- 题目456-邮票分你一半
- [UE4]C++实现动态加载的问题:LoadClass<T>()和LoadObject<T>()
- Oracle数据类型之number(转)
- 如何判断是否是微信端登录
- 基本操作详解
- python3.6安装记录