Page 1 of 1
call python in php
Posted: Fri Dec 27, 2013 9:41 am
by kunnu
Hi ,
I am very new to PHP and writing a small web interface . Basically this has an image button and when I click it should install network printer.
My php code contains like this :
Code: Select all
<?php
$output = system('C:\Python27\python.exe c:\wamp\www\printer\scripts\pythoncode.py');
echo $output;
?>
My python code:
My python code contains like this :
import subprocess
import os
psxmlgen = subprocess.Popen([r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe',
'-ExecutionPolicy',
'Unrestricted',
'c:\wamp\www\printer\scripts\printer.ps1',
], cwd=os.getcwd())
result = psxmlgen.wait()
When I click on the image button and it comes to my php page but nothing displays on the page nor it adds .
When I run the same code like this in command prompt : "C:\Python27\python.exe c:\wamp\www\printer\scripts\pythoncode.py" it adds the printer without any issues .
Any suggestions on how to execute python script in php ... Sample code if possible as I am struggling a lot to execute this for last 2 weeks and searching google doesn't help in fixing myself either its because i dont know how to execute in php nor understanding..
Thanks a lot in advance...
Re: call python in php
Posted: Fri Dec 27, 2013 7:33 pm
by Eric!
That's an odd use for php. What errors are you getting in your error log?
It's possible that shell access is disabled. Check for system/exec/etc.:
Code: Select all
var_dump(@ini_get("disable_functions"));
You could also try exec
Code: Select all
exec('C:\Python27\python.exe c:\wamp\www\printer\scripts\pythoncode.py');
Re: call python in php
Posted: Fri Dec 27, 2013 8:40 pm
by kunnu
Hi Eric ,
Thank you for replying me back .
When I execute like you said here is what I get in the webpage:
string '' (length=0)
Tried in both the ways :
<?php
exec('C:\Python27\python.exe c:\wamp\www\printer\scripts\pythoncode.py');
var_dump(@ini_get("disable_functions"));
?>
<?php
$output = system('C:\Python27\python.exe c:\wamp\www\printer\scripts\pythoncode.py');
var_dump(@ini_get("disable_functions"));
echo $output;
?>
Both the ways I get the same error : string '' (length=0)
Please let me know what I am doing wrong ..
Re: call python in php
Posted: Fri Dec 27, 2013 9:41 pm
by kunnu
Also as far as I know shell is not disabled because I can run the powershell scripts and python scripts without any errors .
I enabled set-execution policy to unrestricted and remotesigned which should fix with the shell execution issues.
Re: call python in php
Posted: Sat Dec 28, 2013 1:33 am
by Eric!
Try looking in your php.ini file to see where error_log is being written and then look in that file for the error that is being generated.
Re: call python in php
Posted: Sat Dec 28, 2013 2:02 am
by kunnu
So, right now the error i get is :
[Sat Dec 28 13:19:40.053194 2013] [:error] [pid 4036:tid 1540] [client 127.0.0.1:55034] PHP Parse error: syntax error, unexpected '<' in C:\\wamp\\www\\printer\\rabbit.php on line 10, referer:
http://localhost/printer/
Exception calling "AddWindowsPrinterConnection" with "1" argument(s): "No print
ers were found. (Exception from HRESULT: 0x80070BC4)"
At C:\wamp\www\printer\scripts\printer.ps1:1 char:68
+ (New-Object -ComObject WScript.Network).AddWindowsPrinterConnection <<<< ("\\
file-workgroup.google.com.com\IN-SEA-GOOGLE")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
my php script is :
Code: Select all
<?php
$output = system('C:\Python27\python.exe c:\wamp\www\printer\scripts\pythoncode.py');
var_dump(@ini_get("disable_functions"));
echo $output;
?>
Re: call python in php
Posted: Sat Dec 28, 2013 11:14 am
by Eric!
Your script is called rabbit.php right? The error you're getting appears to be caused by python not finding the printer.
I never use windows, but I think you need to escape the slashes. Obviously you don't need the ini_get call in there, that was just to see if system was disabled. And $output will only have the last line output by your python script (see the example below for 'dir')
Code: Select all
$output = system('C:\\Python27\\python.exe c:\\wamp\\www\\printer\\scripts\\pythoncode.py');
I also suggest you break the problem into pieces and just get the shell working to start with.
Code: Select all
<?php
echo '<pre>';
// Outputs all the result of shellcommand "dir", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('dir', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>
Re: call python in php
Posted: Sun Dec 29, 2013 3:40 am
by kunnu
Calling python code always gives me an output with "String ' ' (length=0) as I am very new to php its getting me a very tough time in fixing it .
Do i need add any python related libraries in PHP ? I am trying this using WAMP on windows .
Actually my python code call powershell which contains line :
(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\omga-omgb\hr-printer") Even if I can call this as well I think that would be sufficient .
Re: call python in php
Posted: Sun Dec 29, 2013 8:19 pm
by Eric!
Take it in small steps. Were you able to run the PHP code I posted for you to test and see how it works?
If that works, the next test would be to write something simple in python that just says:
print "Python Running."
Then try to call it from PHP. If that is working then you can try debugging why calling your other printer python code doesn't seem to be working.