0
votes

I have a html file with one button. When the button is clicked, a javascript function "run" is called:

function run() {
            window.open("http://localhost/cgi-bin/run.py", "_self");
        }

run.py is simply trying to run a helloworld.exe program, that outputs in a terminal the string "helloworld", but nothing happens, and browser keeps "waiting for localhost" indefinitely.

#!python
import sys, string, os, cgitb

cgitb.enable()
os.system("helloworld.exe")

I have tried helloworld.exe alone and it works, I have run run.py on the terminal, and it worked, and also I have tested on the browser the test site http://localhost/cgi-bin/helloworld.py, and it worked fine (helloworld.py is another script just to see if my apache is configured OK).

I am using wamp.

What I am trying to do is a bigger program that allows a client connect to a server, and "interact" with a program on the server side. The program is already done in c++, and won't be translated into php or javascript.

EDIT: I have been trying with the functions: subprocess.Popen, subprocess.call and os.system. I have also tested the code to run .exe files created by me living at apache/cgi-bin folder or executables like wordpad, living at c:\windows. And it always succeeds when the python script runs from the terminal, and it never works when trying from the browser. Is it possible that it is because of the server I am using? I use the apache from wamp, and have added the sentence "AddHandler cgi-script .exe" to the httpd.conf file.

2
In your Javascript code, you seem to be missing a quotation mark just before the comma. - cyroxx
http://localhost/cgi-bin/helloworld.py or http://localhost/cgi-bin/run.py? - user707650
thanks, but I had misspelled the quotation mark, and had not explained well what I ment with helloworld.py. think now is corrected :) - toni

2 Answers

2
votes

I am sure it doesn't work locally. os.system returns you the exit code of the command, not its output.

You will need to use subprocess.Popen and pipeline the output to read the output.

import subprocess
p = subprocess.Popen("whatever.exe",stdout=subprocess.PIPE)
print p.stdout.read()
p.wait()

Also, the output of your CGI script is not valid HTTP protocol, and depending on your server that could be causing problems (some servers sanitize the output of the scripts, some others expect them to be written properly).

To me this code example works (on GNU/Linux using weborf as server, but it should be the same). You can try setting the content type and sending the \r\n\r\n final sequence. Servers are not expected to send that because the CGI script might want to add some more HTTP headers.

#!/usr/bin/env python

import cgitb
import subprocess
import sys

cgitb.enable()

sys.stdout.write("Content-type: text/plain\r\n\r\n")

p = subprocess.Popen("/usr/games/fortune",stdout=subprocess.PIPE)
data = p.stdout.read()
p.wait()

print data
0
votes

In general my experience has been that if something works locally and doesn't work when you install all the same code on a different server, you are looking at a permissions problem. Check the permissions that visitors on your site have and make sure that the user coming through your web server has the right permissions to run helloworld.exe and run.py.