Can customize message of server

This commit is contained in:
Pierre HUBERT 2024-09-23 13:23:53 +02:00
parent c8917f3b71
commit 7cd6146ec9

View File

@ -1,4 +1,10 @@
from http.server import BaseHTTPRequestHandler, HTTPServer from http.server import BaseHTTPRequestHandler, HTTPServer
import os
message = "Hello, World! Here is simple HTTP response..."
if "MESSAGE" in os.environ:
message = os.environ["MESSAGE"]
PORT = 80 PORT = 80
@ -6,11 +12,13 @@ print(f"Will listen on port {PORT}")
class handler(BaseHTTPRequestHandler): class handler(BaseHTTPRequestHandler):
def do_GET(self): def do_GET(self):
global message
self.send_response(200) self.send_response(200)
self.send_header('Content-type','text/html') self.send_header('Content-type','text/html')
self.end_headers() self.end_headers()
message = "Hello, World! Here is simple HTTP response...\n" message = f"{message}\n"
self.wfile.write(bytes(message, "utf8")) self.wfile.write(bytes(message, "utf8"))
with HTTPServer(('', PORT), handler) as server: with HTTPServer(('', PORT), handler) as server: