2

I am running an exe with python script which is called by the web browser.

Exe file is stored on server side. Exe file takes an input file and in output returns several text files. The python script running the exe is as follows:

import subprocess
print ("Hello I am in python script")

args = "/path/import.exe /path/input_file.txt -terms".split()

popen = subprocess.Popen(args, stdout=subprocess.PIPE)

popen.wait()

output = popen.stdout.read()

When I execute the python script individually it runs as expected and outputs several text files in cgi-bin folder.

But I want to run this exe file from my web browser, therefore I have placed this in cgi-bin folder on my xampp server.

On my web browser I enter the URL as follows: http://localhost/cgi-bin/script.py

The script is executed but I am not able to see expected output of the exe file which works perfectly fine otherwise in case of individual execution on terminal.

I have edited the httpd.conf file to allow .py files to be handled on web server. The script.py file has a path given to the python installed on system. Path given is absolute path. Permission given to exe file, script.py is chmod 777 as of now, for testing purpose.

I am unable to capture any error from the subprocess module of python. I have used stderr in the module but it does not print any information.

Vivek Ette
  • 60
  • 10
  • Serve a `txt` file and why used an `exe` ? A important point `wwwuser not equal to os level user`. Which OS ? On windows: `Need create right system variable for python files`, in your script: `exe run on os` / `which app handle your script`(Apache CGI directory definitions)? Don't forget a CGI script always start with `#! /usr/bin/python `(on linux or equivalent..) – dsgdfg Jul 14 '16 at 06:48
  • Exe file is a program which performs conversion operation on a input text file. I am working on Mac. I have ensured 777 permissions to script.py, to .exe file and cgi-bin directory. And I also started by CGI script with #! /usr/bin/python . I am not sure about the wwwuser but giving all permissions to the files should not be a problem. – Vivek Ette Jul 14 '16 at 07:04
  • Exe on MAC ? Which version of `WINE` ? – dsgdfg Jul 14 '16 at 07:12
  • my exe file is compiled from c source code files on mac. I am not sure of WINE version at present but I can check that out. – Vivek Ette Jul 14 '16 at 07:22
  • Friend Mac don't use exe. So is standart app for mac ?(forget Wine questions.) – dsgdfg Jul 14 '16 at 07:24
  • `proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)` My opinion : wrong arguments,paths,permission etc. – dsgdfg Jul 14 '16 at 07:30
  • This exe file has been compiled and been created as an executable file on mac and is basically C code. objective of this exe is to work on an input file uploaded from browser and store output on server side. exe file is stored on server side itself. xampp is server being used. – Vivek Ette Jul 14 '16 at 07:32
  • You haven't any traceback point for related things. I offer you use `stderr=subprocess.PIPE` for check `exe` or `script` are `OK` ? Where occurred a error ? apache handle, script, exec, os ? You show finger and want full body checkup ? – dsgdfg Jul 14 '16 at 08:56
  • @dsgdfg I have used stderr=subprocess.PIPE as stated in my question. It is not printing any output relevant to any error when I am running the python script from browser. But it does not give the expected output. As mentioned earlier when running exec individually on terminal it does not give any error. When executing the script individually it does not give any error. As mentioned initially in my question, I want a way to capture error and debug it. – Vivek Ette Jul 14 '16 at 18:08
  • I see, file output not a cgi process. Need create a `cgi` handle with @Daniel answer. Additional `import cgitb; cgitb.enable()` or use a `main` try-except for grab all error messages. Python script not a `cgi` app. Grab `None` error output cos Apache(or whatever) running script. For linux `cat /var/www/{DOMAIN_NAME}/logs/error.log` or where stored apache error log. – dsgdfg Jul 15 '16 at 07:24

1 Answers1

0

You have the redirect the output to the page, doing something like this before printing stuff out.

sys.stdout.write("Content-Type: text/html\r\n\r\n")

Maybe this can help you: Display the result on the webpage as soon as the data is available at server

Community
  • 1
  • 1
Daniel
  • 49
  • 3
  • This did not help. And I do not need the output to be sent to browser. Exe file has to execute on the server side and store the output text files on the server side. – Vivek Ette Jul 14 '16 at 06:42