0

I need to pass a user's input from PHP to Python.

Here is the typical scenario, so that you can understand the process: 1. Users need to input their username, password and etc. 2. PHP code is connected to Python 3. Python gets all users' input and processes it 4. After computing the result, PHP will get the output from Python

My code is below.

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"))
2
  • stackoverflow.com/questions/12197815/… Commented Mar 19, 2018 at 4:05
  • Hi, thank you for the link i really appreciate that but, can you give more examples, suggestion, answers? Thank you! Commented Mar 21, 2018 at 7:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.