learning python, using string help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mikeisgreat@gmail.com

    #1

    learning python, using string help

    hi all,

    I have a simple snippet I am trying to keep the format the same as
    plain text, though I want to embed it in html ...

    basically,

    print "Content-type:text/plain\n\n";
    getrup = os.popen('rupti me').read()
    print getrup

    is the same format as if I ran 'ruptime' from the command line.

    If I use...

    print "Content-type:text/html\n\n";
    getrup = os.popen('rupti me').read()
    print getrup

    I can just split the lines and loop through it ... but I am not sure
    how to keep the tab spacing the same
    I'm sure its something so simple I am missing ....

    thanks for helping someone learning..
    --mike

  • Paul McGuire

    #2
    Re: learning python, using string help


    <mikeisgreat@gm ail.com> wrote in message
    news:1138906062 .727850.177960@ g14g2000cwa.goo glegroups.com.. .[color=blue]
    > hi all,
    >
    > I have a simple snippet I am trying to keep the format the same as
    > plain text, though I want to embed it in html ...
    >
    > basically,
    >
    > print "Content-type:text/plain\n\n";
    > getrup = os.popen('rupti me').read()
    > print getrup
    >
    > is the same format as if I ran 'ruptime' from the command line.
    >
    > If I use...
    >
    > print "Content-type:text/html\n\n";
    > getrup = os.popen('rupti me').read()
    > print getrup
    >
    > I can just split the lines and loop through it ... but I am not sure
    > how to keep the tab spacing the same
    > I'm sure its something so simple I am missing ....
    >
    > thanks for helping someone learning..
    > --mike
    >[/color]
    What about :

    print "Content-type:text/html\n\n";
    print "<pre>"
    getrup = os.popen('rupti me').read()
    print getrup
    print "</pre>"

    The <pre> tags indicate that the enclosed text has been pre-formatted.
    Otherwise your tabs will get collapsed out by HTML.

    -- Paul


    Comment

    • mikeisgreat@gmail.com

      #3
      Re: learning python, using string help

      Well, I did want to add some formatting for example

      STATUS = "up"

      getrup = os.popen('rupti me').read()
      show = getrup.splitlin es()
      gethost = show[0]
      hostname = gethost.split()
      print hostname[0]
      getstatus = hostname[1]

      if getstatus.find( "STATUS"):
      print STATUS
      else:
      print "<font color='RED'>HOS T DOWN</font>"

      I think that will convey what I am attempting.


      I know that is sloppy, but I am learning ..
      Thanks for your patience.

      Comment

      • Tom Anderson

        #4
        Re: learning python, using string help

        On Thu, 2 Feb 2006, mikeisgreat@gma il.com wrote:
        [color=blue]
        > Well, I did want to add some formatting for example[/color]

        I getcha. This is really an HTML problem rather than a python problem,
        isn't it? What you need to do is output a table.

        FWIW, here's how i'd do it (assuming you've got HP-UX ruptime, since
        that's the only one i can find example output for [1]):



        You can use this as a library, a command-line tool, or a CGI script; it'll
        automagically detect which context it's in and do the right thing. It's
        built around the output from the HP-UX version of ruptime; let me know
        what the output from yours looks like (a few lines would do) and i'll show
        you how to change it.

        The first key bit is a pair of regular expressions:

        lineRe = re.compile(r"(\ S+)\s+(\S+)\s+([\d+:]+)(?:,\s+(\d+) users,\s+load ([\d., ]+))?")
        uptimeRe = re.compile(r"(? :(\d+)\+)?(\d*) :(\d*)")

        These rip the output from ruptime apart to produce a set of fields, which
        can then be massaged into useful data. Once that's done, there's a big
        splodge of code which prints out an HTML document containing a table
        displaying the information. It's probably neither the shortest nor the
        cleanest bit of code in the universe, but it does the job and should, i
        hope, be reasonably clear.

        tom

        [1] http://docs.hp.com/en/B2355-90743/ch06s02.html

        --
        Science Never Sleeps

        Comment

        • mikeisgreat@gmail.com

          #5
          Re: learning python, using string help

          thanks tom,

          I am running OpenBSD, NetBSD as well as OS X (FreeBSD)

          My first python script

          #!/usr/local/bin/python

          print "Content-type:text/html\n\n";

          import os, string

          getrup = os.popen('rupti me').read()
          show = getrup.splitlin es()

          for line in show:
          if line.find("up" or "down"):
          UP = "<font color='GREEN'>U P</font>"
          DOWN = "<font color='RED'>DOW N</font>"
          line = line.replace("u p", UP )
          line = line.replace("d own", DOWN )

          print "<pre>%s</pre>" % line

          But I was refering to more or lest format specifiers like %2d, %2s,
          %.2f
          I know I can do it in HTML or CSS.

          But I am glad you sent me that code ... it has helped me learn.

          --thanks

          Comment

          • mikeisgreat@gmail.com

            #6
            Re: learning python, using string help

            Tom, the script you referenced me errored ... But I will see if I can
            get it working.

            Comment

            • mikeisgreat@gmail.com

              #7
              Re: learning python, using string help

              silly newbie mistake

              your code runs fine on my openbsd box. ( I didnt uncomment the return
              map(...) line

              thanks for the awesome example!

              --mike

              Comment

              • Tom Anderson

                #8
                Re: learning python, using string help

                On Fri, 2 Feb 2006, mikeisgreat@gma il.com wrote:
                [color=blue]
                > silly newbie mistake
                >
                > your code runs fine on my openbsd box. ( I didnt uncomment the return
                > map(...) line[/color]

                My apologies - i should have made it clearer in the comment that it was
                hardwired to return example data!
                [color=blue]
                > thanks for the awesome example![/color]

                I'm not sure how awesome it is - it's pretty simple, and probably has lots
                of bugs. Is the BSD ruptime output format the same as on HP-UX? I have a
                Mac myself, but no local machines broadcasting rwho data, so i don't get
                any output to play with when i run ruptime!

                tom

                --
                hip whizzo teddy bear egghead realpolitik tiddly-om-pom-pom
                sacred cow gene blues celeb cheerio civvy street U-boat tailspin
                ceasefire ad-lib demob pop wizard hem-line lumpenproletari at avant
                garde kitsch sudden death Big Apple sex drive-in Mickey Mouse bagel
                dumb down pesticide racism spliff dunk cheeseburger Blitzkrieg
                Molotov cocktail snafu buzz pissed off DNA mobile phone megabucks
                Wonderbra cool Big Brother brainwashing fast food Generation X
                hippy non-U boogie sexy psychedelic beatnik cruise missile cyborg
                awesome bossa nova peacenik byte miniskirt acid love-in It-girl
                microchip hypermarket green Watergate F-word punk detox Trekkie
                naff all trainers karaoke power dressing toy-boy hip-hop beatbox
                double-click OK yah mobile virtual reality gangsta latte applet
                hot-desking URL have it large Botox kitten heels ghetto fabulous
                dot-commer text message google bling bling 9/11 axis of evil sex
                up chav

                Comment

                Working...