learning python, using string help

M

mikeisgreat

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('ruptime').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('ruptime').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
 
P

Paul McGuire

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('ruptime').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('ruptime').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
What about :

print "Content-type:text/html\n\n";
print "<pre>"
getrup = os.popen('ruptime').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
 
M

mikeisgreat

Well, I did want to add some formatting for example

STATUS = "up"

getrup = os.popen('ruptime').read()
show = getrup.splitlines()
gethost = show[0]
hostname = gethost.split()
print hostname[0]
getstatus = hostname[1]

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

I think that will convey what I am attempting.


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

Tom Anderson

Well, I did want to add some formatting for example

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]):

http://urchin.earth.li/~twic/ruptime.py

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
 
M

mikeisgreat

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('ruptime').read()
show = getrup.splitlines()

for line in show:
if line.find("up" or "down"):
UP = "<font color='GREEN'>UP</font>"
DOWN = "<font color='RED'>DOWN</font>"
line = line.replace("up", UP )
line = line.replace("down", 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
 
M

mikeisgreat

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

mikeisgreat

silly newbie mistake

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

thanks for the awesome example!

--mike
 
T

Tom Anderson

silly newbie mistake

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

My apologies - i should have made it clearer in the comment that it was
hardwired to return example data!
thanks for the awesome example!

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 lumpenproletariat 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,283
Messages
2,571,405
Members
48,098
Latest member
inno vation

Latest Threads

Top