38 lines
1.0 KiB
Python
38 lines
1.0 KiB
Python
|
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||
|
import threading
|
||
|
|
||
|
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
|
||
|
def do_GET(self):
|
||
|
self.send_response(200)
|
||
|
self.send_header('Content-type', 'text/plain')
|
||
|
self.end_headers()
|
||
|
self.wfile.write(b'Hello, World!')
|
||
|
|
||
|
class HTTPService:
|
||
|
def __init__(self, host='localhost', port=8000):
|
||
|
self.host = host
|
||
|
self.port = port
|
||
|
self.server = None
|
||
|
self.thread = None
|
||
|
|
||
|
def start(self):
|
||
|
self.server = HTTPServer((self.host, self.port), SimpleHTTPRequestHandler)
|
||
|
self.thread = threading.Thread(target=self.server.serve_forever)
|
||
|
self.thread.start()
|
||
|
print(f"HTTP service started on http://{self.host}:{self.port}")
|
||
|
|
||
|
def stop(self):
|
||
|
if self.server:
|
||
|
self.server.shutdown()
|
||
|
self.server.server_close()
|
||
|
self.thread.join()
|
||
|
print("HTTP service stopped")
|
||
|
|
||
|
http_service = HTTPService()
|
||
|
|
||
|
def initialize():
|
||
|
http_service.start()
|
||
|
|
||
|
def shutdown():
|
||
|
http_service.stop()
|