繁体   English   中英

如何将html / php页面的值传递给Python脚本

[英]how can I pass the value of html/php page to Python script

我有一个非常简单的问题...我在php页面(index.php)中有两个输入文本字段和一个输出文本字段,在Python(add.py)中有一个乘法函数代码。 现在,我想从php页面获取输入,并在python函数内计算结果并将结果打印在php输出文本字段中。 我不知道该怎么做。

的index.php

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
    Input 1: <input type="text" name="number" value="<?php echo $input1;?>">
    <span class="error">* <?php echo $inputErr;?></span>
    <br>
    <br>
    Input 2: <input type="text" name="number" value="<?php echo $email;?>">
    <span class="error">* <?php echo $inputErr;?></span>
    <br>
    <br>
    Output:
    <textarea name="output" rows="5" cols="40">
        <?php echo $output;?>
    </textarea>
    <br>
    <br>

    <input type="submit" name="submit" value="Submit">  
</form>

multiply.py

number1 = int(input("Enter first number: "))
number2 = int(input("Enter second number: "))
mul = number1 * number2
print("Multiplication of given two numbers is: ", mul)

首先,您必须更改您的python脚本,以便它可以从命令行参数获取数字,而不是从stdin读取。 您可能还想更改输出,以便仅输出结果而没有其他不相关的文本:

# add.py
# NB : this should really be named 'multiply', not 'add'

import sys
try:
    number1 = int(sys.argv[1])
    number2 = int(sys.argv[2])
except (IndexError, ValueError):
    sys.exit("usage: python add.py <number1> <number2>")

result = number1 * number2
print(result)

然后在php端,只需使用带有正确命令字符串的exec调用脚本 (您必须在命令字符串中插入数字)并从$ouput数组读取结果即可。

话虽这么说,但人们只能想知道为什么要调用Python脚本来完成诸如乘法之类的简单操作...

暂无
暂无

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

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