A high-performance ASGI and WSGI web server implementation that provides comprehensive support for modern web protocols including HTTP/1, HTTP/2, WebSockets, and experimental HTTP/3
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Hypercorn's configuration system provides comprehensive control over all server behavior through the Config class. The configuration can be loaded from various sources including Python files, TOML files, dictionaries, or set programmatically.
The primary configuration class that manages all server settings including network binding, SSL/TLS configuration, worker settings, logging options, and protocol-specific parameters.
class Config:
"""
Main configuration class for Hypercorn server.
Contains all configuration options for server behavior, networking,
SSL/TLS, logging, and protocol settings.
"""
def __init__(self):
# Network binding
self.bind: list[str] # Server binding addresses
self.insecure_bind: list[str] # HTTP-only binding addresses
self.quic_bind: list[str] # HTTP/3 QUIC binding addresses
# SSL/TLS Configuration
self.certfile: str | None # SSL certificate file path
self.keyfile: str | None # SSL private key file path
self.keyfile_password: str | None # SSL private key password
self.ca_certs: str | None # CA certificate file path
self.ciphers: str # SSL cipher configuration
self.verify_mode: ssl.VerifyMode | None # SSL certificate verification mode
self.verify_flags: ssl.VerifyFlags | None # SSL certificate verification flags
# Worker Configuration
self.workers: int # Number of worker processes
self.worker_class: str # Worker class (asyncio, uvloop, trio)
self.backlog: int # Socket backlog size
self.graceful_timeout: int # Graceful shutdown timeout
self.shutdown_timeout: int # Hard shutdown timeout
# Request Handling
self.keep_alive_timeout: int # Keep-alive timeout seconds
self.max_requests: int # Max requests per worker
self.max_requests_jitter: int # Jitter for max_requests
self.h11_max_incomplete_size: int # HTTP/1.1 incomplete request max size
self.h2_max_concurrent_streams: int # HTTP/2 max concurrent streams
# Logging Configuration
self.accesslog: str | None # Access log file path
self.errorlog: str | None # Error log file path
self.logconfig: str | None # Logging configuration file
self.access_log_format: str # Access log format string
self.logger_class: str # Logger class to use
# Application Configuration
self.application_path: str # Path to ASGI/WSGI application
self.root_path: str # ASGI root path
self.server_names: list[str] # Valid server names
# Development/Debug
self.debug: bool # Enable debug mode
self.reload: bool # Enable auto-reload on file changes
self.use_reloader: bool # Use reloader process
# Process Management
self.pid_path: str | None # PID file path
self.user: int | None # User ID to run as
self.group: int | None # Group ID to run as
# Protocol Settings
self.alpn_protocols: list[str] # ALPN protocol list
self.alt_svc_headers: list[str] # Alt-Svc headers
# WSGI Configuration
self.wsgi_max_body_size: int # WSGI max request body size
@classmethod
def from_mapping(cls, mapping: dict) -> Config:
"""
Create Config instance from dictionary.
Args:
mapping: Dictionary containing configuration key-value pairs
Returns:
Configured Config instance
"""
@classmethod
def from_pyfile(cls, filename: str) -> Config:
"""
Create Config instance from Python configuration file.
Args:
filename: Path to Python file containing configuration variables
Returns:
Configured Config instance
"""
@classmethod
def from_toml(cls, filename: str) -> Config:
"""
Create Config instance from TOML configuration file.
Args:
filename: Path to TOML file containing configuration
Returns:
Configured Config instance
"""
@classmethod
def from_object(cls, instance: object | str) -> Config:
"""
Create Config instance from object, module, or module path.
Args:
instance: Object, module, or string module path containing configuration attributes
Returns:
Configured Config instance
"""
def create_ssl_context(self) -> ssl.SSLContext | None:
"""
Create SSL context from configuration.
Returns:
SSL context if SSL is enabled, None otherwise
"""
def create_sockets(self) -> Sockets:
"""
Create server sockets based on configuration.
Returns:
Sockets container with secure, insecure, and QUIC sockets
"""
def response_headers(self, protocol: str) -> list[tuple[bytes, bytes]]:
"""
Get default response headers for the given protocol.
Args:
protocol: Protocol version ("1.0", "1.1", "2", "3")
Returns:
List of default response headers as (name, value) byte tuples
"""
@property
def ssl_enabled(self) -> bool:
"""
Check if SSL/TLS is enabled.
Returns:
True if SSL certificate and key files are configured
"""Container class for managing different types of server sockets created from configuration.
@dataclass
class Sockets:
"""
Container for server sockets.
Holds the different socket types created from configuration:
secure sockets for HTTPS, insecure sockets for HTTP, and
QUIC sockets for HTTP/3.
"""
secure_sockets: list[socket.socket] # HTTPS sockets
insecure_sockets: list[socket.socket] # HTTP sockets
quic_sockets: list[socket.socket] # HTTP/3 QUIC socketsType definitions and constants used in configuration.
# Configuration value units
BYTES: int # Byte multiplier constant
OCTETS: int # Octet multiplier constant
SECONDS: int # Second multiplier constant
# Type aliases
FilePath = str # File path type alias
SocketKind = str # Socket type identifierExceptions raised during configuration and socket creation.
class SocketTypeError(Exception):
"""
Raised when an incorrect socket type is provided.
This exception occurs when socket configuration specifies
an invalid or unsupported socket type.
"""from hypercorn.config import Config
# Basic server configuration
config = Config()
config.bind = ["127.0.0.1:8000"]
config.workers = 1
config.worker_class = "asyncio"config = Config()
config.bind = ["0.0.0.0:443"]
config.certfile = "/path/to/cert.pem"
config.keyfile = "/path/to/key.pem"
config.ssl_enabled = Trueconfig = Config()
config.bind = ["0.0.0.0:8000"]
config.workers = 4
config.worker_class = "uvloop"
config.backlog = 2048
config.keep_alive_timeout = 5
config.graceful_timeout = 30
config.accesslog = "/var/log/hypercorn-access.log"
config.errorlog = "/var/log/hypercorn-error.log"# From TOML file
config = Config.from_toml("hypercorn.toml")
# From Python file
config = Config.from_pyfile("hypercorn_config.py")
# From dictionary
settings = {
"bind": ["0.0.0.0:8000"],
"workers": 2,
"certfile": "cert.pem"
}
config = Config.from_mapping(settings)Install with Tessl CLI
npx tessl i tessl/pypi-hypercorn