3 Use this in the same way as Python's SimpleHTTPServer:
7 The only difference is that, for files ending in '.html', ssi_server will
8 inline SSI (Server Side Includes) of the form:
10 <!-- #include virtual="fragment.html" -->
12 Run ./ssi_server.py in this directory and visit localhost:8000 for an exmaple.
17 from SimpleHTTPServer
import SimpleHTTPRequestHandler
18 import SimpleHTTPServer
22 class SSIRequestHandler(SimpleHTTPRequestHandler
):
23 """Adds minimal support for <!-- #include --> directives.
25 The key bit is translate_path, which intercepts requests and serves them
26 using a temporary file which inlines the #includes.
29 def __init__(self
, request
, client_address
, server
):
31 SimpleHTTPRequestHandler
.__init__(self
, request
, client_address
, server
)
34 SimpleHTTPRequestHandler
.do_GET(self
)
35 self
.delete_temp_files()
38 SimpleHTTPRequestHandler
.do_HEAD(self
)
39 self
.delete_temp_files()
41 def translate_path(self
, path
):
42 fs_path
= SimpleHTTPRequestHandler
.translate_path(self
, path
)
43 if self
.path
.endswith('/'):
44 for index
in "index.html", "index.htm":
45 index
= os
.path
.join(fs_path
, index
)
46 if os
.path
.exists(index
):
50 if fs_path
.endswith('.html'):
51 content
= ssi
.InlineIncludes(fs_path
)
52 fs_path
= self
.create_temp_file(fs_path
, content
)
55 def delete_temp_files(self
):
56 for temp_file
in self
.temp_files
:
59 def create_temp_file(self
, original_path
, content
):
60 _
, ext
= os
.path
.splitext(original_path
)
61 fd
, path
= tempfile
.mkstemp(suffix
=ext
)
65 self
.temp_files
.append(path
)
69 if __name__
== '__main__':
70 SimpleHTTPServer
.test(HandlerClass
=SSIRequestHandler
)