diff --git a/runprint4html.py b/runprint4html.py new file mode 100644 index 0000000..79d1633 --- /dev/null +++ b/runprint4html.py @@ -0,0 +1,27 @@ +from http.server import SimpleHTTPRequestHandler, HTTPServer +import os + +class MyHandler(SimpleHTTPRequestHandler): + def do_GET(self): + if self.path == '/get_file_content': + filename = '找找自己問題.txt' + if os.path.exists(filename): + with open(filename, 'r', encoding='utf-8') as f: + content = f.read() + self.send_response(200) + self.send_header('Content-type', 'text/plain; charset=utf-8') + self.end_headers() + self.wfile.write(content.encode('utf-8')) + else: + self.send_response(404) + self.send_header('Content-type', 'text/plain; charset=utf-8') + self.end_headers() + self.wfile.write(b'File not found.') + else: + super().do_GET() + +if __name__ == '__main__': + port = 8000 + httpd = HTTPServer(('localhost', port), MyHandler) + print(f"Serving on port {port}") + httpd.serve_forever()