A basic socket-based HTTP server.
| 55 | |
| 56 | |
| 57 | class Server: |
| 58 | """A basic socket-based HTTP server.""" |
| 59 | |
| 60 | host: str |
| 61 | """Host name or IP address the server is listening on. ``None`` if server is stopped.""" |
| 62 | |
| 63 | port: int |
| 64 | """Port the server is listening on. ``None`` if server is stopped.""" |
| 65 | |
| 66 | root_path: str |
| 67 | """Root directory to serve files from. ``None`` if serving files is disabled.""" |
| 68 | |
| 69 | @staticmethod |
| 70 | def _validate_https_cert_provided( |
| 71 | certfile: Union[str, None], keyfile: Union[str, None] |
| 72 | ) -> None: |
| 73 | if certfile is None or keyfile is None: |
| 74 | raise ValueError("Both certfile and keyfile must be specified for HTTPS") |
| 75 | |
| 76 | @staticmethod |
| 77 | def _create_circuitpython_ssl_context(certfile: str, keyfile: str) -> SSLContext: |
| 78 | ssl_context = create_default_context() |
| 79 | |
| 80 | ssl_context.load_verify_locations(cadata="") |
| 81 | ssl_context.load_cert_chain(certfile, keyfile) |
| 82 | |
| 83 | return ssl_context |
| 84 | |
| 85 | @staticmethod |
| 86 | def _create_cpython_ssl_context(certfile: str, keyfile: str) -> SSLContext: |
| 87 | ssl_context = create_default_context(purpose=Purpose.CLIENT_AUTH) |
| 88 | |
| 89 | ssl_context.load_cert_chain(certfile, keyfile) |
| 90 | |
| 91 | ssl_context.verify_mode = CERT_NONE |
| 92 | ssl_context.check_hostname = False |
| 93 | |
| 94 | return ssl_context |
| 95 | |
| 96 | @classmethod |
| 97 | def _create_ssl_context(cls, certfile: str, keyfile: str) -> SSLContext: |
| 98 | return ( |
| 99 | cls._create_circuitpython_ssl_context(certfile, keyfile) |
| 100 | if implementation.name == "circuitpython" |
| 101 | else cls._create_cpython_ssl_context(certfile, keyfile) |
| 102 | ) |
| 103 | |
| 104 | def __init__( |
| 105 | self, |
| 106 | socket_source: _ISocketPool, |
| 107 | root_path: str = None, |
| 108 | *, |
| 109 | https: bool = False, |
| 110 | certfile: str = None, |
| 111 | keyfile: str = None, |
| 112 | debug: bool = False, |
| 113 | ) -> None: |
| 114 | """Create a server, and get it ready to run. |
no outgoing calls
no test coverage detected