Re: dynamically creating html code with python...

anartz at anartz.cjb.net anartz at anartz.cjb.net
Mon Aug 11 12:26:49 EDT 2008


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

[CODE]
#!c:/Python25/python.exe -u

import StringIO

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

print "Content-type: text/html\n"
print f.read()
f.close()
[/CODE]

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

Thanks!


Jerry Hill <malaclypse2 at gmail.com> wrote :

> On Mon, Aug 11, 2008 at 9:15 AM,  <anartz at anartz.cjb.net> wrote:
> > [CODE]
> > f=StringIO.StringIO()
> > f.write('<html><head><title>data analysis</title></head><body>')
> > f.write(urllib.urlopen("http://localhost/path2Libs/myLibs.py", urllib.urlencode(TheData)))
> > f.write("</body></html>")
> >
> > print "Content-type: text/html\n"
> > print f.read()
> > 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')
> f.seek(0)
> print f.read()
> 
> 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 f.read() with f.getvalue() without having to mess
> with seek(), but then your code won't work with real files, if that's
> important.
> 
> -- 
> Jerry
> --
> http://mail.python.org/mailman/listinfo/python-list






More information about the Python-list mailing list