'Printing data from html form to python and printing it back to html through php
So I'm getting data from a html form and sending it to a python script. Then i want to be able to print this out in my php.
this is my html file
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="utf-8" />
<title>Gymnasie arbete</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" />
</head>
<body>
<div id="background">
<div id="top">
<div id="tab1">
<button class="button" onclick="location.href='Main.html'">Player search</button>
</div>
<div id="tab2">
<button class="button" onclick="location.href='Champ.html'">Champions</button>
</div>
<div id="tab3">
<button class="button" onclick="location.href='Leaderboard.html'">Leaderboard</button>
</div>
</div>
<div id="bot"></div>
<div id="enter">
<form method="POST" action="Main.php">
<input type="text" id="Username" name="Username" size="60" autofocus placeholder="Username">
<select id="region" name="region">
<option>EUW</option>
<option>EUNE</option>
<option>NA</option>
<option>KR</option>
<option>BR</option>
<option>JP</option>
<option>RU</option>
<option>LAS</option>
<option>LAN</option>
<option>OCE</option>
<option>TR</option>
</select>
<input type="submit" name="hello" value="Sök">
</form>
</div>
</div>
<script src="Main.py"></script>
</body>
</html>
Python file:
from riotwatcher import LolWatcher, ApiError
import pandas as pd
from bs4 import BeautifulSoup
import urllib.request
from flask import Flask, request, render_template
import cgi, cgitb
import sys
#user1 = 0;
#region1 = 0;
#app = Flask(__name__)
#@app.route('/')
#def Main():
# return render_template('Main.html')
#@app.route('/', methods =['GET', 'POST'])
#def summoner():
# if request.method == "POST":
# user1 = request.form.get('Username')
# region1 = request.form.get('region')
#user = user1;
#region = region1;
user = sys.argv[1];
region = sys.argv[2];
print("Your username: " + user)
print("Your region: " + region)
if region == 'NA':
region = 'NA1';
elif region == 'BR':
region = 'BR1';
elif region == 'LAN':
region = 'LA1';
elif region == 'LAS':
region = 'LA2';
elif region == 'OCE':
region = 'OC1';
elif region == 'KR':
region = 'KR';
elif region == 'JP':
region = 'JP1';
elif region == 'EUNE':
region = 'EUN1';
elif region == 'EUW':
region = 'EUW1';
elif region == 'RU':
region = 'RU';
elif region == 'TR':
region = 'TR1';
# global variables
api_key = 'api key'
watcher = LolWatcher(api_key)
my_region = 'EUW1'
if region == 'NA1':
my_region1 = 'AMERICAS';
elif region == 'BR1':
my_region1 = 'AMERICAS';
elif region == 'LA1':
my_region1 = 'AMERICAS';
elif region == 'LA2':
my_region1 = 'AMERICAS';
elif region == 'OC1':
my_region1 = 'AMERICAS';
elif region == 'KR':
my_region1 = 'ASIA';
elif region == 'JP1':
my_region1 = 'ASIA';
elif region == 'EUN1':
my_region1 = 'EUROPE';
elif region == 'EUW1':
my_region1 = 'EUROPE';
elif region == 'RU':
my_region1 = 'EUROPE';
else:
my_region1 = 'EUROPE';
me = watcher.summoner.by_name(my_region, user)
print(me)
# Return the rank status for User
my_ranked_stats = watcher.league.by_summoner(my_region, me['id'])
print(my_ranked_stats)
my_matches = watcher.match.matchlist_by_puuid(my_region1, me['puuid'])
#print(my_matches)
m1 = my_matches[0]
#print(m1)
# fetch last match detail
#last_match = my_matches['matches'][0]
match_detail = watcher.match.by_id(my_region1, m1)
participants = []
for row in match_detail['info']['participants']:
participants_row = {}
participants_row['summonerName'] = row['summonerName']
participants_row['individualPosition'] = row['individualPosition']
participants_row['championName'] = row['championName']
participants_row['champLevel'] = row['champLevel']
participants_row['kills'] = row['kills']
participants_row['deaths'] = row['deaths']
participants_row['assists'] = row['assists']
participants_row['visionScore'] = row['visionScore']
participants_row['goldEarned'] = row['goldEarned']
participants_row['item0'] = row['item0']
participants_row['item1'] = row['item1']
participants_row['item2'] = row['item2']
participants_row['item3'] = row['item3']
participants_row['item4'] = row['item4']
participants_row['item5'] = row['item5']
participants_row['item6'] = row['item6']
participants.append(participants_row)
df = pd.DataFrame(participants)
df
print(participants)
PHP file:
<?php
$escape = 'C:\pythongym\Scripts\main.py "'.$_POST['Username'].'" "'.$_POST['region'].'"';
$command_exec = escapeshellcmd($escape);
$str_output = shell_exec($command_exec);
echo $str_output;
?>
I've managed to get it to work before but this time the python file is longer and I don't know what changed to get it not to work this time.
This is the other code: html file:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Pyttan</title>
</head>
<body>
<form action="python.php" method="post">
Input: <input type="text" name="in1" /><br /><br />
<input type="submit" />
</form>
</body>
</html>
python file:
import sys
answer = sys.argv[1]
print("Answer: " + answer)
php file:
<?php
$escape = 'C:\pythongym\Scripts\test.py "'.$_POST['in1'].'"';
$command_exec = escapeshellcmd($escape);
$str_output = shell_exec($command_exec);
echo $str_output;
?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
