PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 25th, 2013, 06:33 PM
MynE MynE is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 38 MynE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 18 m 33 sec
Reputation Power: 1
Run python scripts on PHP

Hi, I'm new in PHP and 'm looking for the way to run my Python scripts via PHP. I'm now using WINSCP to edit my index page where I can store my python code here. I have found some simple code to execute like this:

PHP Code:
<html>
<
body>
<
form>
Test: <input type="button" value="Submit" onclick="exec('TPHP.py');">
</
form>
</
body>
</
html


But it doesn't work at all, if someone knows this problem, please help me find out. Thank you very much.

Reply With Quote
  #2  
Old February 25th, 2013, 07:40 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Dec 2004
Posts: 8,057 E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 1 Day 6 h 4 m 32 sec
Reputation Power: 7104
onclick is a JavaScript handler; you cannot run PHP or Python code in it because it executes on the client side.

In a PHP script, you can run a python script using the exec function:
PHP Code:
 exec("python TPHP.py"); 


Depending on your server configuration, invoking the python script using the python binary may or may not be required. You might also be required to specify the full path to the python binary, like:
PHP Code:
 exec("/usr/bin/python TPHP.py"); 


If in doubt, ask your web host or server administrator.

As far as building a form to do it, you need two separate pages. For for the form, and one to process the form. The action attribute of the form tag specifies the PHP file that will be requested when the form is submitted.
__________________
PHP FAQ
How to program a basic, secure login system using PHP
Connect with me on LinkedIn


Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
  #3  
Old February 26th, 2013, 12:55 PM
MynE MynE is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 38 MynE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 18 m 33 sec
Reputation Power: 1
Thank you for your reply, I'm quite new in PHP and JavaScript so I don't know pretty much to do about this stuff. I have followed your instruction and I put exec like you said but looks like it doesn't work anyway, but when i tried to use "onclick" method to load my python's file, it works. Here's the code:
PHP Code:
<body>
<?
php echo "TPHP test";
exec("python TPHP.py");
?>
<form>
<input type="button" value="ViewCode" onclick="location="'python TPHP.py'">
</form>
</body> 


So my question is if onclick "location" can find my script by just typing the name of my python script, so the location for exec() should be the same right? Please help

Reply With Quote
  #4  
Old February 26th, 2013, 07:14 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Dec 2004
Posts: 8,057 E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 1 Day 6 h 4 m 32 sec
Reputation Power: 7104
Your form or button or link needs to take the user to a different PHP page. On the second PHP page, you need to run the exec call.

You cannot call the Python file from the form unless your web server is configured to serve Python scripts and your form takes the user to the URL of that Python script directly. However, in that case, you would not be calling Python from PHP.

Reply With Quote
  #5  
Old February 27th, 2013, 11:22 PM
MynE MynE is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 38 MynE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 18 m 33 sec
Reputation Power: 1
Hi, sorry to bother you again, I'm really curious about this so i tried to run the python script that can just print "Hello Python" on the screen, and it works, here's the code:

PHP Code:
<?php 
$output 
null;
exec('python HelloPython.py'$output$return);
print_r($output);
print_r($return)
?>

and the result is Array ( [0] => Hello!Python!Coming! ) 0

But when I change my python script to "TPHP.py" (This script is about to insert text to database just one line) it said:
Array ( ) 1 , it returns value 1 and the array is empty, moreover the data doesn't get in the database. Do you know why? Please help

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Run python scripts on PHP

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap