|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Simple Hello World Python HTTP Server |
| 4 | +""" |
| 5 | + |
| 6 | +from http.server import HTTPServer, BaseHTTPRequestHandler |
| 7 | +import json |
| 8 | +import os |
| 9 | + |
| 10 | +class HelloWorldHandler(BaseHTTPRequestHandler): |
| 11 | + def do_GET(self): |
| 12 | + if self.path == '/': |
| 13 | + self.send_response(200) |
| 14 | + self.send_header('Content-type', 'text/html') |
| 15 | + self.end_headers() |
| 16 | + |
| 17 | + html_content = """ |
| 18 | + <!DOCTYPE html charset="utf-8"> |
| 19 | + <html lang="en"> |
| 20 | + <head> |
| 21 | + <title>Hello World Server</title> |
| 22 | + <style> |
| 23 | + body { |
| 24 | + font-family: Arial, sans-serif; |
| 25 | + max-width: 800px; |
| 26 | + margin: 50px auto; |
| 27 | + padding: 20px; |
| 28 | + background-color: #f5f5f5; |
| 29 | + } |
| 30 | + .container { |
| 31 | + background-color: white; |
| 32 | + padding: 30px; |
| 33 | + border-radius: 10px; |
| 34 | + box-shadow: 0 2px 10px rgba(0,0,0,0.1); |
| 35 | + text-align: center; |
| 36 | + } |
| 37 | + h1 { color: #333; } |
| 38 | + .status { color: #28a745; font-weight: bold; } |
| 39 | + </style> |
| 40 | + </head> |
| 41 | + <body> |
| 42 | + <div class="container"> |
| 43 | + <h1>Hello World! 🌍</h1> |
| 44 | + <p class="status">Server is running successfully!</p> |
| 45 | + <p>This is a simple Python HTTP server running in a Docker container.</p> |
| 46 | + </div> |
| 47 | + </body> |
| 48 | + </html> |
| 49 | + """ |
| 50 | + self.wfile.write(html_content.encode()) |
| 51 | + |
| 52 | + elif self.path == '/api/hello': |
| 53 | + self.send_response(200) |
| 54 | + self.send_header('Content-type', 'application/json') |
| 55 | + self.end_headers() |
| 56 | + |
| 57 | + response = { |
| 58 | + "message": "Hello World!", |
| 59 | + "status": "success", |
| 60 | + "server": "Python HTTP Server" |
| 61 | + } |
| 62 | + self.wfile.write(json.dumps(response, indent=2).encode()) |
| 63 | + |
| 64 | + elif self.path == '/health': |
| 65 | + self.send_response(200) |
| 66 | + self.send_header('Content-type', 'application/json') |
| 67 | + self.end_headers() |
| 68 | + |
| 69 | + health_response = { |
| 70 | + "status": "healthy", |
| 71 | + "message": "Server is running" |
| 72 | + } |
| 73 | + self.wfile.write(json.dumps(health_response).encode()) |
| 74 | + |
| 75 | + else: |
| 76 | + self.send_response(404) |
| 77 | + self.send_header('Content-type', 'application/json') |
| 78 | + self.end_headers() |
| 79 | + |
| 80 | + error_response = { |
| 81 | + "error": "Not Found", |
| 82 | + "message": f"Path {self.path} not found" |
| 83 | + } |
| 84 | + self.wfile.write(json.dumps(error_response).encode()) |
| 85 | + |
| 86 | + def do_POST(self): |
| 87 | + if self.path == '/api/echo': |
| 88 | + content_length = int(self.headers['Content-Length']) |
| 89 | + post_data = self.rfile.read(content_length) |
| 90 | + |
| 91 | + self.send_response(200) |
| 92 | + self.send_header('Content-type', 'application/json') |
| 93 | + self.end_headers() |
| 94 | + |
| 95 | + response = { |
| 96 | + "message": "Echo endpoint", |
| 97 | + "received_data": post_data.decode('utf-8'), |
| 98 | + "status": "success" |
| 99 | + } |
| 100 | + self.wfile.write(json.dumps(response, indent=2).encode()) |
| 101 | + else: |
| 102 | + self.send_response(404) |
| 103 | + self.send_header('Content-type', 'application/json') |
| 104 | + self.end_headers() |
| 105 | + |
| 106 | + error_response = { |
| 107 | + "error": "Not Found", |
| 108 | + "message": f"POST endpoint {self.path} not found" |
| 109 | + } |
| 110 | + self.wfile.write(json.dumps(error_response).encode()) |
| 111 | + |
| 112 | + def log_message(self, format, *args): |
| 113 | + print(f"[{self.date_time_string()}] {format % args}") |
| 114 | + |
| 115 | +def run_server(port=8000): |
| 116 | + server_address = ('', port) |
| 117 | + httpd = HTTPServer(server_address, HelloWorldHandler) |
| 118 | + print(f"Server running on port {port}") |
| 119 | + print(f"Visit http://localhost:{port} to see the Hello World page") |
| 120 | + print(f"API endpoints available:") |
| 121 | + print(f" GET /api/hello - JSON hello message") |
| 122 | + print(f" GET /health - Health check") |
| 123 | + print(f" POST /api/echo - Echo back posted data") |
| 124 | + |
| 125 | + try: |
| 126 | + httpd.serve_forever() |
| 127 | + except KeyboardInterrupt: |
| 128 | + print("\nShutting down server...") |
| 129 | + httpd.shutdown() |
| 130 | + |
| 131 | +if __name__ == '__main__': |
| 132 | + port = int(os.environ.get('PORT', 8000)) |
| 133 | + run_server(port) |
0 commit comments