BaseHTTPRequestHandler reading .html with python code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gcmartijn@gmail.com

    #1

    BaseHTTPRequestHandler reading .html with python code

    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.localt ime()[7])
    </special pythoncodetag>

    webserver.py
    ---------
    def do_GET(self):
    try:
    if self.path.endsw ith(".html"):
    f = open(curdir + sep + self.path) #self.path has /
    test.html
    self.send_respo nse(200)
    self.send_heade r('Content-type', 'text/html')
    self.end_header s()
    # now display the <b>hello world</band run the python code.
    self.wfile.writ e(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 ?

  • aspineux

    #2
    Re: BaseHTTPRequest Handler reading .html with python code

    On 6 avr, 11:52, gcmart...@gmail .com wrote:
    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



    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.localt ime()[7])
    </special pythoncodetag>
    >
    webserver.py
    ---------
    def do_GET(self):
    try:
    if self.path.endsw ith(".html"):
    f = open(curdir + sep + self.path) #self.path has /
    test.html
    self.send_respo nse(200)
    self.send_heade r('Content-type', 'text/html')
    self.end_header s()
    # now display the <b>hello world</band run the python code.
    self.wfile.writ e(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 ?

    Comment

    Working...