dongyu8694 2017-07-22 14:41
浏览 523
已采纳

从PHP调用Python脚本的函数

I have Python script with a fucntion. I want to call this function from PHP while passing two parameters to the function.
What am i doing wrong here? :/

myscript.py

def InitPython(ID,Name):
      'DO STUFF'

PHP

 $varone='bleh';
    $vartwo='blah';
    $output = shell_exec("myscript.py '$varone' 'vartwo' ");
  • 写回答

1条回答 默认 最新

  • dst2017 2017-07-22 16:10
    关注

    PHP:

    It could be possible that your myscript.py is not found, use a Full Path:

     $output = shell_exec("<your Full Path>myscript.py '$varone' 'vartwo' ");
    

    myscript.py

    You must Read the passed Parameters in your Python Script, for instance:

    def InitPython(ID,Name):
      'DO STUFF'
    
    if __name__ == '__main__':
       import sys
       InitPython(sys.argv[1], sys.argv[2])
    

    Relevant: Python Documentation: module-argparse

    The argparse module makes it easy to write user-friendly command-line interfaces. The program defines what arguments it requires, and argparse will figure out how to parse those out of sys.argv

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥100 webapi的部署(标签-服务器)
    • ¥20 怎么加快手机软件内部计时的时间(关键词-日期时间)
    • ¥15 C语言除0问题的检测方法
    • ¥15 为什么四分管的内径有的是16mm有的15mm,四分不应该是12.7mm吗
    • ¥15 macos13下 ios交叉编译的问题
    • ¥15 bgz压缩文件怎么打开
    • ¥15 封装dll(引入了pcl的点云设计库)
    • ¥30 关于#开发语言#的问题:我需要在抄板的基础上再抄板抄程序,根据RDA8851CM基础上开发
    • ¥15 oracle 多个括号,怎么删除指定的括号及里面的内容?
    • ¥15 小新14API2019想用bios调风扇