BaseHTTPRequestHandler reading .html with python code

gcmartijn at gmail.com gcmartijn at gmail.com
Fri Apr 6 05:52:10 EDT 2007


H!

I was wondering how I can do something like this.

file.html
---------
<b>hello world</b>
<special pythoncodetag>
print 'hello world'
#or display str(time.localtime()[7])
</special pythoncodetag>

webserver.py
---------
    def do_GET(self):
        try:
            if self.path.endswith(".html"):
                f = open(curdir + sep + self.path) #self.path has /
test.html
                self.send_response(200)
                self.send_header('Content-type',	'text/html')
                self.end_headers()
# now display the <b>hello world</b> and run the python code.
                self.wfile.write(f.read())
                f.close()
                return

I saw the mod python for apache and some PSP but I really want to know
how they do this.
Is there are special module for doing this ?

Or is there something like this:
---------------------------
1. a running python program
2. include with write(f.read()) a extra python code inside the already
running python program.

How they handle this ?




More information about the Python-list mailing list