简体   繁体   English

使用php中的参数执行python脚本

[英]Execute python script with parameters in php

I have a php script which searches a mySQL database and returns all of its instances. 我有一个php脚本,它搜索mySQL数据库并返回其所有实例。 I would like to use this instance data (string, string, string, list of lists of (string, string, string, string)) as parameter for a python script. 我想将此实例数据(字符串,字符串,字符串,(字符串,字符串,字符串,字符串)的列表列表)用作python脚本的参数。

so how do i pass all this info to my python script? 那么如何将所有这些信息传递给我的python脚本? I know i use exec to execute the script, but how i do include the list of lists as a parameter? 我知道我使用exec执行脚本,但是我如何将列表列表作为参数呢?

you cant well not exactly 你不能完全不正确

you can use redirection to send text to the script 您可以使用重定向将文本发送到脚本

exec("something.py < inputstuff.txt");

or you can pass an argument list 或者你可以传递一个参数列表

exec("something.py arg1 arg2 arg3");

you could pass it in as a string representation and eval it inside the script or something if you use redirection the data should be available from sys.stdin if you pass it as an arg list the data should be available via sys.argv 您可以将其作为字符串表示形式传递并在脚本内进行评估,或者如果使用重定向,则应从sys.stdin中获取数据;如果将其作为arg列表进行传递,则数据应可通过sys.argv获得

<?php
    $list = array('arg1', 'arg2');
    exec(sprintf('python script.py %s', escapeshellarg(implode(' ', $list)));

Then just use argparse: http://docs.python.org/library/argparse.html#module-argparse . 然后只需使用argparse: http : //docs.python.org/library/argparse.html#module-argparse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM