Python CGI: Translate from stdout to html (e.g. replace \n with <BR>) -


i have great command line program prints stdout. want let people run through cgi. in cgi version, want have same output (with "<br>" instead of "\n").

my cgi_main works calling my real_main; there lot of options process , bad design multiple times. since real_main calls print, don't know how change "\n" "<br>" post hoc.

i change instances of print go tempfile , dump @ end, seems inelegant change existing command line version much.

one option have real_main call function (passing along argv); new function returns values. real_main prints values. cgi_main call same function, print values differently.

another option use class member/static function everywhere use print now. class told whether reformat or not. switching between command line , cgi output require changing reform argument.

here example of how thought class , function work: class cgi_tools: def init(self, reform = false): self.reform = reform def myprint(self, *args): if not self.reform: print(args) i,x in enumerate(args): if type(x) == str: self.myprint(x.replace('\n', '
')), elif type(x) in atoms: # check if x atom (i.e. int, float, etc.): print x, else: self.myprint(x), if != len(args)-1: print ' ', print ''

with ran trouble trying think of atoms (i.e. irreducible objects) in python. couldn't find built-in function test, , thought writing myself brittle idea.

i tried use insepct print source code of print, couldn't that! print sys.out.print:

import inspect print inspect.getsource(print) # not work! 

all of seems classic python cgi question, couldn't find great solution. know how patch things in sloppy way , want, love advice on how elegant. want grow, , don't want program sloppily anymore.

thanks lot thoughts , advice.<br> -oliver

at start of cgi script, put print "content-type: text/plain\r\n\r\n". result show on webpage same on console.

in general, if want have 2 variations on program, 1 formats output in html , other outputs plain text, abstract of logic can routines or objects no output, have separate output routines variations. see example http://jcomeau.unternet.net/src/colorforth/as/cf2text, symlinked cf2html , cf2ansi; same program produces 3 different outputs depending on symlink called. note old code , don't program same way more, maybe you'll useful ideas it; maybe not.

actually, looking on link gave you, used different strategy there, using global indicate desired output style. don't have recent open-source code better example give you, it's owned clients.

here's 1 way modify output without changing print statements:

>>> import sys, stringio >>> oldout = sys.stdout >>> sys.stdout = newout = stringio.stringio() >>> line in 'this', 'that', 'the other': print line ...  >>> sys.stdout = oldout >>> print newout.getvalue().replace('\n', '<br />\n') this<br /> that<br /> other<br /> 

if use method, can change content-type text/html , use other html functionality. more complex modifications of output can use regular expression substitution. , if want output lines, can:

>>> newout.seek(0) >>> newout.readlines() ['this\n', 'that\n', 'the other\n'] 

Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -