1

I have a python script that opens a file. It will run in terminal and it will open the readme.html file and prints everything to terminal. When i access the python.php page i want it to execute the python file and open the readme.html. It runs the python file with no errors and prints out both "I am Here" and "I am Done" but does not open the readme.html file. I think it could be a permissions issue. But i have 777 main.py, python.php, and readme.html. They are all user:(none) as well. How can i fix this?

main.py

#!C:/Python27/python.exe
import os
print "I am Here"

os.startfile('C:/wamp/www/readme.html')
print "I am Done"

python.php

echo "Python test<br/>";
echo exec("C:/Python27/python.exe C:/wamp/www/main.py",$output);
var_dump($output);
echo "After";
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
rubio
  • 936
  • 5
  • 16
  • 36
  • Any errors in your server log? – mata Jun 29 '13 at 20:47
  • Why do you think os.startfile is appropriate? Do you really want the apache user to open its own web browser to view a file? – Wooble Jun 29 '13 at 20:55
  • @mata no errors in the server log because php executes successfully and the python script runs through. It just doesnt open the file – rubio Jun 29 '13 at 21:00
  • @Wooble This is for a media server on my LAN. So basically a user dbls clicks on a video file either from the desktop on localhost or from their mobile device thats connect to my LAN and the pc opens the file in its default player. Im just learning and messing around with python :) – rubio Jun 29 '13 at 21:02
  • 1
    the webserver is probably running as different user, I don't think `os.startfile` will work that way. – mata Jun 29 '13 at 21:06
  • You were right it is running as a different user and i cannot run it that way. – rubio Jul 04 '13 at 14:57

1 Answers1

0

Programs started as services cannot access the desktop by default in Windows. Apache is probably running through the service framework, so the python script cannot open GUI programs and make them appear on your desktop.

There are workarounds, for example you can set the "Interactive" flag on the windows service. However, all of them are ugly. Here's a couple references if you want to go down this path:

But I wouldn't suggest trying that. Fiddling with Windows' configuration becomes frustrating very quickly.

Instead, you should run a Python program from the desktop and communicate to it using some sort of IPC method. For IPC you could use sockets, ZeroMQ, XML-RPC, JSON-RPC, or many, many others. For example, Apache would run a PHP script, which runs a Python script, which sends a message using XML-RPC to tell your running Python program to launch the given file.

Good luck with your project.

Community
  • 1
  • 1
Stu Gla
  • 1,129
  • 12
  • 16