File size: 17,234 Bytes
9c6594c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 |
from __future__ import annotations
import asyncio
import logging
import os
import platform
import ssl
import sys
import warnings
from configparser import RawConfigParser
from typing import IO, Any, Callable
import click
import uvicorn
from uvicorn._types import ASGIApplication
from uvicorn.config import (
HTTP_PROTOCOLS,
INTERFACES,
LIFESPAN,
LOG_LEVELS,
LOGGING_CONFIG,
LOOP_SETUPS,
SSL_PROTOCOL_VERSION,
WS_PROTOCOLS,
Config,
HTTPProtocolType,
InterfaceType,
LifespanType,
LoopSetupType,
WSProtocolType,
)
from uvicorn.server import Server
from uvicorn.supervisors import ChangeReload, Multiprocess
LEVEL_CHOICES = click.Choice(list(LOG_LEVELS.keys()))
HTTP_CHOICES = click.Choice(list(HTTP_PROTOCOLS.keys()))
WS_CHOICES = click.Choice(list(WS_PROTOCOLS.keys()))
LIFESPAN_CHOICES = click.Choice(list(LIFESPAN.keys()))
LOOP_CHOICES = click.Choice([key for key in LOOP_SETUPS.keys() if key != "none"])
INTERFACE_CHOICES = click.Choice(INTERFACES)
STARTUP_FAILURE = 3
logger = logging.getLogger("uvicorn.error")
def print_version(ctx: click.Context, param: click.Parameter, value: bool) -> None:
if not value or ctx.resilient_parsing:
return
click.echo(
"Running uvicorn {version} with {py_implementation} {py_version} on {system}".format( # noqa: UP032
version=uvicorn.__version__,
py_implementation=platform.python_implementation(),
py_version=platform.python_version(),
system=platform.system(),
)
)
ctx.exit()
@click.command(context_settings={"auto_envvar_prefix": "UVICORN"})
@click.argument("app", envvar="UVICORN_APP")
@click.option(
"--host",
type=str,
default="127.0.0.1",
help="Bind socket to this host.",
show_default=True,
)
@click.option(
"--port",
type=int,
default=8000,
help="Bind socket to this port. If 0, an available port will be picked.",
show_default=True,
)
@click.option("--uds", type=str, default=None, help="Bind to a UNIX domain socket.")
@click.option("--fd", type=int, default=None, help="Bind to socket from this file descriptor.")
@click.option("--reload", is_flag=True, default=False, help="Enable auto-reload.")
@click.option(
"--reload-dir",
"reload_dirs",
multiple=True,
help="Set reload directories explicitly, instead of using the current working directory.",
type=click.Path(exists=True),
)
@click.option(
"--reload-include",
"reload_includes",
multiple=True,
help="Set glob patterns to include while watching for files. Includes '*.py' "
"by default; these defaults can be overridden with `--reload-exclude`. "
"This option has no effect unless watchfiles is installed.",
)
@click.option(
"--reload-exclude",
"reload_excludes",
multiple=True,
help="Set glob patterns to exclude while watching for files. Includes "
"'.*, .py[cod], .sw.*, ~*' by default; these defaults can be overridden "
"with `--reload-include`. This option has no effect unless watchfiles is "
"installed.",
)
@click.option(
"--reload-delay",
type=float,
default=0.25,
show_default=True,
help="Delay between previous and next check if application needs to be. Defaults to 0.25s.",
)
@click.option(
"--workers",
default=None,
type=int,
help="Number of worker processes. Defaults to the $WEB_CONCURRENCY environment"
" variable if available, or 1. Not valid with --reload.",
)
@click.option(
"--loop",
type=LOOP_CHOICES,
default="auto",
help="Event loop implementation.",
show_default=True,
)
@click.option(
"--http",
type=HTTP_CHOICES,
default="auto",
help="HTTP protocol implementation.",
show_default=True,
)
@click.option(
"--ws",
type=WS_CHOICES,
default="auto",
help="WebSocket protocol implementation.",
show_default=True,
)
@click.option(
"--ws-max-size",
type=int,
default=16777216,
help="WebSocket max size message in bytes",
show_default=True,
)
@click.option(
"--ws-max-queue",
type=int,
default=32,
help="The maximum length of the WebSocket message queue.",
show_default=True,
)
@click.option(
"--ws-ping-interval",
type=float,
default=20.0,
help="WebSocket ping interval in seconds.",
show_default=True,
)
@click.option(
"--ws-ping-timeout",
type=float,
default=20.0,
help="WebSocket ping timeout in seconds.",
show_default=True,
)
@click.option(
"--ws-per-message-deflate",
type=bool,
default=True,
help="WebSocket per-message-deflate compression",
show_default=True,
)
@click.option(
"--lifespan",
type=LIFESPAN_CHOICES,
default="auto",
help="Lifespan implementation.",
show_default=True,
)
@click.option(
"--interface",
type=INTERFACE_CHOICES,
default="auto",
help="Select ASGI3, ASGI2, or WSGI as the application interface.",
show_default=True,
)
@click.option(
"--env-file",
type=click.Path(exists=True),
default=None,
help="Environment configuration file.",
show_default=True,
)
@click.option(
"--log-config",
type=click.Path(exists=True),
default=None,
help="Logging configuration file. Supported formats: .ini, .json, .yaml.",
show_default=True,
)
@click.option(
"--log-level",
type=LEVEL_CHOICES,
default=None,
help="Log level. [default: info]",
show_default=True,
)
@click.option(
"--access-log/--no-access-log",
is_flag=True,
default=True,
help="Enable/Disable access log.",
)
@click.option(
"--use-colors/--no-use-colors",
is_flag=True,
default=None,
help="Enable/Disable colorized logging.",
)
@click.option(
"--proxy-headers/--no-proxy-headers",
is_flag=True,
default=True,
help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info.",
)
@click.option(
"--server-header/--no-server-header",
is_flag=True,
default=True,
help="Enable/Disable default Server header.",
)
@click.option(
"--date-header/--no-date-header",
is_flag=True,
default=True,
help="Enable/Disable default Date header.",
)
@click.option(
"--forwarded-allow-ips",
type=str,
default=None,
help="Comma separated list of IP Addresses, IP Networks, or literals "
"(e.g. UNIX Socket path) to trust with proxy headers. Defaults to the "
"$FORWARDED_ALLOW_IPS environment variable if available, or '127.0.0.1'. "
"The literal '*' means trust everything.",
)
@click.option(
"--root-path",
type=str,
default="",
help="Set the ASGI 'root_path' for applications submounted below a given URL path.",
)
@click.option(
"--limit-concurrency",
type=int,
default=None,
help="Maximum number of concurrent connections or tasks to allow, before issuing HTTP 503 responses.",
)
@click.option(
"--backlog",
type=int,
default=2048,
help="Maximum number of connections to hold in backlog",
)
@click.option(
"--limit-max-requests",
type=int,
default=None,
help="Maximum number of requests to service before terminating the process.",
)
@click.option(
"--timeout-keep-alive",
type=int,
default=5,
help="Close Keep-Alive connections if no new data is received within this timeout.",
show_default=True,
)
@click.option(
"--timeout-graceful-shutdown",
type=int,
default=None,
help="Maximum number of seconds to wait for graceful shutdown.",
)
@click.option("--ssl-keyfile", type=str, default=None, help="SSL key file", show_default=True)
@click.option(
"--ssl-certfile",
type=str,
default=None,
help="SSL certificate file",
show_default=True,
)
@click.option(
"--ssl-keyfile-password",
type=str,
default=None,
help="SSL keyfile password",
show_default=True,
)
@click.option(
"--ssl-version",
type=int,
default=int(SSL_PROTOCOL_VERSION),
help="SSL version to use (see stdlib ssl module's)",
show_default=True,
)
@click.option(
"--ssl-cert-reqs",
type=int,
default=int(ssl.CERT_NONE),
help="Whether client certificate is required (see stdlib ssl module's)",
show_default=True,
)
@click.option(
"--ssl-ca-certs",
type=str,
default=None,
help="CA certificates file",
show_default=True,
)
@click.option(
"--ssl-ciphers",
type=str,
default="TLSv1",
help="Ciphers to use (see stdlib ssl module's)",
show_default=True,
)
@click.option(
"--header",
"headers",
multiple=True,
help="Specify custom default HTTP response headers as a Name:Value pair",
)
@click.option(
"--version",
is_flag=True,
callback=print_version,
expose_value=False,
is_eager=True,
help="Display the uvicorn version and exit.",
)
@click.option(
"--app-dir",
default="",
show_default=True,
help="Look for APP in the specified directory, by adding this to the PYTHONPATH."
" Defaults to the current working directory.",
)
@click.option(
"--h11-max-incomplete-event-size",
"h11_max_incomplete_event_size",
type=int,
default=None,
help="For h11, the maximum number of bytes to buffer of an incomplete event.",
)
@click.option(
"--factory",
is_flag=True,
default=False,
help="Treat APP as an application factory, i.e. a () -> <ASGI app> callable.",
show_default=True,
)
def main(
app: str,
host: str,
port: int,
uds: str,
fd: int,
loop: LoopSetupType,
http: HTTPProtocolType,
ws: WSProtocolType,
ws_max_size: int,
ws_max_queue: int,
ws_ping_interval: float,
ws_ping_timeout: float,
ws_per_message_deflate: bool,
lifespan: LifespanType,
interface: InterfaceType,
reload: bool,
reload_dirs: list[str],
reload_includes: list[str],
reload_excludes: list[str],
reload_delay: float,
workers: int,
env_file: str,
log_config: str,
log_level: str,
access_log: bool,
proxy_headers: bool,
server_header: bool,
date_header: bool,
forwarded_allow_ips: str,
root_path: str,
limit_concurrency: int,
backlog: int,
limit_max_requests: int,
timeout_keep_alive: int,
timeout_graceful_shutdown: int | None,
ssl_keyfile: str,
ssl_certfile: str,
ssl_keyfile_password: str,
ssl_version: int,
ssl_cert_reqs: int,
ssl_ca_certs: str,
ssl_ciphers: str,
headers: list[str],
use_colors: bool,
app_dir: str,
h11_max_incomplete_event_size: int | None,
factory: bool,
) -> None:
run(
app,
host=host,
port=port,
uds=uds,
fd=fd,
loop=loop,
http=http,
ws=ws,
ws_max_size=ws_max_size,
ws_max_queue=ws_max_queue,
ws_ping_interval=ws_ping_interval,
ws_ping_timeout=ws_ping_timeout,
ws_per_message_deflate=ws_per_message_deflate,
lifespan=lifespan,
env_file=env_file,
log_config=LOGGING_CONFIG if log_config is None else log_config,
log_level=log_level,
access_log=access_log,
interface=interface,
reload=reload,
reload_dirs=reload_dirs or None,
reload_includes=reload_includes or None,
reload_excludes=reload_excludes or None,
reload_delay=reload_delay,
workers=workers,
proxy_headers=proxy_headers,
server_header=server_header,
date_header=date_header,
forwarded_allow_ips=forwarded_allow_ips,
root_path=root_path,
limit_concurrency=limit_concurrency,
backlog=backlog,
limit_max_requests=limit_max_requests,
timeout_keep_alive=timeout_keep_alive,
timeout_graceful_shutdown=timeout_graceful_shutdown,
ssl_keyfile=ssl_keyfile,
ssl_certfile=ssl_certfile,
ssl_keyfile_password=ssl_keyfile_password,
ssl_version=ssl_version,
ssl_cert_reqs=ssl_cert_reqs,
ssl_ca_certs=ssl_ca_certs,
ssl_ciphers=ssl_ciphers,
headers=[header.split(":", 1) for header in headers], # type: ignore[misc]
use_colors=use_colors,
factory=factory,
app_dir=app_dir,
h11_max_incomplete_event_size=h11_max_incomplete_event_size,
)
def run(
app: ASGIApplication | Callable[..., Any] | str,
*,
host: str = "127.0.0.1",
port: int = 8000,
uds: str | None = None,
fd: int | None = None,
loop: LoopSetupType = "auto",
http: type[asyncio.Protocol] | HTTPProtocolType = "auto",
ws: type[asyncio.Protocol] | WSProtocolType = "auto",
ws_max_size: int = 16777216,
ws_max_queue: int = 32,
ws_ping_interval: float | None = 20.0,
ws_ping_timeout: float | None = 20.0,
ws_per_message_deflate: bool = True,
lifespan: LifespanType = "auto",
interface: InterfaceType = "auto",
reload: bool = False,
reload_dirs: list[str] | str | None = None,
reload_includes: list[str] | str | None = None,
reload_excludes: list[str] | str | None = None,
reload_delay: float = 0.25,
workers: int | None = None,
env_file: str | os.PathLike[str] | None = None,
log_config: dict[str, Any] | str | RawConfigParser | IO[Any] | None = LOGGING_CONFIG,
log_level: str | int | None = None,
access_log: bool = True,
proxy_headers: bool = True,
server_header: bool = True,
date_header: bool = True,
forwarded_allow_ips: list[str] | str | None = None,
root_path: str = "",
limit_concurrency: int | None = None,
backlog: int = 2048,
limit_max_requests: int | None = None,
timeout_keep_alive: int = 5,
timeout_graceful_shutdown: int | None = None,
ssl_keyfile: str | os.PathLike[str] | None = None,
ssl_certfile: str | os.PathLike[str] | None = None,
ssl_keyfile_password: str | None = None,
ssl_version: int = SSL_PROTOCOL_VERSION,
ssl_cert_reqs: int = ssl.CERT_NONE,
ssl_ca_certs: str | None = None,
ssl_ciphers: str = "TLSv1",
headers: list[tuple[str, str]] | None = None,
use_colors: bool | None = None,
app_dir: str | None = None,
factory: bool = False,
h11_max_incomplete_event_size: int | None = None,
) -> None:
if app_dir is not None:
sys.path.insert(0, app_dir)
config = Config(
app,
host=host,
port=port,
uds=uds,
fd=fd,
loop=loop,
http=http,
ws=ws,
ws_max_size=ws_max_size,
ws_max_queue=ws_max_queue,
ws_ping_interval=ws_ping_interval,
ws_ping_timeout=ws_ping_timeout,
ws_per_message_deflate=ws_per_message_deflate,
lifespan=lifespan,
interface=interface,
reload=reload,
reload_dirs=reload_dirs,
reload_includes=reload_includes,
reload_excludes=reload_excludes,
reload_delay=reload_delay,
workers=workers,
env_file=env_file,
log_config=log_config,
log_level=log_level,
access_log=access_log,
proxy_headers=proxy_headers,
server_header=server_header,
date_header=date_header,
forwarded_allow_ips=forwarded_allow_ips,
root_path=root_path,
limit_concurrency=limit_concurrency,
backlog=backlog,
limit_max_requests=limit_max_requests,
timeout_keep_alive=timeout_keep_alive,
timeout_graceful_shutdown=timeout_graceful_shutdown,
ssl_keyfile=ssl_keyfile,
ssl_certfile=ssl_certfile,
ssl_keyfile_password=ssl_keyfile_password,
ssl_version=ssl_version,
ssl_cert_reqs=ssl_cert_reqs,
ssl_ca_certs=ssl_ca_certs,
ssl_ciphers=ssl_ciphers,
headers=headers,
use_colors=use_colors,
factory=factory,
h11_max_incomplete_event_size=h11_max_incomplete_event_size,
)
server = Server(config=config)
if (config.reload or config.workers > 1) and not isinstance(app, str):
logger = logging.getLogger("uvicorn.error")
logger.warning("You must pass the application as an import string to enable 'reload' or 'workers'.")
sys.exit(1)
try:
if config.should_reload:
sock = config.bind_socket()
ChangeReload(config, target=server.run, sockets=[sock]).run()
elif config.workers > 1:
sock = config.bind_socket()
Multiprocess(config, target=server.run, sockets=[sock]).run()
else:
server.run()
except KeyboardInterrupt:
pass # pragma: full coverage
finally:
if config.uds and os.path.exists(config.uds):
os.remove(config.uds) # pragma: py-win32
if not server.started and not config.should_reload and config.workers == 1:
sys.exit(STARTUP_FAILURE)
def __getattr__(name: str) -> Any:
if name == "ServerState":
warnings.warn(
"uvicorn.main.ServerState is deprecated, use uvicorn.server.ServerState instead.",
DeprecationWarning,
)
from uvicorn.server import ServerState
return ServerState
raise AttributeError(f"module {__name__} has no attribute {name}")
if __name__ == "__main__":
main() # pragma: no cover
|