Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
c_call_python
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wanli
c_call_python
Commits
8b380051
Commit
8b380051
authored
Oct 27, 2021
by
wanli
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
更新测试用例
parent
5907af94
Pipeline
#585
canceled with stages
Changes
10
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
26 additions
and
27 deletions
+26
-27
.gitignore
.gitignore
+2
-1
READme.md
READme.md
+4
-1
demo.py
demo.py
+1
-6
main.c
main.c
+3
-2
test_CallModuleClass.c
test_CallModuleClass.c
+6
-6
test_CallModuleClassReturnTuple.c
test_CallModuleClassReturnTuple.c
+0
-1
test_CallModuleFunction.c
test_CallModuleFunction.c
+4
-4
test_CallModuleFunctionByParameters.c
test_CallModuleFunctionByParameters.c
+5
-5
test_CallPythonFunction.c
test_CallPythonFunction.c
+1
-1
排序算法比较结果.xlsx
排序算法比较结果.xlsx
+0
-0
No files found.
.gitignore
View file @
8b380051
__pycache__
__pycache__
*.pro.*
*.pro.*
\ No newline at end of file
*.xlsx
\ No newline at end of file
READme.md
View file @
8b380051
...
@@ -125,4 +125,7 @@ static PyObject* PyInit_emb(void) # 模块初始化函数
...
@@ -125,4 +125,7 @@ static PyObject* PyInit_emb(void) # 模块初始化函数
# 增加模块:
# 增加模块:
PyImport_AppendInittab
(
"emb"
,
&
PyInit_emb
);
# 增加一个模块
PyImport_AppendInittab
(
"emb"
,
&
PyInit_emb
);
# 增加一个模块
```
```
\ No newline at end of file
## 参考链接
+
https://docs.python.org/zh-cn/3.8/c-api/index.html
\ No newline at end of file
demo.py
View file @
8b380051
...
@@ -31,12 +31,7 @@ class dedecms_get_webshell:
...
@@ -31,12 +31,7 @@ class dedecms_get_webshell:
def
check
(
self
,
site
,
port
):
def
check
(
self
,
site
,
port
):
print
(
"Exploiting Host:
%
s, Port:(
%
d)......"
%
(
site
,
port
))
print
(
"Exploiting Host:
%
s, Port:(
%
d)......"
%
(
site
,
port
))
flag
=
1
flag
=
1
if
flag
:
content
=
{
"flag"
:
flag
,
"content"
:
"POST http://www.baidu.com/shell.php (cmd)"
}
content
=
{
"flag"
:
1
,
"content"
:
"POST http://www.baidu.com/shell.php (cmd)"
}
else
:
content
=
{
"flag"
:
0
,
"content"
:
"POST http://www.baidu.com/shell.php (cmd)"
}
return
content
return
content
...
...
main.c
View file @
8b380051
...
@@ -5,13 +5,14 @@ void getCurrentEnv()
...
@@ -5,13 +5,14 @@ void getCurrentEnv()
{
{
PyRun_SimpleString
(
"import sys"
);
PyRun_SimpleString
(
"import sys"
);
PyRun_SimpleString
(
"sys.path.append('./')"
);
PyRun_SimpleString
(
"sys.path.append('./')"
);
return
;
}
}
int
main
(
int
argc
,
const
char
*
argv
[])
int
main
(
int
argc
,
const
char
*
argv
[])
{
{
printf
(
"argc => %d
\r\n
"
,
argc
);
printf
(
"argc => %d
\r\n
"
,
argc
);
printf
(
"argv => %s
\r\n
"
,
*
argv
);
printf
(
"argv => %s
\r\n\r\n
"
,
*
argv
);
printf
(
"%s
\r\n\r\n
"
,
Py_GetVersion
());
test_SimpleString
();
// 执行一行python脚本语句
test_SimpleString
();
// 执行一行python脚本语句
test_CallFunction
();
// repl 一条语句一条语句地执行
test_CallFunction
();
// repl 一条语句一条语句地执行
...
...
test_CallModuleClass.c
View file @
8b380051
...
@@ -7,13 +7,13 @@ void test_CallModuleClass() {
...
@@ -7,13 +7,13 @@ void test_CallModuleClass() {
getCurrentEnv
();
getCurrentEnv
();
PyObject
*
pModule
=
NULL
,
*
pDict
=
NULL
,
*
pClass
=
NULL
,
*
pInstance
=
NULL
,
*
result
=
NULL
;
PyObject
*
pModule
=
NULL
,
*
pDict
=
NULL
,
*
pClass
=
NULL
,
*
pInstance
=
NULL
,
*
result
=
NULL
;
pModule
=
PyImport_ImportModule
(
"demo"
);
// 引入模块
pModule
=
PyImport_ImportModule
(
"demo"
);
// 引入模块
pDict
=
PyModule_GetDict
(
pModule
);
// 获取模块字典属性
pDict
=
PyModule_GetDict
(
pModule
);
// 获取模块字典属性
pClass
=
PyDict_GetItemString
(
pDict
,
"Class_A"
);
// 通过字典属性获取模块中的类
pClass
=
PyDict_GetItemString
(
pDict
,
"Class_A"
);
// 通过字典属性获取模块中的类
pInstance
=
PyObject_CallObject
(
pClass
,
NULL
);
// 实例化获取的类
pInstance
=
PyObject_CallObject
(
pClass
,
NULL
);
// 实例化获取的类
result
=
PyObject_CallMethod
(
pInstance
,
"func"
,
"(s)"
,
"python_
000"
);
// 调用类的方法
result
=
PyObject_CallMethod
(
pInstance
,
"func"
,
"(s)"
,
"python_
xxx"
);
// 调用类的方法
char
*
name
=
NULL
;
char
*
name
=
NULL
;
PyArg_Parse
(
result
,
"s"
,
&
name
);
// 将python类型的返回值转换为c/c++类型
PyArg_Parse
(
result
,
"s"
,
&
name
);
// 将python类型的返回值转换为c/c++类型
printf
(
"%s
\n
"
,
name
);
printf
(
"%s
\n
"
,
name
);
Py_Finalize
();
Py_Finalize
();
...
...
test_CallModuleClassReturnTuple.c
View file @
8b380051
...
@@ -6,7 +6,6 @@ void test_CallModuleClassReturnTuple() {
...
@@ -6,7 +6,6 @@ void test_CallModuleClassReturnTuple() {
Py_Initialize
();
Py_Initialize
();
getCurrentEnv
();
getCurrentEnv
();
PyObject
*
pModule
=
NULL
,
*
pDict
=
NULL
,
*
pClass
=
NULL
,
*
pInstance
=
NULL
,
*
result
=
NULL
;
PyObject
*
pModule
=
NULL
,
*
pDict
=
NULL
,
*
pClass
=
NULL
,
*
pInstance
=
NULL
,
*
result
=
NULL
;
pModule
=
PyImport_ImportModule
(
"demo"
);
// 引入模块
pModule
=
PyImport_ImportModule
(
"demo"
);
// 引入模块
pDict
=
PyModule_GetDict
(
pModule
);
// 获取模块字典属性
pDict
=
PyModule_GetDict
(
pModule
);
// 获取模块字典属性
...
...
test_CallModuleFunction.c
View file @
8b380051
...
@@ -7,10 +7,10 @@ void test_CallModuleFunction() {
...
@@ -7,10 +7,10 @@ void test_CallModuleFunction() {
getCurrentEnv
();
getCurrentEnv
();
PyObject
*
pModule
=
NULL
,
*
pFunc
=
NULL
,
*
pArg
=
NULL
;
PyObject
*
pModule
=
NULL
,
*
pFunc
=
NULL
,
*
pArg
=
NULL
;
pModule
=
PyImport_ImportModule
(
"demo"
);
// 引入模块
pModule
=
PyImport_ImportModule
(
"demo"
);
// 引入模块
pFunc
=
PyObject_GetAttrString
(
pModule
,
"print_arg"
);
// 直接获取模块中的函数
pFunc
=
PyObject_GetAttrString
(
pModule
,
"print_arg"
);
// 直接获取模块中的函数
pArg
=
Py_BuildValue
(
"(s)"
,
"hello_python"
);
// 參数类型转换,传递一个字符串。将c/c++类型的字符串转换为python类型。元组中的python类型查看python文档
pArg
=
Py_BuildValue
(
"(s)"
,
"hello_python"
);
// 參数类型转换,传递一个字符串。将c/c++类型的字符串转换为python类型。元组中的python类型查看python文档
PyEval_CallObject
(
pFunc
,
pArg
);
// 调用直接获得的函数。并传递參数
PyEval_CallObject
(
pFunc
,
pArg
);
// 调用直接获得的函数。并传递參数
Py_Finalize
();
Py_Finalize
();
...
...
test_CallModuleFunctionByParameters.c
View file @
8b380051
...
@@ -8,13 +8,13 @@ void test_CallModuleFunctionByParameters() {
...
@@ -8,13 +8,13 @@ void test_CallModuleFunctionByParameters() {
getCurrentEnv
();
getCurrentEnv
();
PyObject
*
pModule
=
NULL
,
*
pDict
=
NULL
,
*
pFunc
=
NULL
,
*
pArg
=
NULL
,
*
result
=
NULL
;
PyObject
*
pModule
=
NULL
,
*
pDict
=
NULL
,
*
pFunc
=
NULL
,
*
pArg
=
NULL
,
*
result
=
NULL
;
pModule
=
PyImport_ImportModule
(
"demo"
);
// 引入模块
pModule
=
PyImport_ImportModule
(
"demo"
);
// 引入模块
pDict
=
PyModule_GetDict
(
pModule
);
// 获取模块字典属性 //相当于Python模块对象的__dict__ 属性,得到模块名称空间下的字典对象
pDict
=
PyModule_GetDict
(
pModule
);
// 获取模块字典属性 //相当于Python模块对象的__dict__ 属性,得到模块名称空间下的字典对象
pFunc
=
PyDict_GetItemString
(
pDict
,
"add"
);
// 从字典属性中获取函数
pFunc
=
PyDict_GetItemString
(
pDict
,
"add"
);
// 从字典属性中获取函数
pArg
=
Py_BuildValue
(
"(i, i)"
,
1
,
2
);
// 參数类型转换,传递两个整型參数
pArg
=
Py_BuildValue
(
"(i, i)"
,
1
,
2
);
// 參数类型转换,传递两个整型參数
result
=
PyEval_CallObject
(
pFunc
,
pArg
);
// 调用函数。并得到python类型的返回值
result
=
PyEval_CallObject
(
pFunc
,
pArg
);
// 调用函数。并得到python类型的返回值
int
sum
;
int
sum
;
PyArg_Parse
(
result
,
"i"
,
&
sum
);
// 将python类型的返回值转换为c/c++类型
PyArg_Parse
(
result
,
"i"
,
&
sum
);
// 将python类型的返回值转换为c/c++类型
printf
(
"sum = %d
\n
"
,
sum
);
printf
(
"sum = %d
\n
"
,
sum
);
Py_Finalize
();
Py_Finalize
();
...
...
test_CallPythonFunction.c
View file @
8b380051
...
@@ -8,7 +8,7 @@ void test_CallPythonFunction(void) {
...
@@ -8,7 +8,7 @@ void test_CallPythonFunction(void) {
// Py_SetPythonHome(L"D:/Anaconda3");
// Py_SetPythonHome(L"D:/Anaconda3");
Py_Initialize
();
Py_Initialize
();
getCurrentEnv
();
getCurrentEnv
();
// 设置当前python工作路径,也就是告诉python去哪里加载python文件
// Create some Python objects that will later be assigned values.
// Create some Python objects that will later be assigned values.
PyObject
*
pModule
,
*
pDict
,
*
pFunc_add
,
*
pFunc_hello
,
*
pArgs
,
*
pValue
,
*
pValue2
;
PyObject
*
pModule
,
*
pDict
,
*
pFunc_add
,
*
pFunc_hello
,
*
pArgs
,
*
pValue
,
*
pValue2
;
...
...
排序算法比较结果.xlsx
deleted
100644 → 0
View file @
5907af94
File deleted
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment