转 php 框架 Php 依赖框架 后台 调用python 脚本

感谢 ruchee,Tim HallmanSMshrimant

https://www.v2ex.com/t/351428

https://stackoverflow.com/questions/19735250/running-a-python-script-from-php

转 php 框架 Php 依赖框架 后台 调用python 脚本

##html 的按钮和其他选项框  起到 中介功能,调用后端。

以下案例仅仅对windows php 调用python 脚本有效

D:wamp64wwwapp  新建2个文件index.php 和 test.py

index.php

//php.php

<html>
<body>
<head>
<title>
run
</title>
</head>

<form method="post">

<input type="submit" value="GO" name="GO">
</form>
</body>
</html>

<?php
if(isset($_POST['GO']))
{
exec("D:/python3/python.exe D:/wamp64/www/peng/test.py 2>&1");
echo"success";
}
?>

test.py

#!D:python3python.exe
# -*- coding: utf-8 -*- 
import os    
os.makedirs("thisfolder");

  

http://localhost/app/index.php

完成后,就会创建一个目录

thisfolder


### 感谢

https://blog.csdn.net/liao392781/article/details/80321614?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase



a.py

import sys

#传入3个参数,具体操作根据个人情况
def main(argv):
print(argv[1])
print(argv[2])
print(argv[3])

#if __name__ == "__main__":
# main(sys.argv)
a=sys.argv[1]
b=sys.argv[2]
c=sys.argv[3]

f = open('d:/newfile.txt', 'w')
f.write(a)
f.write(b)
f.write(c)
f.close()

执行过程

python a.py 1 2  3

####2

感谢jieliu8080

import sys

#传入3个参数,具体操作根据个人情况
def main(argv):
print(argv[1])
print(argv[1][4])
print(arr)
# print(argv[2])
# print(argv[3])

a=sys.argv[1]
arr=a.split(',')


if __name__ == "__main__":
main(sys.argv)


f = open('d:/newfile.txt', 'w')
f.write(a)
f.close()

字符串转数组
str = '1,2,3'
arr = str.split(',')

数组转字符串
arr = ['a','b']
str = ','.join(arr)

arr = [1,2,3]
str = ','.join(str(i) for i in b)

原文地址:https://www.cnblogs.com/feiyun8616/p/13213055.html