Execute bat file through python cgi

I am running a apache 2.2 server with python cgi scripting. I am trying to execute a batch file from a script which deploys installers to machines on a domain remotely. The batch file works in its own right, and running my python script from the IDLE shell, i can get it to work as well. There is something wrong with it in apache. I am getting the following error:

malformed header from script. Bad header=0 File(s) copied: script.py, referer: http://localhost/host_add_agent.htm

below is the code for my script. The hosts it will depoly to comes from a form on the previous page which has checkboxes. If ticked, the checkbox parameter is 'yes', if unticked it is 'no'.

How can i make this script work??
print "Content-type: text/plain"
import sys
import cgi
import os
import subprocess
from subprocess import Popen, PIPE
form = cgi.FieldStorage()
hosts = {}
for item in form :
	if str(form.getvalue(item)) == 'yes':
		hosts[item] = 'yes'
os.chdir('C:\\Program Files\\Apache Software Foundation\\Apache2.2\\cgi-bin\\deploy\\')
for host in hosts:
	host = str(host)
	os.system('deploy.bat '+host)
	#Popen('deploy.bat ' + host, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HonorGodSoftware EngineerCommented:
In order for this to work, you need the python script to respond to the request in the proper fashion.

When a request is received by the web server, the first part of the response needs to identify the kind of response it is (e.g., content_type = 'text/plain')

This script is not designed to be executed in that fashion.  It is designed, and written to be executed as a stand along script.

So, you are either going to have to re do it to fit the framework of the web server request/response protocol, or you are going to have to have a wrapper of some sort that handles the request/response properly and allows stand-alone scripts to be executed.

Does this make sense?
HonorGodSoftware EngineerCommented:
Take a look at this tutorial:

bman87Author Commented:
i had a look through that tutorial. had some useful things in it, thanks.
When you say use a wrapper to handle request/response...how would i go about doing this? I am using apache 2.2 - do you know how i need to wrap the program so as apache can handle its standalone function?
HonorGodSoftware EngineerCommented:
Well, I was thinking of having all of the requests routed to a single program that somehow routed to the individual program/script.  I was trying though, to figure out how to have this done.

One way would be for the "wrapper" script (i.e., the one that gets executed), to "exec" another python script based upon the request that was received.  This program would write it's output in such a way as to have the response sent to the client browser.

So, in order to do this, the content-type could either be set up by the wrapper script, or by the executed script.  Whatever you decide.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HonorGodSoftware EngineerCommented:
Is your Python file getting executed?

I would consider having it write messages not only to the console (i.e., "stdout"), but also to a file, in order to see what statements are being executed, in what order.

Does this make sense?

Have you made any additional progress?

What else can I do to help?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.