Re: dynamically creating html code with python...

anartz at anartz at
Mon Aug 11 18:26:49 CEST 2008

I have tried calling a script containing the code below from a web browser and it did not get the text.

#!c:/Python25/python.exe -u

import StringIO

f.write('<html><head><title>data analysis site</title></head><body>')
f.write("<p>This is a trial test</p>")

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

So, I assume this is not the way to create web pages.... any links that can help me take the right way? 


Jerry Hill <malaclypse2 at> wrote :

> On Mon, Aug 11, 2008 at 9:15 AM,  <anartz at> wrote:
> > [CODE]
> > f=StringIO.StringIO()
> > f.write('<html><head><title>data analysis</title></head><body>')
> > f.write(urllib.urlopen("http://localhost/path2Libs/", urllib.urlencode(TheData)))
> > f.write("</body></html>")
> >
> > print "Content-type: text/html\n"
> > print
> > f.close()
> > [/CODE]
> >
> > What is wrong with this approach/code? Is there an easier way of doing it?
> A StringIO object works a lot like a file.  When you write to it, it
> keeps track of the current position in the file.  When you read from
> it, it reads from the current position to the end of the file.  Once
> you're done writing to the StringIO object, you can rewind the
> position to the beggining and then read to the end, like this:
> f = StringIO.StringIO()
> f.write('This is some data')
> print
> StringIO objects also have a special getvalue() method, which allows
> you to get the entire contents without changing the current position.
> You can replace your with f.getvalue() without having to mess
> with seek(), but then your code won't work with real files, if that's
> important.
> -- 
> Jerry
> --

More information about the Python-list mailing list