473,325 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Python cgi script

Hi, All:
I try to write a CGI script to post network traffic routing info in the web page.
I have successfully obtained traffic info from wraped python with C++ and MYSQL and
SNMP code and output to a file. I need read a file which contain traffic info and
post to web. In CGI script, I use:
file=open('/apps/www/htdocs/internal/nd/output1', 'r')
flag=0
while not flag
aLine = file.readline()
if aLine != "":
print aLine
print '\n'
else:
flag = 1
file.close()
print "</body>"
print "</html>"
The output from print statement above in web can display, but no line separation between different lines.
How can I preserve the orginal line format of the input file (space within a line and space between lines)?
Thank you very much for your help in advance.

I am looking forward to receiving your reply.

Yong

Jul 18 '05 #1
1 1392
On Thu, 5 Aug 2004, Yong Wang wrote:
file=open('/apps/www/htdocs/internal/nd/output1', 'r')
flag=0
while not flag
aLine = file.readline()
if aLine != "":
print aLine
print '\n'
else:
flag = 1
file.close()
print "</body>"
print "</html>" The output from print statement above in web can display, but no line
separation between different lines. How can I preserve the orginal line
format of the input file (space within a line and space between lines)?


HTML generally ignores whitespace. You need to either append a break tag
(<br/>) to each line, or enclose the output in <pre>...</pre>. Also don't
forget to start the document with <html><body>.

A couple of other notes:

file.readline() retains the line's newline terminator, and print adds one.
Your code ends up printing 4 newlines per actual line. Just using
'print aLine,' should work (the trailing comma prevents print from adding
a newline).

Assuming you are using a relatively recent version of Python (2.2 or 2.3,
not sure about 2.1), you can rewrite your loop using iteration over the
file object:

for aLine in file:
print aLine,

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Erik Johnson | last post by:
This is somewhat a NEWBIE question... My company maintains a small RDBS driven website. We currently generate HTML using PHP. I've hacked a bit in Python, and generally think it is a rather...
2
by: DeepBleu | last post by:
When one is using an HTML form via a web broswer, the user submits the form contents and these are passed to a CGI Python script on the web server. I need to write a client script that connects to...
52
by: Olivier Scalbert | last post by:
Hello , What is the python way of doing this : perl -pi -e 's/string1/string2/' file ? Thanks Olivier
4
by: Tom Purl | last post by:
I just wrote a Python script that is going to be called from bash script. If the Python script fails, I want the bash script to also stop running. Unfortunately, I can't seem to get that to work. ...
17
by: Paul Rubin | last post by:
Dumb question from a Windows ignoramus: I find myself needing to write a Python app (call it myapp.py) that uses tkinter, which as it happens has to be used under (ugh) Windows. That's Windows...
9
by: TPJ | last post by:
First I have to admit that my English isn't good enough. I'm still studying and sometimes I just can't express what I want to express. A few weeks ago I've written 'Python Builder' - a bash...
3
by: krzychu | last post by:
Hi, I have installed brand new platform - Zope-2-7-6, Python 2.4.1, Plone 2.0.5, OS Debian 1:3.3.6-2. After import a old Plone site from the following platform Zope-2-7-4, Python 2.3.3, Plone...
6
by: manatlan | last post by:
I've got a trouble, and i think that anybody there can help me I've got a python script which i distribute in somes packages for *nix. This script is full of python and need python 2.4 ! And i'd...
37
by: John Salerno | last post by:
I contacted my domain host about how Python is implemented on their server, and got this response: ------------------- Hello John, Please be informed that the implementation of python in our...
6
by: tatamata | last post by:
Hello. How can I run some Python script within C# program? Thanks, Zlatko
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.