SSI Server
Source
License
Fork
SSI Server Source Code
#!/usr/bin/env python3 ''' A simple http server with SSI support. Usage: ./ssi_server.py [--port 8000] [--dir .] SSI is enabled if file name ends with '.shtml'. Use ./ssi_server.py -g to generate html by expand shtml. ''' __author__ = 'Michael Liao' __version__ = '1.0' import re, os, urllib, shutil, argparse, functools, mimetypes from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler, HTTPStatus AUTO_GENERATED = 'AUTO GENERATED HTML: DO NOT MODIFY MANUALLY' DIR_IGNORE = ['node_modules', 'artifacts', 'cache', 'build', 'bin', 'tests'] INCLUDE_FILE = re.compile(r'^\s*\<\!\-\-\s*\#include\s+file\=\"([a-zA-Z0-9\-\_\.\/]+)\"\s*\-\-\>\s*$') mimetypes.add_type('text/html', '.shtml') def _expand_line(doc_root, line): ''' Return line itself or file content by SSI directive ''' m = INCLUDE_FILE.match(line) if m: inc_file = m.group(1) print(f'found include: {inc_file}') if inc_file.endswith('.shtml'): return _expand_shtml(doc_root, inc_file) else: with open(os.path.join(doc_root, inc_file), 'r', encoding='utf-8') as fp: inc_content = fp.read() if inc_content[-1] != '\n': inc_content = inc_content + '\n' return inc_content else: if line.find(' ''' with open(os.path.join(doc_root, fs), 'r', encoding='utf-8') as fp: lines = fp.readlines() slines = [_expand_line(doc_root, line) for line in lines] return f'\n' + ''.join(slines) def _generate(doc_root, fs, force_overwrite): ''' Process xxx.shtml and generate xxx.html. ''' fh = fs[:-6] + '.html' print(f'generate {fs} to {fh}...') html = _expand_shtml(doc_root, fs) if os.path.exists(fh): with open(fh, 'r', encoding='utf-8') as fp: first_line = fp.readline() if not first_line.startswith('