BaseHTTPRequestHandler reading .html with python code

G

gcmartijn

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 ?
 
A

aspineux

H!

I was wondering how I can do something like this.

Use a template engine like :

Genshi
Django/Jinja
Cheetah
Kid template

For more engine look at

http://www.turbogears.org/cogbin/

And maybe what you are looking fore is a complete framework like :

turbogears
pylon
django

Hope this help
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 ?
 

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
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top