将参数从php传递给python

时间:2018-03-19 04:01:32

标签: php python

你们可以帮我解决我的代码吗?

我需要将用户的输入从php传递给python。

以下是方案,以便您了解该过程  1.用户需要输入用户名,密码等  2. php代码连接到python  3. python获取所有用户的输入并生成它  4.计算结果后,php将从python中获取输出

CODES ...

的index.php

    <!DOCTYPE html>
<html>
    <head>
        <title>Asurion</title>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>

    <body>
        <div class="wrap" style="margin: auto; padding-top: 10%; padding-left: 40%; font-size: 20px;">
            <img src="pic/asurion.png" style="width: 70px; height: 70px; display: block; margin-left: 12%; margin-bottom: 5px;">
            <form name="forms" method="post">
                <input type="text" required name="username" placeholder="Username" style="font-size: 20px;"/><br>
                <input type="password" required name="password" placeholder="Password" style="font-size: 20px; margin-top: 5px;"/><br>
                Site: <select name="site" style="font-size: 20px; margin-top: 5px">
                <option>NED</option>
                <option>SED</option>
                </select><br>
                <input type="text" required name="ClusterName" placeholder="Cluster Name" style="font-size: 20px; margin-top: 5px"/><br><br>
                <input type="submit" value="Submit" name="submit" style="font-size: 20px;"/><BR><BR>
            </form>
        </div>

<center>
<?php
if (isset($_POST['submit'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $uri = $_POST['site'];
    $search_cg = $_POST['ClusterName'];

    echo "<br><br><br>PYTHON SCRIPT:<br>";
    $command = 'python orig.py ' . $username . ' ' . $password . ' ' . $uri . ' ' . $search_cg;
    $output = passthru($command);
    echo $command;
}
?>
</center>

    </body>
</html>

orig.py

    #Modules
import requests as r
import sys
import getpass as getpass

from requests.packages.urllib3.exceptions import InsecureRequestWarning

#Variables with Integers
allocated = 0.
written = 0
cl_num = 0

#site variables
NED = 'https://10.3.34.190:443'
SED = 'https://10.4.34.160:443'

#Empty lists
volumes = []
hosts = []

# Parameters
user = input('Username: ')
password = getpass.getpass(prompt='Password: ', stream=None)
uri = eval(input('NED or SED: '))
headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'Accept-Charset': 'utf-8',
    'Accept-Encoding': 'gzip',
    'Connection': 'Keep-Alive',
}

r.packages.urllib3.disable_warnings(InsecureRequestWarning)
#Gather host and volume information from the cluster
search_cg = input("Cluster: ")
C_ID = r.get(
    url=uri + '/api/rest/clusters?sort=name&name=eq:' + search_cg + "&page_size=50&page=1",
    auth=(user, password), headers=headers, verify=False)
lun = C_ID.json()

for h in lun['result']:
    host = h['hosts']
    for h in host:
        name = h['name']
        hosts.append(name)
for x in lun['result']:
    id = x['luns']
    for v in id:
        vol = v['volume_id']
        volumes.append(vol)
#Exit if the cluster doesn't contain any volumes
if not volumes:
    sys.exit("Cluster doesn't contain any volumes")

for v in volumes:
    C_Size = r.get(
        url=uri + '/api/rest/volumes/' + str(v),
        auth=(user, password), headers=headers, verify=False)
    # JSON Math
    done = C_Size.json()
    allocated += done['result']['size']
    written += done['result']['allocated']


print("Hosts in cluster: " + (", ".join(hosts)))
print("# of volumes: " + str(len(volumes)))
print("Presented: " + str(round(allocated / 1024 / 1024 / 1024 / 1024, 2)) + str(" TiB"), end="")
print(" Used: " + str(round(written / 1024 / 1024 / 1024 / 1024, 2)) + str(" TiB"))

谢谢你提前!!!

0 个答案:

没有答案