web.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. import asyncio
  2. import logging
  3. import socket
  4. import sys
  5. from argparse import ArgumentParser
  6. from collections.abc import Iterable
  7. from importlib import import_module
  8. from typing import (
  9. Any as Any,
  10. Awaitable as Awaitable,
  11. Callable as Callable,
  12. Iterable as TypingIterable,
  13. List as List,
  14. Optional as Optional,
  15. Set as Set,
  16. Type as Type,
  17. Union as Union,
  18. cast as cast,
  19. )
  20. from .abc import AbstractAccessLogger
  21. from .helpers import all_tasks
  22. from .log import access_logger
  23. from .web_app import Application as Application, CleanupError as CleanupError
  24. from .web_exceptions import (
  25. HTTPAccepted as HTTPAccepted,
  26. HTTPBadGateway as HTTPBadGateway,
  27. HTTPBadRequest as HTTPBadRequest,
  28. HTTPClientError as HTTPClientError,
  29. HTTPConflict as HTTPConflict,
  30. HTTPCreated as HTTPCreated,
  31. HTTPError as HTTPError,
  32. HTTPException as HTTPException,
  33. HTTPExpectationFailed as HTTPExpectationFailed,
  34. HTTPFailedDependency as HTTPFailedDependency,
  35. HTTPForbidden as HTTPForbidden,
  36. HTTPFound as HTTPFound,
  37. HTTPGatewayTimeout as HTTPGatewayTimeout,
  38. HTTPGone as HTTPGone,
  39. HTTPInsufficientStorage as HTTPInsufficientStorage,
  40. HTTPInternalServerError as HTTPInternalServerError,
  41. HTTPLengthRequired as HTTPLengthRequired,
  42. HTTPMethodNotAllowed as HTTPMethodNotAllowed,
  43. HTTPMisdirectedRequest as HTTPMisdirectedRequest,
  44. HTTPMovedPermanently as HTTPMovedPermanently,
  45. HTTPMultipleChoices as HTTPMultipleChoices,
  46. HTTPNetworkAuthenticationRequired as HTTPNetworkAuthenticationRequired,
  47. HTTPNoContent as HTTPNoContent,
  48. HTTPNonAuthoritativeInformation as HTTPNonAuthoritativeInformation,
  49. HTTPNotAcceptable as HTTPNotAcceptable,
  50. HTTPNotExtended as HTTPNotExtended,
  51. HTTPNotFound as HTTPNotFound,
  52. HTTPNotImplemented as HTTPNotImplemented,
  53. HTTPNotModified as HTTPNotModified,
  54. HTTPOk as HTTPOk,
  55. HTTPPartialContent as HTTPPartialContent,
  56. HTTPPaymentRequired as HTTPPaymentRequired,
  57. HTTPPermanentRedirect as HTTPPermanentRedirect,
  58. HTTPPreconditionFailed as HTTPPreconditionFailed,
  59. HTTPPreconditionRequired as HTTPPreconditionRequired,
  60. HTTPProxyAuthenticationRequired as HTTPProxyAuthenticationRequired,
  61. HTTPRedirection as HTTPRedirection,
  62. HTTPRequestEntityTooLarge as HTTPRequestEntityTooLarge,
  63. HTTPRequestHeaderFieldsTooLarge as HTTPRequestHeaderFieldsTooLarge,
  64. HTTPRequestRangeNotSatisfiable as HTTPRequestRangeNotSatisfiable,
  65. HTTPRequestTimeout as HTTPRequestTimeout,
  66. HTTPRequestURITooLong as HTTPRequestURITooLong,
  67. HTTPResetContent as HTTPResetContent,
  68. HTTPSeeOther as HTTPSeeOther,
  69. HTTPServerError as HTTPServerError,
  70. HTTPServiceUnavailable as HTTPServiceUnavailable,
  71. HTTPSuccessful as HTTPSuccessful,
  72. HTTPTemporaryRedirect as HTTPTemporaryRedirect,
  73. HTTPTooManyRequests as HTTPTooManyRequests,
  74. HTTPUnauthorized as HTTPUnauthorized,
  75. HTTPUnavailableForLegalReasons as HTTPUnavailableForLegalReasons,
  76. HTTPUnprocessableEntity as HTTPUnprocessableEntity,
  77. HTTPUnsupportedMediaType as HTTPUnsupportedMediaType,
  78. HTTPUpgradeRequired as HTTPUpgradeRequired,
  79. HTTPUseProxy as HTTPUseProxy,
  80. HTTPVariantAlsoNegotiates as HTTPVariantAlsoNegotiates,
  81. HTTPVersionNotSupported as HTTPVersionNotSupported,
  82. )
  83. from .web_fileresponse import FileResponse as FileResponse
  84. from .web_log import AccessLogger
  85. from .web_middlewares import (
  86. middleware as middleware,
  87. normalize_path_middleware as normalize_path_middleware,
  88. )
  89. from .web_protocol import (
  90. PayloadAccessError as PayloadAccessError,
  91. RequestHandler as RequestHandler,
  92. RequestPayloadError as RequestPayloadError,
  93. )
  94. from .web_request import (
  95. BaseRequest as BaseRequest,
  96. FileField as FileField,
  97. Request as Request,
  98. )
  99. from .web_response import (
  100. ContentCoding as ContentCoding,
  101. Response as Response,
  102. StreamResponse as StreamResponse,
  103. json_response as json_response,
  104. )
  105. from .web_routedef import (
  106. AbstractRouteDef as AbstractRouteDef,
  107. RouteDef as RouteDef,
  108. RouteTableDef as RouteTableDef,
  109. StaticDef as StaticDef,
  110. delete as delete,
  111. get as get,
  112. head as head,
  113. options as options,
  114. patch as patch,
  115. post as post,
  116. put as put,
  117. route as route,
  118. static as static,
  119. view as view,
  120. )
  121. from .web_runner import (
  122. AppRunner as AppRunner,
  123. BaseRunner as BaseRunner,
  124. BaseSite as BaseSite,
  125. GracefulExit as GracefulExit,
  126. NamedPipeSite as NamedPipeSite,
  127. ServerRunner as ServerRunner,
  128. SockSite as SockSite,
  129. TCPSite as TCPSite,
  130. UnixSite as UnixSite,
  131. )
  132. from .web_server import Server as Server
  133. from .web_urldispatcher import (
  134. AbstractResource as AbstractResource,
  135. AbstractRoute as AbstractRoute,
  136. DynamicResource as DynamicResource,
  137. PlainResource as PlainResource,
  138. Resource as Resource,
  139. ResourceRoute as ResourceRoute,
  140. StaticResource as StaticResource,
  141. UrlDispatcher as UrlDispatcher,
  142. UrlMappingMatchInfo as UrlMappingMatchInfo,
  143. View as View,
  144. )
  145. from .web_ws import (
  146. WebSocketReady as WebSocketReady,
  147. WebSocketResponse as WebSocketResponse,
  148. WSMsgType as WSMsgType,
  149. )
  150. __all__ = (
  151. # web_app
  152. "Application",
  153. "CleanupError",
  154. # web_exceptions
  155. "HTTPAccepted",
  156. "HTTPBadGateway",
  157. "HTTPBadRequest",
  158. "HTTPClientError",
  159. "HTTPConflict",
  160. "HTTPCreated",
  161. "HTTPError",
  162. "HTTPException",
  163. "HTTPExpectationFailed",
  164. "HTTPFailedDependency",
  165. "HTTPForbidden",
  166. "HTTPFound",
  167. "HTTPGatewayTimeout",
  168. "HTTPGone",
  169. "HTTPInsufficientStorage",
  170. "HTTPInternalServerError",
  171. "HTTPLengthRequired",
  172. "HTTPMethodNotAllowed",
  173. "HTTPMisdirectedRequest",
  174. "HTTPMovedPermanently",
  175. "HTTPMultipleChoices",
  176. "HTTPNetworkAuthenticationRequired",
  177. "HTTPNoContent",
  178. "HTTPNonAuthoritativeInformation",
  179. "HTTPNotAcceptable",
  180. "HTTPNotExtended",
  181. "HTTPNotFound",
  182. "HTTPNotImplemented",
  183. "HTTPNotModified",
  184. "HTTPOk",
  185. "HTTPPartialContent",
  186. "HTTPPaymentRequired",
  187. "HTTPPermanentRedirect",
  188. "HTTPPreconditionFailed",
  189. "HTTPPreconditionRequired",
  190. "HTTPProxyAuthenticationRequired",
  191. "HTTPRedirection",
  192. "HTTPRequestEntityTooLarge",
  193. "HTTPRequestHeaderFieldsTooLarge",
  194. "HTTPRequestRangeNotSatisfiable",
  195. "HTTPRequestTimeout",
  196. "HTTPRequestURITooLong",
  197. "HTTPResetContent",
  198. "HTTPSeeOther",
  199. "HTTPServerError",
  200. "HTTPServiceUnavailable",
  201. "HTTPSuccessful",
  202. "HTTPTemporaryRedirect",
  203. "HTTPTooManyRequests",
  204. "HTTPUnauthorized",
  205. "HTTPUnavailableForLegalReasons",
  206. "HTTPUnprocessableEntity",
  207. "HTTPUnsupportedMediaType",
  208. "HTTPUpgradeRequired",
  209. "HTTPUseProxy",
  210. "HTTPVariantAlsoNegotiates",
  211. "HTTPVersionNotSupported",
  212. # web_fileresponse
  213. "FileResponse",
  214. # web_middlewares
  215. "middleware",
  216. "normalize_path_middleware",
  217. # web_protocol
  218. "PayloadAccessError",
  219. "RequestHandler",
  220. "RequestPayloadError",
  221. # web_request
  222. "BaseRequest",
  223. "FileField",
  224. "Request",
  225. # web_response
  226. "ContentCoding",
  227. "Response",
  228. "StreamResponse",
  229. "json_response",
  230. # web_routedef
  231. "AbstractRouteDef",
  232. "RouteDef",
  233. "RouteTableDef",
  234. "StaticDef",
  235. "delete",
  236. "get",
  237. "head",
  238. "options",
  239. "patch",
  240. "post",
  241. "put",
  242. "route",
  243. "static",
  244. "view",
  245. # web_runner
  246. "AppRunner",
  247. "BaseRunner",
  248. "BaseSite",
  249. "GracefulExit",
  250. "ServerRunner",
  251. "SockSite",
  252. "TCPSite",
  253. "UnixSite",
  254. "NamedPipeSite",
  255. # web_server
  256. "Server",
  257. # web_urldispatcher
  258. "AbstractResource",
  259. "AbstractRoute",
  260. "DynamicResource",
  261. "PlainResource",
  262. "Resource",
  263. "ResourceRoute",
  264. "StaticResource",
  265. "UrlDispatcher",
  266. "UrlMappingMatchInfo",
  267. "View",
  268. # web_ws
  269. "WebSocketReady",
  270. "WebSocketResponse",
  271. "WSMsgType",
  272. # web
  273. "run_app",
  274. )
  275. try:
  276. from ssl import SSLContext
  277. except ImportError: # pragma: no cover
  278. SSLContext = Any # type: ignore
  279. HostSequence = TypingIterable[str]
  280. async def _run_app(
  281. app: Union[Application, Awaitable[Application]],
  282. *,
  283. host: Optional[Union[str, HostSequence]] = None,
  284. port: Optional[int] = None,
  285. path: Optional[str] = None,
  286. sock: Optional[socket.socket] = None,
  287. shutdown_timeout: float = 60.0,
  288. ssl_context: Optional[SSLContext] = None,
  289. print: Callable[..., None] = print,
  290. backlog: int = 128,
  291. access_log_class: Type[AbstractAccessLogger] = AccessLogger,
  292. access_log_format: str = AccessLogger.LOG_FORMAT,
  293. access_log: Optional[logging.Logger] = access_logger,
  294. handle_signals: bool = True,
  295. reuse_address: Optional[bool] = None,
  296. reuse_port: Optional[bool] = None,
  297. ) -> None:
  298. # A internal functio to actually do all dirty job for application running
  299. if asyncio.iscoroutine(app):
  300. app = await app # type: ignore
  301. app = cast(Application, app)
  302. runner = AppRunner(
  303. app,
  304. handle_signals=handle_signals,
  305. access_log_class=access_log_class,
  306. access_log_format=access_log_format,
  307. access_log=access_log,
  308. )
  309. await runner.setup()
  310. sites = [] # type: List[BaseSite]
  311. try:
  312. if host is not None:
  313. if isinstance(host, (str, bytes, bytearray, memoryview)):
  314. sites.append(
  315. TCPSite(
  316. runner,
  317. host,
  318. port,
  319. shutdown_timeout=shutdown_timeout,
  320. ssl_context=ssl_context,
  321. backlog=backlog,
  322. reuse_address=reuse_address,
  323. reuse_port=reuse_port,
  324. )
  325. )
  326. else:
  327. for h in host:
  328. sites.append(
  329. TCPSite(
  330. runner,
  331. h,
  332. port,
  333. shutdown_timeout=shutdown_timeout,
  334. ssl_context=ssl_context,
  335. backlog=backlog,
  336. reuse_address=reuse_address,
  337. reuse_port=reuse_port,
  338. )
  339. )
  340. elif path is None and sock is None or port is not None:
  341. sites.append(
  342. TCPSite(
  343. runner,
  344. port=port,
  345. shutdown_timeout=shutdown_timeout,
  346. ssl_context=ssl_context,
  347. backlog=backlog,
  348. reuse_address=reuse_address,
  349. reuse_port=reuse_port,
  350. )
  351. )
  352. if path is not None:
  353. if isinstance(path, (str, bytes, bytearray, memoryview)):
  354. sites.append(
  355. UnixSite(
  356. runner,
  357. path,
  358. shutdown_timeout=shutdown_timeout,
  359. ssl_context=ssl_context,
  360. backlog=backlog,
  361. )
  362. )
  363. else:
  364. for p in path:
  365. sites.append(
  366. UnixSite(
  367. runner,
  368. p,
  369. shutdown_timeout=shutdown_timeout,
  370. ssl_context=ssl_context,
  371. backlog=backlog,
  372. )
  373. )
  374. if sock is not None:
  375. if not isinstance(sock, Iterable):
  376. sites.append(
  377. SockSite(
  378. runner,
  379. sock,
  380. shutdown_timeout=shutdown_timeout,
  381. ssl_context=ssl_context,
  382. backlog=backlog,
  383. )
  384. )
  385. else:
  386. for s in sock:
  387. sites.append(
  388. SockSite(
  389. runner,
  390. s,
  391. shutdown_timeout=shutdown_timeout,
  392. ssl_context=ssl_context,
  393. backlog=backlog,
  394. )
  395. )
  396. for site in sites:
  397. await site.start()
  398. if print: # pragma: no branch
  399. names = sorted(str(s.name) for s in runner.sites)
  400. print(
  401. "======== Running on {} ========\n"
  402. "(Press CTRL+C to quit)".format(", ".join(names))
  403. )
  404. # sleep forever by 1 hour intervals,
  405. # on Windows before Python 3.8 wake up every 1 second to handle
  406. # Ctrl+C smoothly
  407. if sys.platform == "win32" and sys.version_info < (3, 8):
  408. delay = 1
  409. else:
  410. delay = 3600
  411. while True:
  412. await asyncio.sleep(delay)
  413. finally:
  414. await runner.cleanup()
  415. def _cancel_tasks(
  416. to_cancel: Set["asyncio.Task[Any]"], loop: asyncio.AbstractEventLoop
  417. ) -> None:
  418. if not to_cancel:
  419. return
  420. for task in to_cancel:
  421. task.cancel()
  422. loop.run_until_complete(
  423. asyncio.gather(*to_cancel, loop=loop, return_exceptions=True)
  424. )
  425. for task in to_cancel:
  426. if task.cancelled():
  427. continue
  428. if task.exception() is not None:
  429. loop.call_exception_handler(
  430. {
  431. "message": "unhandled exception during asyncio.run() shutdown",
  432. "exception": task.exception(),
  433. "task": task,
  434. }
  435. )
  436. def run_app(
  437. app: Union[Application, Awaitable[Application]],
  438. *,
  439. host: Optional[Union[str, HostSequence]] = None,
  440. port: Optional[int] = None,
  441. path: Optional[str] = None,
  442. sock: Optional[socket.socket] = None,
  443. shutdown_timeout: float = 60.0,
  444. ssl_context: Optional[SSLContext] = None,
  445. print: Callable[..., None] = print,
  446. backlog: int = 128,
  447. access_log_class: Type[AbstractAccessLogger] = AccessLogger,
  448. access_log_format: str = AccessLogger.LOG_FORMAT,
  449. access_log: Optional[logging.Logger] = access_logger,
  450. handle_signals: bool = True,
  451. reuse_address: Optional[bool] = None,
  452. reuse_port: Optional[bool] = None,
  453. ) -> None:
  454. """Run an app locally"""
  455. loop = asyncio.get_event_loop()
  456. # Configure if and only if in debugging mode and using the default logger
  457. if loop.get_debug() and access_log and access_log.name == "aiohttp.access":
  458. if access_log.level == logging.NOTSET:
  459. access_log.setLevel(logging.DEBUG)
  460. if not access_log.hasHandlers():
  461. access_log.addHandler(logging.StreamHandler())
  462. try:
  463. main_task = loop.create_task(
  464. _run_app(
  465. app,
  466. host=host,
  467. port=port,
  468. path=path,
  469. sock=sock,
  470. shutdown_timeout=shutdown_timeout,
  471. ssl_context=ssl_context,
  472. print=print,
  473. backlog=backlog,
  474. access_log_class=access_log_class,
  475. access_log_format=access_log_format,
  476. access_log=access_log,
  477. handle_signals=handle_signals,
  478. reuse_address=reuse_address,
  479. reuse_port=reuse_port,
  480. )
  481. )
  482. loop.run_until_complete(main_task)
  483. except (GracefulExit, KeyboardInterrupt): # pragma: no cover
  484. pass
  485. finally:
  486. _cancel_tasks({main_task}, loop)
  487. _cancel_tasks(all_tasks(loop), loop)
  488. if sys.version_info >= (3, 6): # don't use PY_36 to pass mypy
  489. loop.run_until_complete(loop.shutdown_asyncgens())
  490. loop.close()
  491. def main(argv: List[str]) -> None:
  492. arg_parser = ArgumentParser(
  493. description="aiohttp.web Application server", prog="aiohttp.web"
  494. )
  495. arg_parser.add_argument(
  496. "entry_func",
  497. help=(
  498. "Callable returning the `aiohttp.web.Application` instance to "
  499. "run. Should be specified in the 'module:function' syntax."
  500. ),
  501. metavar="entry-func",
  502. )
  503. arg_parser.add_argument(
  504. "-H",
  505. "--hostname",
  506. help="TCP/IP hostname to serve on (default: %(default)r)",
  507. default="localhost",
  508. )
  509. arg_parser.add_argument(
  510. "-P",
  511. "--port",
  512. help="TCP/IP port to serve on (default: %(default)r)",
  513. type=int,
  514. default="8080",
  515. )
  516. arg_parser.add_argument(
  517. "-U",
  518. "--path",
  519. help="Unix file system path to serve on. Specifying a path will cause "
  520. "hostname and port arguments to be ignored.",
  521. )
  522. args, extra_argv = arg_parser.parse_known_args(argv)
  523. # Import logic
  524. mod_str, _, func_str = args.entry_func.partition(":")
  525. if not func_str or not mod_str:
  526. arg_parser.error("'entry-func' not in 'module:function' syntax")
  527. if mod_str.startswith("."):
  528. arg_parser.error("relative module names not supported")
  529. try:
  530. module = import_module(mod_str)
  531. except ImportError as ex:
  532. arg_parser.error(f"unable to import {mod_str}: {ex}")
  533. try:
  534. func = getattr(module, func_str)
  535. except AttributeError:
  536. arg_parser.error(f"module {mod_str!r} has no attribute {func_str!r}")
  537. # Compatibility logic
  538. if args.path is not None and not hasattr(socket, "AF_UNIX"):
  539. arg_parser.error(
  540. "file system paths not supported by your operating" " environment"
  541. )
  542. logging.basicConfig(level=logging.DEBUG)
  543. app = func(extra_argv)
  544. run_app(app, host=args.hostname, port=args.port, path=args.path)
  545. arg_parser.exit(message="Stopped\n")
  546. if __name__ == "__main__": # pragma: no branch
  547. main(sys.argv[1:]) # pragma: no cover