Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6109281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:23:14+00:00 2026-05-23T14:23:14+00:00

I have a great command line program that prints to stdout. I want to

  • 0

I have a great command line program that prints to stdout. I want to let people run this through CGI. In the CGI version, I want to have the same output (with "<BR>" instead of "\n").

My cgi_main works by calling my real_main; there are a lot of options to process and it was bad design to do that multiple times. But since real_main calls print, I don’t know how to change "\n" to "<BR>" post hoc.

I could easily change all instances of print to go to a tempfile and then dump that at the end, but it seems inelegant to change the existing command line version so much.

One option is to have real_main call another function (passing along argv); that new function returns values. Then real_main prints those values. cgi_main would call the same function, but print the values differently.

Another option is to use a class member/static function everywhere I use print now. This class could be told whether to reformat or not. Then switching between command line and CGI output would just require changing the reform argument.

Here is an example of how I thought this class and function could work:
class cgi_tools:
def init(self, reform = False):
self.reform = reform
def myprint(self, *args):
if not self.reform:
print(args)
for i,x in enumerate(args):
if type(x) == str:
self.myprint(x.replace(‘\n’, ‘
‘)),
elif type(x) in atoms: # check if x is an atom (i.e. int, float, etc.):
print x,
else:
self.myprint(x),
if i != len(args)-1:
print ‘ ‘,
print ”

With this I ran into trouble trying to think of all atoms (i.e. irreducible objects) in Python. I couldn’t find a built-in function for this test, and I thought writing it myself was a brittle idea.

I tried to use insepct to print the source code of print, but I couldn’t do that!
print sys.out.print:

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

All of this seems like a classic Python CGI question, but I couldn’t find a great solution. I know how to patch things together in a sloppy way and get what I want, but I would love your advice on how to be elegant. I want to grow, and I don’t want to program sloppily anymore.

Thanks a lot for your thoughts and advice.<br>
-Oliver

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:23:14+00:00Added an answer on May 23, 2026 at 2:23 pm

    At the start of your CGI script, put print "Content-type: text/plain\r\n\r\n". The result will show up on a webpage the same as on a console.

    In general, if I want to have two variations on a program, one that formats output in HTML and the other that outputs plain text, I abstract as much of the logic as I can into routines or objects that do no output, then have separate output routines for the variations. See for example http://jcomeau.unternet.net/src/colorforth/as/cf2text, which is symlinked to cf2html and cf2ansi; the same program produces 3 different outputs depending on which symlink is called. Note that this is old code and I don’t program the same way any more, but maybe you’ll get some useful ideas from it; maybe not.

    Actually, looking over the link I gave you, I used a different strategy there, using a global to indicate the desired output style. I don’t have any recent open-source code with a better example to give you, it’s all owned by my clients.

    Here’s one way to modify your output without changing your print statements:

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

    If you use this method, then you can change your content-type to text/html and use any other HTML functionality. For more complex modifications of the output you can use regular expression substitution. And if you want the output as lines, you can:

    >>> newout.seek(0)
    >>> newout.readlines()
    ['this\n', 'that\n', 'the other\n']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a module that needs to run a small .Net command-line program to
We have a command line application that could benefit from a GUI. We want
I have a real basic command-line program, in Objective-C, that searches for user inputed
I have a command that works great on the command line. It has lots
I have installed khtml2png and run it via command line and it works great
I am making this little program using command line arguments, I have like 90%
This command line parses a contact list document that may or may not have
I have come across this great function/command . Colour to RGB, you can do
Summary I have written a process monitor command-line application that takes as parameters: The
I have a command line php app that I need to distribute to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.