from pprint import pp from pathlib import Path from urllib import request import re src = Path('src') root = src.joinpath('error') with root.joinpath('root.html').open('r') as root_file: root_html = root_file.read() with root.joinpath('root.css').open('r') as root_file: root_css = root_file.read() error = Path('dist_error') error.mkdir(exist_ok=True) for x in error.rglob('*'): if x.is_file() or x.is_symlink(): x.unlink() req = request.Request( 'https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html', method='GET') with request.urlopen(req) as res: raw = res.read().decode('utf-8') codes = (re.finditer(r"(?s)
(.*?)
', raw)) nginx_head = '' nginx_body = '' for item in codes: code, title, desc = item.groups() if int(code) < 400 or int(code) >= 600: continue with error.joinpath(f'{code}.html').open('w') as html: html.write(root_html.format( code=code, title=title, desc=desc, style=root_css)) nginx_head += f'error_page {code} @error{code};\n' nginx_body += f'location @error{code} {{\n'\ f'root {error.resolve()};\n'\ f'try_files /{code}.html /{code}.html;\n'\ 'internal;}\n' with error.joinpath(f'nginx.conf').open('w') as nginx_file: nginx_file.write(f'{nginx_head}\n{nginx_body}\n\n'\ 'proxy_intercept_errors on;')