serving.py 39 KB

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