• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1297
  • Last Modified:

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

  • 4
1 Solution
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?
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?
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.
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?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now