Python CGI script - redirect doesn't always work -


i'm writing small cgi script assignment (python 2.4) takes form data, runs shell command it, , displays 1 or version of own page depending on did. e.g. if add comment, reloads "item" version of page rather "list of items" view, incorporating new comment. there several places in program it's supposed reload itself. in 1 place works , in 1 place doesn't , i'm wracking brain trying see difference.

if mode == "change":     if newcomment != "":         comment_command = "some shell command \"" + item + "\" " + comment         os.system(comment_command)     if rating != "":         rate_command = "same command \"" + item + "\" " + rating         os.system(rate_command)  # never works!     print "%s%s" % ("location:http://blahblah/cgi-bin/myproject.cgi?item=", urllib.quote_plus(item))   elif mode == "newitem":     add_command = "command \"" + newitem + "\""     result = os.system(add_command)     retcode = os.wexitstatus(result)     # redirect depending on results     if retcode == 1:         # 1 works!         print "%s%s" % ("location:http://blahblah/cgi-bin/myproject.cgi?item=", urllib.quote_plus(newitem))     else:         print("location:http://blahblah/cgi-bin/myproject.cgi") 

i hope enough of code. don't see why works in 1 place , not another. assume it's ignoring both redirects , "falling past" attempt @ redirect, except ?item= version work in 1 place. there os.system don't understand?

if command os.system print anything, location header invalid.

  • ensure os.system not output anything
  • if should, location header should go before data print
  • prefer subprocess module instead of os.system :
     import subprocess;     subprocess.popen(command, shell=true).communicate() 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -