开发者

Different results when invoking a python script from the command line or in the web server

I am writing a python script to run on a apache web server. My first goal is to list the network interfaces that are available and, after, for each one, build a form to input some parameters of interest. My problem is that when I run the following script from the command line I get the expected result (a formatted html web page with form(s)) while if I assess it from the web, i.e. putting the script in my web server and remotely access it through http://myipaddr/cgi-bin/myscript.py, I get only the submission button and not the form(s).

#!/usr/bin/python
# import required modules
import re
import cgi
from subprocess import *

var=Popen("ifconfig", stdout=PIPE, shell=True).stdout.read()
result = re.findall("wlan[1-9]", var)

def DisplayForm():

    HTMLFormL1= '\n\nInterface:<BR> <INPUT TYPE=TEXT NAME="interface%d" size=60><BR>\n'
    HTMLFormL2= 'Number of packets to send:<BR> <INPUT TYPE=TEXT NAME="npackets%d" size=60><BR>\n'
    HTMLFormL3= 'Tra开发者_运维问答nsmission channel:<BR> <INPUT TYPE=TEXT NAME="channel%d" size=60><BR>\n'
    HTMLFormL4= 'Sleep time in usec:<BR> <INPUT TYPE=TEXT NAME="sleeptime%d" size=60><BR><BR><BR>\n'

    HTMLForm = HTMLFormL1 + HTMLFormL2 + HTMLFormL3 + HTMLFormL4

    HTMLStart = '<FORM METHOD="POST" ACTION="caos.py">\n<INPUT TYPE=HIDDEN NAME="key" VALUE="process">\n'

    for num in range(len(result)):
        HTMLForm_idx = HTMLForm % (num, num, num, num)
        HTMLStart = "%s%s" % (HTMLStart, HTMLForm_idx)

    HTMLBody = HTMLStart + '\n<BR><P><INPUT TYPE="SUBMIT" VALUE="Configure">\n</FORM>\n'

    print "Content-Type: text/html\n\n"
    HTMLHeader ='<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">\n<html>\n<head>\n<META NAME="keywords" CONTENT="blah blah -- your ad here">\n<title>CAOS</title>\n</head>\n<body>'
    HTMLFooter ='</body>\n</html>'
    print HTMLHeader
    print HTMLBody
    print HTMLFooter


#--- Begin of "main"
form = cgi.FieldStorage()
try:
    key = form["key"].value
except:
    key = None
if key != "process":
    DisplayForm()

I have looked for a similar problem, but I could not find anything similar on the web. Most likely I'm doing something stupid, but I cannot figure it out myself. I would very happy if someone could point me the right direction.

Cheers, bman


Probably different versions of python are being run. Check if your ssh user and web server user (www) is running same python from same path.


Try commenting out Popen and replace result with mock values. If Popen is causing the problem (as I suspect), you probably will want to obtain the values in a different manner than through an stdout pipe.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜