473,394 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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.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</band 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 ?

Apr 6 '07 #1
1 4400
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

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</band 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 ?

Apr 6 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ben | last post by:
Hi all, I have written a web server that listens for requests: class MyHandler(BaseHTTPRequestHandler): def do_GET(self): if (self.path)=='/': data=makeList() else:
4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
2
by: Andy Leszczynski | last post by:
Does BaseHTTPServer.BaseHTTPRequestHandler support HTTP protocol chunking? Thanks, Andy
0
by: travis ray | last post by:
Hi, I have an extension in which a file object is created in python and passed down to a c extension which attempts to read from it or write to it. Writing to the file pointer seems to work...
8
by: Andrew Robert | last post by:
Hi Everyone. I tried the following to get input into optionparser from either a file or command line. The code below detects the passed file argument and prints the file contents but the...
1
by: Christopher J. Bottaro | last post by:
Hi, I can't for the life of me figure out how to get the post vars when using basehttpserver. Here's my code: <code> class MyHandler(BaseHTTPRequestHandler): def do_POST(self):
1
by: Maksim Kasimov | last post by:
Hi, I'm trying to write an server-application, using BaseHTTPServer/BaseHTTPRequestHandler. When application is running, some client-application can post data to the server, and...
10
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
12
by: WaterWalk | last post by:
Hello. I wonder what's the effective way of figuring out how a piece of python code works. With C I often find it very useful to be able to run the code in step mode and set breakpoints in a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.