serving.py 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. """A WSGI and HTTP server for use **during development only**. This
  2. server is convenient to use, but is not designed to be particularly
  3. stable, secure, or efficient. Use a dedicate WSGI server and HTTP
  4. server when deploying to production.
  5. It provides features like interactive debugging and code reloading. Use
  6. ``run_simple`` to start the server. Put this in a ``run.py`` script:
  7. .. code-block:: python
  8. from myapp import create_app
  9. from werkzeug import run_simple
  10. """
  11. from __future__ import annotations
  12. import errno
  13. import io
  14. import os
  15. import selectors
  16. import socket
  17. import socketserver
  18. import sys
  19. import typing as t
  20. from datetime import datetime as dt
  21. from datetime import timedelta
  22. from datetime import timezone
  23. from http.server import BaseHTTPRequestHandler
  24. from http.server import HTTPServer
  25. from urllib.parse import unquote
  26. from urllib.parse import urlsplit
  27. from ._internal import _log
  28. from ._internal import _wsgi_encoding_dance
  29. from .exceptions import InternalServerError
  30. from .http import parse_set_header
  31. from .urls import uri_to_iri
  32. try:
  33. import ssl
  34. connection_dropped_errors: tuple[type[Exception], ...] = (
  35. ConnectionError,
  36. socket.timeout,
  37. ssl.SSLEOFError,
  38. )
  39. except ImportError:
  40. class _SslDummy:
  41. def __getattr__(self, name: str) -> t.Any:
  42. raise RuntimeError( # noqa: B904
  43. "SSL is unavailable because this Python runtime was not"
  44. " compiled with SSL/TLS support."
  45. )
  46. ssl = _SslDummy() # type: ignore
  47. connection_dropped_errors = (ConnectionError, socket.timeout)
  48. _log_add_style = True
  49. if os.name == "nt":
  50. try:
  51. __import__("colorama")
  52. except ImportError:
  53. _log_add_style = False
  54. can_fork = hasattr(os, "fork")
  55. if can_fork:
  56. ForkingMixIn = socketserver.ForkingMixIn
  57. else:
  58. class ForkingMixIn: # type: ignore
  59. pass
  60. try:
  61. af_unix = socket.AF_UNIX
  62. except AttributeError:
  63. af_unix = None # type: ignore
  64. LISTEN_QUEUE = 128
  65. _TSSLContextArg = t.Optional[
  66. t.Union["ssl.SSLContext", tuple[str, t.Optional[str]], t.Literal["adhoc"]]
  67. ]
  68. if t.TYPE_CHECKING:
  69. from _typeshed.wsgi import WSGIApplication
  70. from _typeshed.wsgi import WSGIEnvironment
  71. from cryptography.hazmat.primitives.asymmetric.rsa import (
  72. RSAPrivateKeyWithSerialization,
  73. )
  74. from cryptography.x509 import Certificate
  75. class DechunkedInput(io.RawIOBase):
  76. """An input stream that handles Transfer-Encoding 'chunked'"""
  77. def __init__(self, rfile: t.IO[bytes]) -> None:
  78. self._rfile = rfile
  79. self._done = False
  80. self._len = 0
  81. def readable(self) -> bool:
  82. return True
  83. def read_chunk_len(self) -> int:
  84. try:
  85. line = self._rfile.readline().decode("latin1")
  86. _len = int(line.strip(), 16)
  87. except ValueError as e:
  88. raise OSError("Invalid chunk header") from e
  89. if _len < 0:
  90. raise OSError("Negative chunk length not allowed")
  91. return _len
  92. def readinto(self, buf: bytearray) -> int: # type: ignore
  93. read = 0
  94. while not self._done and read < len(buf):
  95. if self._len == 0:
  96. # This is the first chunk or we fully consumed the previous
  97. # one. Read the next length of the next chunk
  98. self._len = self.read_chunk_len()
  99. if self._len == 0:
  100. # Found the final chunk of size 0. The stream is now exhausted,
  101. # but there is still a final newline that should be consumed
  102. self._done = True
  103. if self._len > 0:
  104. # There is data (left) in this chunk, so append it to the
  105. # buffer. If this operation fully consumes the chunk, this will
  106. # reset self._len to 0.
  107. n = min(len(buf), self._len)
  108. # If (read + chunk size) becomes more than len(buf), buf will
  109. # grow beyond the original size and read more data than
  110. # required. So only read as much data as can fit in buf.
  111. if read + n > len(buf):
  112. buf[read:] = self._rfile.read(len(buf) - read)
  113. self._len -= len(buf) - read
  114. read = len(buf)
  115. else:
  116. buf[read : read + n] = self._rfile.read(n)
  117. self._len -= n
  118. read += n
  119. if self._len == 0:
  120. # Skip the terminating newline of a chunk that has been fully
  121. # consumed. This also applies to the 0-sized final chunk
  122. terminator = self._rfile.readline()
  123. if terminator not in (b"\n", b"\r\n", b"\r"):
  124. raise OSError("Missing chunk terminating newline")
  125. return read
  126. class WSGIRequestHandler(BaseHTTPRequestHandler):
  127. """A request handler that implements WSGI dispatching."""
  128. server: BaseWSGIServer
  129. @property
  130. def server_version(self) -> str: # type: ignore
  131. return self.server._server_version
  132. def make_environ(self) -> WSGIEnvironment:
  133. request_url = urlsplit(self.path)
  134. url_scheme = "http" if self.server.ssl_context is None else "https"
  135. if not self.client_address:
  136. self.client_address = ("<local>", 0)
  137. elif isinstance(self.client_address, str):
  138. self.client_address = (self.client_address, 0)
  139. # If there was no scheme but the path started with two slashes,
  140. # the first segment may have been incorrectly parsed as the
  141. # netloc, prepend it to the path again.
  142. if not request_url.scheme and request_url.netloc:
  143. path_info = f"/{request_url.netloc}{request_url.path}"
  144. else:
  145. path_info = request_url.path
  146. path_info = unquote(path_info)
  147. environ: WSGIEnvironment = {
  148. "wsgi.version": (1, 0),
  149. "wsgi.url_scheme": url_scheme,
  150. "wsgi.input": self.rfile,
  151. "wsgi.errors": sys.stderr,
  152. "wsgi.multithread": self.server.multithread,
  153. "wsgi.multiprocess": self.server.multiprocess,
  154. "wsgi.run_once": False,
  155. "werkzeug.socket": self.connection,
  156. "SERVER_SOFTWARE": self.server_version,
  157. "REQUEST_METHOD": self.command,
  158. "SCRIPT_NAME": "",
  159. "PATH_INFO": _wsgi_encoding_dance(path_info),
  160. "QUERY_STRING": _wsgi_encoding_dance(request_url.query),
  161. # Non-standard, added by mod_wsgi, uWSGI
  162. "REQUEST_URI": _wsgi_encoding_dance(self.path),
  163. # Non-standard, added by gunicorn
  164. "RAW_URI": _wsgi_encoding_dance(self.path),
  165. "REMOTE_ADDR": self.address_string(),
  166. "REMOTE_PORT": self.port_integer(),
  167. "SERVER_NAME": self.server.server_address[0],
  168. "SERVER_PORT": str(self.server.server_address[1]),
  169. "SERVER_PROTOCOL": self.request_version,
  170. }
  171. for key, value in self.headers.items():
  172. if "_" in key:
  173. continue
  174. key = key.upper().replace("-", "_")
  175. value = value.replace("\r\n", "")
  176. if key not in ("CONTENT_TYPE", "CONTENT_LENGTH"):
  177. key = f"HTTP_{key}"
  178. if key in environ:
  179. value = f"{environ[key]},{value}"
  180. environ[key] = value
  181. if "chunked" in parse_set_header(environ.get("HTTP_TRANSFER_ENCODING")):
  182. environ["wsgi.input_terminated"] = True
  183. environ["wsgi.input"] = DechunkedInput(environ["wsgi.input"])
  184. # Per RFC 2616, if the URL is absolute, use that as the host.
  185. # We're using "has a scheme" to indicate an absolute URL.
  186. if request_url.scheme and request_url.netloc:
  187. environ["HTTP_HOST"] = request_url.netloc
  188. try:
  189. # binary_form=False gives nicer information, but wouldn't be compatible with
  190. # what Nginx or Apache could return.
  191. peer_cert = self.connection.getpeercert(binary_form=True)
  192. if peer_cert is not None:
  193. # Nginx and Apache use PEM format.
  194. environ["SSL_CLIENT_CERT"] = ssl.DER_cert_to_PEM_cert(peer_cert)
  195. except ValueError:
  196. # SSL handshake hasn't finished.
  197. self.server.log("error", "Cannot fetch SSL peer certificate info")
  198. except AttributeError:
  199. # Not using TLS, the socket will not have getpeercert().
  200. pass
  201. return environ
  202. def run_wsgi(self) -> None:
  203. if self.headers.get("Expect", "").lower().strip() == "100-continue":
  204. self.wfile.write(b"HTTP/1.1 100 Continue\r\n\r\n")
  205. self.environ = environ = self.make_environ()
  206. status_set: str | None = None
  207. headers_set: list[tuple[str, str]] | None = None
  208. status_sent: str | None = None
  209. headers_sent: list[tuple[str, str]] | None = None
  210. chunk_response: bool = False
  211. def write(data: bytes) -> None:
  212. nonlocal status_sent, headers_sent, chunk_response
  213. assert status_set is not None, "write() before start_response"
  214. assert headers_set is not None, "write() before start_response"
  215. if status_sent is None:
  216. status_sent = status_set
  217. headers_sent = headers_set
  218. try:
  219. code_str, msg = status_sent.split(None, 1)
  220. except ValueError:
  221. code_str, msg = status_sent, ""
  222. code = int(code_str)
  223. self.send_response(code, msg)
  224. header_keys = set()
  225. for key, value in headers_sent:
  226. self.send_header(key, value)
  227. header_keys.add(key.lower())
  228. # Use chunked transfer encoding if there is no content
  229. # length. Do not use for 1xx and 204 responses. 304
  230. # responses and HEAD requests are also excluded, which
  231. # is the more conservative behavior and matches other
  232. # parts of the code.
  233. # https://httpwg.org/specs/rfc7230.html#rfc.section.3.3.1
  234. if (
  235. not (
  236. "content-length" in header_keys
  237. or environ["REQUEST_METHOD"] == "HEAD"
  238. or (100 <= code < 200)
  239. or code in {204, 304}
  240. )
  241. and self.protocol_version >= "HTTP/1.1"
  242. ):
  243. chunk_response = True
  244. self.send_header("Transfer-Encoding", "chunked")
  245. # Always close the connection. This disables HTTP/1.1
  246. # keep-alive connections. They aren't handled well by
  247. # Python's http.server because it doesn't know how to
  248. # drain the stream before the next request line.
  249. self.send_header("Connection", "close")
  250. self.end_headers()
  251. assert isinstance(data, bytes), "applications must write bytes"
  252. if data:
  253. if chunk_response:
  254. self.wfile.write(hex(len(data))[2:].encode())
  255. self.wfile.write(b"\r\n")
  256. self.wfile.write(data)
  257. if chunk_response:
  258. self.wfile.write(b"\r\n")
  259. self.wfile.flush()
  260. def start_response(status, headers, exc_info=None): # type: ignore
  261. nonlocal status_set, headers_set
  262. if exc_info:
  263. try:
  264. if headers_sent:
  265. raise exc_info[1].with_traceback(exc_info[2])
  266. finally:
  267. exc_info = None
  268. elif headers_set:
  269. raise AssertionError("Headers already set")
  270. status_set = status
  271. headers_set = headers
  272. return write
  273. def execute(app: WSGIApplication) -> None:
  274. application_iter = app(environ, start_response)
  275. try:
  276. for data in application_iter:
  277. write(data)
  278. if not headers_sent:
  279. write(b"")
  280. if chunk_response:
  281. self.wfile.write(b"0\r\n\r\n")
  282. finally:
  283. # Check for any remaining data in the read socket, and discard it. This
  284. # will read past request.max_content_length, but lets the client see a
  285. # 413 response instead of a connection reset failure. If we supported
  286. # keep-alive connections, this naive approach would break by reading the
  287. # next request line. Since we know that write (above) closes every
  288. # connection we can read everything.
  289. selector = selectors.DefaultSelector()
  290. selector.register(self.connection, selectors.EVENT_READ)
  291. total_size = 0
  292. total_reads = 0
  293. # A timeout of 0 tends to fail because a client needs a small amount of
  294. # time to continue sending its data.
  295. while selector.select(timeout=0.01):
  296. # Only read 10MB into memory at a time.
  297. data = self.rfile.read(10_000_000)
  298. total_size += len(data)
  299. total_reads += 1
  300. # Stop reading on no data, >=10GB, or 1000 reads. If a client sends
  301. # more than that, they'll get a connection reset failure.
  302. if not data or total_size >= 10_000_000_000 or total_reads > 1000:
  303. break
  304. selector.close()
  305. if hasattr(application_iter, "close"):
  306. application_iter.close()
  307. try:
  308. execute(self.server.app)
  309. except connection_dropped_errors as e:
  310. self.connection_dropped(e, environ)
  311. except Exception as e:
  312. if self.server.passthrough_errors:
  313. raise
  314. if status_sent is not None and chunk_response:
  315. self.close_connection = True
  316. try:
  317. # if we haven't yet sent the headers but they are set
  318. # we roll back to be able to set them again.
  319. if status_sent is None:
  320. status_set = None
  321. headers_set = None
  322. execute(InternalServerError())
  323. except Exception:
  324. pass
  325. from .debug.tbtools import DebugTraceback
  326. msg = DebugTraceback(e).render_traceback_text()
  327. self.server.log("error", f"Error on request:\n{msg}")
  328. def handle(self) -> None:
  329. """Handles a request ignoring dropped connections."""
  330. try:
  331. super().handle()
  332. except (ConnectionError, socket.timeout) as e:
  333. self.connection_dropped(e)
  334. except Exception as e:
  335. if self.server.ssl_context is not None and is_ssl_error(e):
  336. self.log_error("SSL error occurred: %s", e)
  337. else:
  338. raise
  339. def connection_dropped(
  340. self, error: BaseException, environ: WSGIEnvironment | None = None
  341. ) -> None:
  342. """Called if the connection was closed by the client. By default
  343. nothing happens.
  344. """
  345. def __getattr__(self, name: str) -> t.Any:
  346. # All HTTP methods are handled by run_wsgi.
  347. if name.startswith("do_"):
  348. return self.run_wsgi
  349. # All other attributes are forwarded to the base class.
  350. return getattr(super(), name)
  351. def address_string(self) -> str:
  352. if getattr(self, "environ", None):
  353. return self.environ["REMOTE_ADDR"] # type: ignore
  354. if not self.client_address:
  355. return "<local>"
  356. return self.client_address[0]
  357. def port_integer(self) -> int:
  358. return self.client_address[1]
  359. # Escape control characters. This is defined (but private) in Python 3.12.
  360. _control_char_table = str.maketrans(
  361. {c: rf"\x{c:02x}" for c in [*range(0x20), *range(0x7F, 0xA0)]}
  362. )
  363. _control_char_table[ord("\\")] = r"\\"
  364. def log_request(self, code: int | str = "-", size: int | str = "-") -> None:
  365. try:
  366. path = uri_to_iri(self.path)
  367. msg = f"{self.command} {path} {self.request_version}"
  368. except AttributeError:
  369. # path isn't set if the requestline was bad
  370. msg = self.requestline
  371. # Escape control characters that may be in the decoded path.
  372. msg = msg.translate(self._control_char_table)
  373. code = str(code)
  374. if code[0] == "1": # 1xx - Informational
  375. msg = _ansi_style(msg, "bold")
  376. elif code == "200": # 2xx - Success
  377. pass
  378. elif code == "304": # 304 - Resource Not Modified
  379. msg = _ansi_style(msg, "cyan")
  380. elif code[0] == "3": # 3xx - Redirection
  381. msg = _ansi_style(msg, "green")
  382. elif code == "404": # 404 - Resource Not Found
  383. msg = _ansi_style(msg, "yellow")
  384. elif code[0] == "4": # 4xx - Client Error
  385. msg = _ansi_style(msg, "bold", "red")
  386. else: # 5xx, or any other response
  387. msg = _ansi_style(msg, "bold", "magenta")
  388. self.log("info", '"%s" %s %s', msg, code, size)
  389. def log_error(self, format: str, *args: t.Any) -> None:
  390. self.log("error", format, *args)
  391. def log_message(self, format: str, *args: t.Any) -> None:
  392. self.log("info", format, *args)
  393. def log(self, type: str, message: str, *args: t.Any) -> None:
  394. # an IPv6 scoped address contains "%" which breaks logging
  395. address_string = self.address_string().replace("%", "%%")
  396. _log(
  397. type,
  398. f"{address_string} - - [{self.log_date_time_string()}] {message}\n",
  399. *args,
  400. )
  401. def _ansi_style(value: str, *styles: str) -> str:
  402. if not _log_add_style:
  403. return value
  404. codes = {
  405. "bold": 1,
  406. "red": 31,
  407. "green": 32,
  408. "yellow": 33,
  409. "magenta": 35,
  410. "cyan": 36,
  411. }
  412. for style in styles:
  413. value = f"\x1b[{codes[style]}m{value}"
  414. return f"{value}\x1b[0m"
  415. def generate_adhoc_ssl_pair(
  416. cn: str | None = None,
  417. ) -> tuple[Certificate, RSAPrivateKeyWithSerialization]:
  418. try:
  419. from cryptography import x509
  420. from cryptography.hazmat.backends import default_backend
  421. from cryptography.hazmat.primitives import hashes
  422. from cryptography.hazmat.primitives.asymmetric import rsa
  423. from cryptography.x509.oid import NameOID
  424. except ImportError:
  425. raise TypeError(
  426. "Using ad-hoc certificates requires the cryptography library."
  427. ) from None
  428. backend = default_backend()
  429. pkey = rsa.generate_private_key(
  430. public_exponent=65537, key_size=2048, backend=backend
  431. )
  432. # pretty damn sure that this is not actually accepted by anyone
  433. if cn is None:
  434. cn = "*"
  435. subject = x509.Name(
  436. [
  437. x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Dummy Certificate"),
  438. x509.NameAttribute(NameOID.COMMON_NAME, cn),
  439. ]
  440. )
  441. backend = default_backend()
  442. cert = (
  443. x509.CertificateBuilder()
  444. .subject_name(subject)
  445. .issuer_name(subject)
  446. .public_key(pkey.public_key())
  447. .serial_number(x509.random_serial_number())
  448. .not_valid_before(dt.now(timezone.utc))
  449. .not_valid_after(dt.now(timezone.utc) + timedelta(days=365))
  450. .add_extension(x509.ExtendedKeyUsage([x509.OID_SERVER_AUTH]), critical=False)
  451. .add_extension(
  452. x509.SubjectAlternativeName([x509.DNSName(cn), x509.DNSName(f"*.{cn}")]),
  453. critical=False,
  454. )
  455. .sign(pkey, hashes.SHA256(), backend)
  456. )
  457. return cert, pkey
  458. def make_ssl_devcert(
  459. base_path: str, host: str | None = None, cn: str | None = None
  460. ) -> tuple[str, str]:
  461. """Creates an SSL key for development. This should be used instead of
  462. the ``'adhoc'`` key which generates a new cert on each server start.
  463. It accepts a path for where it should store the key and cert and
  464. either a host or CN. If a host is given it will use the CN
  465. ``*.host/CN=host``.
  466. For more information see :func:`run_simple`.
  467. .. versionadded:: 0.9
  468. :param base_path: the path to the certificate and key. The extension
  469. ``.crt`` is added for the certificate, ``.key`` is
  470. added for the key.
  471. :param host: the name of the host. This can be used as an alternative
  472. for the `cn`.
  473. :param cn: the `CN` to use.
  474. """
  475. if host is not None:
  476. cn = host
  477. cert, pkey = generate_adhoc_ssl_pair(cn=cn)
  478. from cryptography.hazmat.primitives import serialization
  479. cert_file = f"{base_path}.crt"
  480. pkey_file = f"{base_path}.key"
  481. with open(cert_file, "wb") as f:
  482. f.write(cert.public_bytes(serialization.Encoding.PEM))
  483. with open(pkey_file, "wb") as f:
  484. f.write(
  485. pkey.private_bytes(
  486. encoding=serialization.Encoding.PEM,
  487. format=serialization.PrivateFormat.TraditionalOpenSSL,
  488. encryption_algorithm=serialization.NoEncryption(),
  489. )
  490. )
  491. return cert_file, pkey_file
  492. def generate_adhoc_ssl_context() -> ssl.SSLContext:
  493. """Generates an adhoc SSL context for the development server."""
  494. import atexit
  495. import tempfile
  496. cert, pkey = generate_adhoc_ssl_pair()
  497. from cryptography.hazmat.primitives import serialization
  498. cert_handle, cert_file = tempfile.mkstemp()
  499. pkey_handle, pkey_file = tempfile.mkstemp()
  500. atexit.register(os.remove, pkey_file)
  501. atexit.register(os.remove, cert_file)
  502. os.write(cert_handle, cert.public_bytes(serialization.Encoding.PEM))
  503. os.write(
  504. pkey_handle,
  505. pkey.private_bytes(
  506. encoding=serialization.Encoding.PEM,
  507. format=serialization.PrivateFormat.TraditionalOpenSSL,
  508. encryption_algorithm=serialization.NoEncryption(),
  509. ),
  510. )
  511. os.close(cert_handle)
  512. os.close(pkey_handle)
  513. ctx = load_ssl_context(cert_file, pkey_file)
  514. return ctx
  515. def load_ssl_context(
  516. cert_file: str, pkey_file: str | None = None, protocol: int | None = None
  517. ) -> ssl.SSLContext:
  518. """Loads SSL context from cert/private key files and optional protocol.
  519. Many parameters are directly taken from the API of
  520. :py:class:`ssl.SSLContext`.
  521. :param cert_file: Path of the certificate to use.
  522. :param pkey_file: Path of the private key to use. If not given, the key
  523. will be obtained from the certificate file.
  524. :param protocol: A ``PROTOCOL`` constant from the :mod:`ssl` module.
  525. Defaults to :data:`ssl.PROTOCOL_TLS_SERVER`.
  526. """
  527. if protocol is None:
  528. protocol = ssl.PROTOCOL_TLS_SERVER
  529. ctx = ssl.SSLContext(protocol)
  530. ctx.load_cert_chain(cert_file, pkey_file)
  531. return ctx
  532. def is_ssl_error(error: Exception | None = None) -> bool:
  533. """Checks if the given error (or the current one) is an SSL error."""
  534. if error is None:
  535. error = t.cast(Exception, sys.exc_info()[1])
  536. return isinstance(error, ssl.SSLError)
  537. def select_address_family(host: str, port: int) -> socket.AddressFamily:
  538. """Return ``AF_INET4``, ``AF_INET6``, or ``AF_UNIX`` depending on
  539. the host and port."""
  540. if host.startswith("unix://"):
  541. return socket.AF_UNIX
  542. elif ":" in host and hasattr(socket, "AF_INET6"):
  543. return socket.AF_INET6
  544. return socket.AF_INET
  545. def get_sockaddr(
  546. host: str, port: int, family: socket.AddressFamily
  547. ) -> tuple[str, int] | str:
  548. """Return a fully qualified socket address that can be passed to
  549. :func:`socket.bind`."""
  550. if family == af_unix:
  551. # Absolute path avoids IDNA encoding error when path starts with dot.
  552. return os.path.abspath(host.partition("://")[2])
  553. try:
  554. res = socket.getaddrinfo(
  555. host, port, family, socket.SOCK_STREAM, socket.IPPROTO_TCP
  556. )
  557. except socket.gaierror:
  558. return host, port
  559. return res[0][4] # type: ignore
  560. def get_interface_ip(family: socket.AddressFamily) -> str:
  561. """Get the IP address of an external interface. Used when binding to
  562. 0.0.0.0 or ::1 to show a more useful URL.
  563. :meta private:
  564. """
  565. # arbitrary private address
  566. host = "fd31:f903:5ab5:1::1" if family == socket.AF_INET6 else "10.253.155.219"
  567. with socket.socket(family, socket.SOCK_DGRAM) as s:
  568. try:
  569. s.connect((host, 58162))
  570. except OSError:
  571. return "::1" if family == socket.AF_INET6 else "127.0.0.1"
  572. return s.getsockname()[0] # type: ignore
  573. class BaseWSGIServer(HTTPServer):
  574. """A WSGI server that that handles one request at a time.
  575. Use :func:`make_server` to create a server instance.
  576. """
  577. multithread = False
  578. multiprocess = False
  579. request_queue_size = LISTEN_QUEUE
  580. allow_reuse_address = True
  581. def __init__(
  582. self,
  583. host: str,
  584. port: int,
  585. app: WSGIApplication,
  586. handler: type[WSGIRequestHandler] | None = None,
  587. passthrough_errors: bool = False,
  588. ssl_context: _TSSLContextArg | None = None,
  589. fd: int | None = None,
  590. ) -> None:
  591. if handler is None:
  592. handler = WSGIRequestHandler
  593. # If the handler doesn't directly set a protocol version and
  594. # thread or process workers are used, then allow chunked
  595. # responses and keep-alive connections by enabling HTTP/1.1.
  596. if "protocol_version" not in vars(handler) and (
  597. self.multithread or self.multiprocess
  598. ):
  599. handler.protocol_version = "HTTP/1.1"
  600. self.host = host
  601. self.port = port
  602. self.app = app
  603. self.passthrough_errors = passthrough_errors
  604. self.address_family = address_family = select_address_family(host, port)
  605. server_address = get_sockaddr(host, int(port), address_family)
  606. # Remove a leftover Unix socket file from a previous run. Don't
  607. # remove a file that was set up by run_simple.
  608. if address_family == af_unix and fd is None:
  609. server_address = t.cast(str, server_address)
  610. if os.path.exists(server_address):
  611. os.unlink(server_address)
  612. # Bind and activate will be handled manually, it should only
  613. # happen if we're not using a socket that was already set up.
  614. super().__init__(
  615. server_address, # type: ignore[arg-type]
  616. handler,
  617. bind_and_activate=False,
  618. )
  619. if fd is None:
  620. # No existing socket descriptor, do bind_and_activate=True.
  621. try:
  622. self.server_bind()
  623. self.server_activate()
  624. except OSError as e:
  625. # Catch connection issues and show them without the traceback. Show
  626. # extra instructions for address not found, and for macOS.
  627. self.server_close()
  628. print(e.strerror, file=sys.stderr)
  629. if e.errno == errno.EADDRINUSE:
  630. print(
  631. f"Port {port} is in use by another program. Either identify and"
  632. " stop that program, or start the server with a different"
  633. " port.",
  634. file=sys.stderr,
  635. )
  636. if sys.platform == "darwin" and port == 5000:
  637. print(
  638. "On macOS, try searching for and disabling"
  639. " 'AirPlay Receiver' in System Settings.",
  640. file=sys.stderr,
  641. )
  642. sys.exit(1)
  643. except BaseException:
  644. self.server_close()
  645. raise
  646. else:
  647. # TCPServer automatically opens a socket even if bind_and_activate is False.
  648. # Close it to silence a ResourceWarning.
  649. self.server_close()
  650. # Use the passed in socket directly.
  651. self.socket = socket.fromfd(fd, address_family, socket.SOCK_STREAM)
  652. self.server_address = self.socket.getsockname()
  653. if address_family != af_unix:
  654. # If port was 0, this will record the bound port.
  655. self.port = self.server_address[1]
  656. if ssl_context is not None:
  657. if isinstance(ssl_context, tuple):
  658. ssl_context = load_ssl_context(*ssl_context)
  659. elif ssl_context == "adhoc":
  660. ssl_context = generate_adhoc_ssl_context()
  661. self.socket = ssl_context.wrap_socket(self.socket, server_side=True)
  662. self.ssl_context: ssl.SSLContext | None = ssl_context
  663. else:
  664. self.ssl_context = None
  665. import importlib.metadata
  666. self._server_version = f"Werkzeug/{importlib.metadata.version('werkzeug')}"
  667. def log(self, type: str, message: str, *args: t.Any) -> None:
  668. _log(type, message, *args)
  669. def serve_forever(self, poll_interval: float = 0.5) -> None:
  670. try:
  671. super().serve_forever(poll_interval=poll_interval)
  672. except KeyboardInterrupt:
  673. pass
  674. finally:
  675. self.server_close()
  676. def handle_error(
  677. self, request: t.Any, client_address: tuple[str, int] | str
  678. ) -> None:
  679. if self.passthrough_errors:
  680. raise
  681. return super().handle_error(request, client_address)
  682. def log_startup(self) -> None:
  683. """Show information about the address when starting the server."""
  684. dev_warning = (
  685. "WARNING: This is a development server. Do not use it in a production"
  686. " deployment. Use a production WSGI server instead."
  687. )
  688. dev_warning = _ansi_style(dev_warning, "bold", "red")
  689. messages = [dev_warning]
  690. if self.address_family == af_unix:
  691. messages.append(f" * Running on {self.host}")
  692. else:
  693. scheme = "http" if self.ssl_context is None else "https"
  694. display_hostname = self.host
  695. if self.host in {"0.0.0.0", "::"}:
  696. messages.append(f" * Running on all addresses ({self.host})")
  697. if self.host == "0.0.0.0":
  698. localhost = "127.0.0.1"
  699. display_hostname = get_interface_ip(socket.AF_INET)
  700. else:
  701. localhost = "[::1]"
  702. display_hostname = get_interface_ip(socket.AF_INET6)
  703. messages.append(f" * Running on {scheme}://{localhost}:{self.port}")
  704. if ":" in display_hostname:
  705. display_hostname = f"[{display_hostname}]"
  706. messages.append(f" * Running on {scheme}://{display_hostname}:{self.port}")
  707. _log("info", "\n".join(messages))
  708. class ThreadedWSGIServer(socketserver.ThreadingMixIn, BaseWSGIServer):
  709. """A WSGI server that handles concurrent requests in separate
  710. threads.
  711. Use :func:`make_server` to create a server instance.
  712. """
  713. multithread = True
  714. daemon_threads = True
  715. class ForkingWSGIServer(ForkingMixIn, BaseWSGIServer):
  716. """A WSGI server that handles concurrent requests in separate forked
  717. processes.
  718. Use :func:`make_server` to create a server instance.
  719. """
  720. multiprocess = True
  721. def __init__(
  722. self,
  723. host: str,
  724. port: int,
  725. app: WSGIApplication,
  726. processes: int = 40,
  727. handler: type[WSGIRequestHandler] | None = None,
  728. passthrough_errors: bool = False,
  729. ssl_context: _TSSLContextArg | None = None,
  730. fd: int | None = None,
  731. ) -> None:
  732. if not can_fork:
  733. raise ValueError("Your platform does not support forking.")
  734. super().__init__(host, port, app, handler, passthrough_errors, ssl_context, fd)
  735. self.max_children = processes
  736. def make_server(
  737. host: str,
  738. port: int,
  739. app: WSGIApplication,
  740. threaded: bool = False,
  741. processes: int = 1,
  742. request_handler: type[WSGIRequestHandler] | None = None,
  743. passthrough_errors: bool = False,
  744. ssl_context: _TSSLContextArg | None = None,
  745. fd: int | None = None,
  746. ) -> BaseWSGIServer:
  747. """Create an appropriate WSGI server instance based on the value of
  748. ``threaded`` and ``processes``.
  749. This is called from :func:`run_simple`, but can be used separately
  750. to have access to the server object, such as to run it in a separate
  751. thread.
  752. See :func:`run_simple` for parameter docs.
  753. """
  754. if threaded and processes > 1:
  755. raise ValueError("Cannot have a multi-thread and multi-process server.")
  756. if threaded:
  757. return ThreadedWSGIServer(
  758. host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd
  759. )
  760. if processes > 1:
  761. return ForkingWSGIServer(
  762. host,
  763. port,
  764. app,
  765. processes,
  766. request_handler,
  767. passthrough_errors,
  768. ssl_context,
  769. fd=fd,
  770. )
  771. return BaseWSGIServer(
  772. host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd
  773. )
  774. def is_running_from_reloader() -> bool:
  775. """Check if the server is running as a subprocess within the
  776. Werkzeug reloader.
  777. .. versionadded:: 0.10
  778. """
  779. return os.environ.get("WERKZEUG_RUN_MAIN") == "true"
  780. def run_simple(
  781. hostname: str,
  782. port: int,
  783. application: WSGIApplication,
  784. use_reloader: bool = False,
  785. use_debugger: bool = False,
  786. use_evalex: bool = True,
  787. extra_files: t.Iterable[str] | None = None,
  788. exclude_patterns: t.Iterable[str] | None = None,
  789. reloader_interval: int = 1,
  790. reloader_type: str = "auto",
  791. threaded: bool = False,
  792. processes: int = 1,
  793. request_handler: type[WSGIRequestHandler] | None = None,
  794. static_files: dict[str, str | tuple[str, str]] | None = None,
  795. passthrough_errors: bool = False,
  796. ssl_context: _TSSLContextArg | None = None,
  797. ) -> None:
  798. """Start a development server for a WSGI application. Various
  799. optional features can be enabled.
  800. .. warning::
  801. Do not use the development server when deploying to production.
  802. It is intended for use only during local development. It is not
  803. designed to be particularly efficient, stable, or secure.
  804. :param hostname: The host to bind to, for example ``'localhost'``.
  805. Can be a domain, IPv4 or IPv6 address, or file path starting
  806. with ``unix://`` for a Unix socket.
  807. :param port: The port to bind to, for example ``8080``. Using ``0``
  808. tells the OS to pick a random free port.
  809. :param application: The WSGI application to run.
  810. :param use_reloader: Use a reloader process to restart the server
  811. process when files are changed.
  812. :param use_debugger: Use Werkzeug's debugger, which will show
  813. formatted tracebacks on unhandled exceptions.
  814. :param use_evalex: Make the debugger interactive. A Python terminal
  815. can be opened for any frame in the traceback. Some protection is
  816. provided by requiring a PIN, but this should never be enabled
  817. on a publicly visible server.
  818. :param extra_files: The reloader will watch these files for changes
  819. in addition to Python modules. For example, watch a
  820. configuration file.
  821. :param exclude_patterns: The reloader will ignore changes to any
  822. files matching these :mod:`fnmatch` patterns. For example,
  823. ignore cache files.
  824. :param reloader_interval: How often the reloader tries to check for
  825. changes.
  826. :param reloader_type: The reloader to use. The ``'stat'`` reloader
  827. is built in, but may require significant CPU to watch files. The
  828. ``'watchdog'`` reloader is much more efficient but requires
  829. installing the ``watchdog`` package first.
  830. :param threaded: Handle concurrent requests using threads. Cannot be
  831. used with ``processes``.
  832. :param processes: Handle concurrent requests using up to this number
  833. of processes. Cannot be used with ``threaded``.
  834. :param request_handler: Use a different
  835. :class:`~BaseHTTPServer.BaseHTTPRequestHandler` subclass to
  836. handle requests.
  837. :param static_files: A dict mapping URL prefixes to directories to
  838. serve static files from using
  839. :class:`~werkzeug.middleware.SharedDataMiddleware`.
  840. :param passthrough_errors: Don't catch unhandled exceptions at the
  841. server level, let the server crash instead. If ``use_debugger``
  842. is enabled, the debugger will still catch such errors.
  843. :param ssl_context: Configure TLS to serve over HTTPS. Can be an
  844. :class:`ssl.SSLContext` object, a ``(cert_file, key_file)``
  845. tuple to create a typical context, or the string ``'adhoc'`` to
  846. generate a temporary self-signed certificate.
  847. .. versionchanged:: 2.1
  848. Instructions are shown for dealing with an "address already in
  849. use" error.
  850. .. versionchanged:: 2.1
  851. Running on ``0.0.0.0`` or ``::`` shows the loopback IP in
  852. addition to a real IP.
  853. .. versionchanged:: 2.1
  854. The command-line interface was removed.
  855. .. versionchanged:: 2.0
  856. Running on ``0.0.0.0`` or ``::`` shows a real IP address that
  857. was bound as well as a warning not to run the development server
  858. in production.
  859. .. versionchanged:: 2.0
  860. The ``exclude_patterns`` parameter was added.
  861. .. versionchanged:: 0.15
  862. Bind to a Unix socket by passing a ``hostname`` that starts with
  863. ``unix://``.
  864. .. versionchanged:: 0.10
  865. Improved the reloader and added support for changing the backend
  866. through the ``reloader_type`` parameter.
  867. .. versionchanged:: 0.9
  868. A command-line interface was added.
  869. .. versionchanged:: 0.8
  870. ``ssl_context`` can be a tuple of paths to the certificate and
  871. private key files.
  872. .. versionchanged:: 0.6
  873. The ``ssl_context`` parameter was added.
  874. .. versionchanged:: 0.5
  875. The ``static_files`` and ``passthrough_errors`` parameters were
  876. added.
  877. """
  878. if not isinstance(port, int):
  879. raise TypeError("port must be an integer")
  880. if static_files:
  881. from .middleware.shared_data import SharedDataMiddleware
  882. application = SharedDataMiddleware(application, static_files)
  883. if use_debugger:
  884. from .debug import DebuggedApplication
  885. application = DebuggedApplication(application, evalex=use_evalex)
  886. # Allow the specified hostname to use the debugger, in addition to
  887. # localhost domains.
  888. application.trusted_hosts.append(hostname)
  889. if not is_running_from_reloader():
  890. fd = None
  891. else:
  892. fd = int(os.environ["WERKZEUG_SERVER_FD"])
  893. srv = make_server(
  894. hostname,
  895. port,
  896. application,
  897. threaded,
  898. processes,
  899. request_handler,
  900. passthrough_errors,
  901. ssl_context,
  902. fd=fd,
  903. )
  904. srv.socket.set_inheritable(True)
  905. os.environ["WERKZEUG_SERVER_FD"] = str(srv.fileno())
  906. if not is_running_from_reloader():
  907. srv.log_startup()
  908. _log("info", _ansi_style("Press CTRL+C to quit", "yellow"))
  909. if use_reloader:
  910. from ._reloader import run_with_reloader
  911. try:
  912. run_with_reloader(
  913. srv.serve_forever,
  914. extra_files=extra_files,
  915. exclude_patterns=exclude_patterns,
  916. interval=reloader_interval,
  917. reloader_type=reloader_type,
  918. )
  919. finally:
  920. srv.server_close()
  921. else:
  922. srv.serve_forever()