serving.py 38 KB

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