dqdtgiw4736 2012-02-20 15:51
浏览 456
已采纳

为什么我的PHP exec()在尝试调用Python函数时不起作用?

I am writing a web app that will use HTML to display a form. Then the user completes the form, and on submit the form is sent to PHP. In the PHP file I have it set up to call and exec() command to a Python file that will return data. But for some reason I am not getting anything back. When I execute the command in terminal it works great, but no response we ran in the web browser.

HTML:

<form id ="nameForm" action="python.php" method="post" target="_blank">
     <label>First Name:</label><input type="text" id="firstName" name="firstName"><br>
     <label>Last Name:</label><input type="text" id="lastName" name="lastName"><br>
     <input type="submit" id="submitName" value="Submit">
</form>

PHP:

$firstName = $_REQUEST['firstName'];
$lastName = $_REQUEST['lastName'];

exec("/opt/http/PythonWeb/web/python pythonName.py " . $firstName . " " . $lastName, $response);

echo $response;

Python:

import sys

def main():
    firstName = sys.argv[1]
    lastName = sys.argv[2]

    output = ""

    output += "<html>
"
    output += "  <head><title></title></head>
"
    output += "  <body>
"
    output += "      <p>Hi " + firstName + " " + lastName + "</p>
"
    output += "  </body>
"
    output += "</html>
"

    print output

if __name__ == '__main__': main()

Please give me some suggestions as to what may be wrong. I am new to Python, but it's what the boss man wants... so I am going to make this work. Thanks!

  • 写回答

3条回答 默认 最新

  • duanhan8757 2012-02-20 15:56
    关注

    According to this http://www.php.net/manual/en/function.passthru.php, you're using passthrough in a wrong way. It is for binary data...

    This function should be used in place of exec() or system() when the output from the Unix command is binary data which needs to be passed directly back to the browser.

    Try Exec().

    Hope I was helpful.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站