45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
import ssl
|
|
from multiprocessing import Process
|
|
from http import HTTPStatus
|
|
from http.server import HTTPServer
|
|
from http.server import BaseHTTPRequestHandler
|
|
|
|
|
|
class Handler(BaseHTTPRequestHandler):
|
|
def log_message(self, format, *args):
|
|
pass
|
|
|
|
def send_text(self, content: str):
|
|
self.wfile.write(content.encode("utf8"))
|
|
|
|
def do_GET(self):
|
|
if self.path == "/":
|
|
self.send_response(HTTPStatus.OK)
|
|
self.end_headers()
|
|
self.send_text("test")
|
|
|
|
|
|
def get_ssl_context(cert_file, key_file):
|
|
context = ssl.SSLContext(ssl.PROTOCOL_TLS)
|
|
context.load_cert_chain(cert_file, key_file)
|
|
context.set_ciphers("@SECLEVEL=1:ALL")
|
|
return context
|
|
|
|
|
|
def _start_http_server(port: int, use_tls: bool = False):
|
|
with HTTPServer(("127.0.0.1", port), Handler) as httpd:
|
|
if use_tls:
|
|
context = get_ssl_context("tests/cert.pem", "tests/key.pem")
|
|
httpd.socket = context.wrap_socket(httpd.socket, server_side=True)
|
|
httpd.serve_forever()
|
|
|
|
|
|
def create_http_server_process(port: int = 80):
|
|
server_process = Process(target=_start_http_server, kwargs={"port": port, "use_tls": False})
|
|
return server_process
|
|
|
|
|
|
def create_https_server_process(port: int = 443):
|
|
server_process = Process(target=_start_http_server, kwargs={"port": port, "use_tls": True})
|
|
return server_process
|