serving.py 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. import io
  12. import os
  13. import platform
  14. import signal
  15. import socket
  16. import socketserver
  17. import sys
  18. import typing as t
  19. import warnings
  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 ._internal import _log
  26. from ._internal import _wsgi_encoding_dance
  27. from .exceptions import InternalServerError
  28. from .urls import uri_to_iri
  29. from .urls import url_parse
  30. from .urls import url_unquote
  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. can_open_by_fd = not platform.system() == "Windows" and hasattr(socket, "fromfd")
  59. _TSSLContextArg = t.Optional[
  60. t.Union["ssl.SSLContext", t.Tuple[str, t.Optional[str]], "te.Literal['adhoc']"]
  61. ]
  62. if t.TYPE_CHECKING:
  63. import typing_extensions as te # noqa: F401
  64. from _typeshed.wsgi import WSGIApplication
  65. from _typeshed.wsgi import WSGIEnvironment
  66. from cryptography.hazmat.primitives.asymmetric.rsa import (
  67. RSAPrivateKeyWithSerialization,
  68. )
  69. from cryptography.x509 import Certificate
  70. class DechunkedInput(io.RawIOBase):
  71. """An input stream that handles Transfer-Encoding 'chunked'"""
  72. def __init__(self, rfile: t.IO[bytes]) -> None:
  73. self._rfile = rfile
  74. self._done = False
  75. self._len = 0
  76. def readable(self) -> bool:
  77. return True
  78. def read_chunk_len(self) -> int:
  79. try:
  80. line = self._rfile.readline().decode("latin1")
  81. _len = int(line.strip(), 16)
  82. except ValueError as e:
  83. raise OSError("Invalid chunk header") from e
  84. if _len < 0:
  85. raise OSError("Negative chunk length not allowed")
  86. return _len
  87. def readinto(self, buf: bytearray) -> int: # type: ignore
  88. read = 0
  89. while not self._done and read < len(buf):
  90. if self._len == 0:
  91. # This is the first chunk or we fully consumed the previous
  92. # one. Read the next length of the next chunk
  93. self._len = self.read_chunk_len()
  94. if self._len == 0:
  95. # Found the final chunk of size 0. The stream is now exhausted,
  96. # but there is still a final newline that should be consumed
  97. self._done = True
  98. if self._len > 0:
  99. # There is data (left) in this chunk, so append it to the
  100. # buffer. If this operation fully consumes the chunk, this will
  101. # reset self._len to 0.
  102. n = min(len(buf), self._len)
  103. # If (read + chunk size) becomes more than len(buf), buf will
  104. # grow beyond the original size and read more data than
  105. # required. So only read as much data as can fit in buf.
  106. if read + n > len(buf):
  107. buf[read:] = self._rfile.read(len(buf) - read)
  108. self._len -= len(buf) - read
  109. read = len(buf)
  110. else:
  111. buf[read : read + n] = self._rfile.read(n)
  112. self._len -= n
  113. read += n
  114. if self._len == 0:
  115. # Skip the terminating newline of a chunk that has been fully
  116. # consumed. This also applies to the 0-sized final chunk
  117. terminator = self._rfile.readline()
  118. if terminator not in (b"\n", b"\r\n", b"\r"):
  119. raise OSError("Missing chunk terminating newline")
  120. return read
  121. class WSGIRequestHandler(BaseHTTPRequestHandler):
  122. """A request handler that implements WSGI dispatching."""
  123. server: "BaseWSGIServer"
  124. @property
  125. def server_version(self) -> str: # type: ignore
  126. from . import __version__
  127. return f"Werkzeug/{__version__}"
  128. def make_environ(self) -> "WSGIEnvironment":
  129. request_url = url_parse(self.path)
  130. def shutdown_server() -> None:
  131. warnings.warn(
  132. "The 'environ['werkzeug.server.shutdown']' function is"
  133. " deprecated and will be removed in Werkzeug 2.1.",
  134. stacklevel=2,
  135. )
  136. self.server.shutdown_signal = True
  137. url_scheme = "http" if self.server.ssl_context is None else "https"
  138. if not self.client_address:
  139. self.client_address = ("<local>", 0)
  140. elif isinstance(self.client_address, str):
  141. self.client_address = (self.client_address, 0)
  142. # If there was no scheme but the path started with two slashes,
  143. # the first segment may have been incorrectly parsed as the
  144. # netloc, prepend it to the path again.
  145. if not request_url.scheme and request_url.netloc:
  146. path_info = f"/{request_url.netloc}{request_url.path}"
  147. else:
  148. path_info = request_url.path
  149. path_info = url_unquote(path_info)
  150. environ: "WSGIEnvironment" = {
  151. "wsgi.version": (1, 0),
  152. "wsgi.url_scheme": url_scheme,
  153. "wsgi.input": self.rfile,
  154. "wsgi.errors": sys.stderr,
  155. "wsgi.multithread": self.server.multithread,
  156. "wsgi.multiprocess": self.server.multiprocess,
  157. "wsgi.run_once": False,
  158. "werkzeug.server.shutdown": shutdown_server,
  159. "werkzeug.socket": self.connection,
  160. "SERVER_SOFTWARE": self.server_version,
  161. "REQUEST_METHOD": self.command,
  162. "SCRIPT_NAME": "",
  163. "PATH_INFO": _wsgi_encoding_dance(path_info),
  164. "QUERY_STRING": _wsgi_encoding_dance(request_url.query),
  165. # Non-standard, added by mod_wsgi, uWSGI
  166. "REQUEST_URI": _wsgi_encoding_dance(self.path),
  167. # Non-standard, added by gunicorn
  168. "RAW_URI": _wsgi_encoding_dance(self.path),
  169. "REMOTE_ADDR": self.address_string(),
  170. "REMOTE_PORT": self.port_integer(),
  171. "SERVER_NAME": self.server.server_address[0],
  172. "SERVER_PORT": str(self.server.server_address[1]),
  173. "SERVER_PROTOCOL": self.request_version,
  174. }
  175. for key, value in self.headers.items():
  176. key = key.upper().replace("-", "_")
  177. value = value.replace("\r\n", "")
  178. if key not in ("CONTENT_TYPE", "CONTENT_LENGTH"):
  179. key = f"HTTP_{key}"
  180. if key in environ:
  181. value = f"{environ[key]},{value}"
  182. environ[key] = value
  183. if environ.get("HTTP_TRANSFER_ENCODING", "").strip().lower() == "chunked":
  184. environ["wsgi.input_terminated"] = True
  185. environ["wsgi.input"] = DechunkedInput(environ["wsgi.input"])
  186. # Per RFC 2616, if the URL is absolute, use that as the host.
  187. # We're using "has a scheme" to indicate an absolute URL.
  188. if request_url.scheme and request_url.netloc:
  189. environ["HTTP_HOST"] = request_url.netloc
  190. try:
  191. # binary_form=False gives nicer information, but wouldn't be compatible with
  192. # what Nginx or Apache could return.
  193. peer_cert = self.connection.getpeercert( # type: ignore[attr-defined]
  194. binary_form=True
  195. )
  196. if peer_cert is not None:
  197. # Nginx and Apache use PEM format.
  198. environ["SSL_CLIENT_CERT"] = ssl.DER_cert_to_PEM_cert(peer_cert)
  199. except ValueError:
  200. # SSL handshake hasn't finished.
  201. self.server.log("error", "Cannot fetch SSL peer certificate info")
  202. except AttributeError:
  203. # Not using TLS, the socket will not have getpeercert().
  204. pass
  205. return environ
  206. def run_wsgi(self) -> None:
  207. if self.headers.get("Expect", "").lower().strip() == "100-continue":
  208. self.wfile.write(b"HTTP/1.1 100 Continue\r\n\r\n")
  209. self.environ = environ = self.make_environ()
  210. status_set: t.Optional[str] = None
  211. headers_set: t.Optional[t.List[t.Tuple[str, str]]] = None
  212. status_sent: t.Optional[str] = None
  213. headers_sent: t.Optional[t.List[t.Tuple[str, str]]] = None
  214. def write(data: bytes) -> None:
  215. nonlocal status_sent, headers_sent
  216. assert status_set is not None, "write() before start_response"
  217. assert headers_set is not None, "write() before start_response"
  218. if status_sent is None:
  219. status_sent = status_set
  220. headers_sent = headers_set
  221. try:
  222. code_str, msg = status_sent.split(None, 1)
  223. except ValueError:
  224. code_str, msg = status_sent, ""
  225. code = int(code_str)
  226. self.send_response(code, msg)
  227. header_keys = set()
  228. for key, value in headers_sent:
  229. self.send_header(key, value)
  230. key = key.lower()
  231. header_keys.add(key)
  232. if not (
  233. "content-length" in header_keys
  234. or environ["REQUEST_METHOD"] == "HEAD"
  235. or code < 200
  236. or code in (204, 304)
  237. ):
  238. self.close_connection = True
  239. self.send_header("Connection", "close")
  240. if "server" not in header_keys:
  241. self.send_header("Server", self.version_string())
  242. if "date" not in header_keys:
  243. self.send_header("Date", self.date_time_string())
  244. self.end_headers()
  245. assert isinstance(data, bytes), "applications must write bytes"
  246. self.wfile.write(data)
  247. self.wfile.flush()
  248. def start_response(status, headers, exc_info=None): # type: ignore
  249. nonlocal status_set, headers_set
  250. if exc_info:
  251. try:
  252. if headers_sent:
  253. raise exc_info[1].with_traceback(exc_info[2])
  254. finally:
  255. exc_info = None
  256. elif headers_set:
  257. raise AssertionError("Headers already set")
  258. status_set = status
  259. headers_set = headers
  260. return write
  261. def execute(app: "WSGIApplication") -> None:
  262. application_iter = app(environ, start_response)
  263. try:
  264. for data in application_iter:
  265. write(data)
  266. if not headers_sent:
  267. write(b"")
  268. finally:
  269. if hasattr(application_iter, "close"):
  270. application_iter.close() # type: ignore
  271. try:
  272. execute(self.server.app)
  273. except (ConnectionError, socket.timeout) as e:
  274. self.connection_dropped(e, environ)
  275. except Exception:
  276. if self.server.passthrough_errors:
  277. raise
  278. from .debug.tbtools import get_current_traceback
  279. traceback = get_current_traceback(ignore_system_exceptions=True)
  280. try:
  281. # if we haven't yet sent the headers but they are set
  282. # we roll back to be able to set them again.
  283. if status_sent is None:
  284. status_set = None
  285. headers_set = None
  286. execute(InternalServerError())
  287. except Exception:
  288. pass
  289. self.server.log("error", "Error on request:\n%s", traceback.plaintext)
  290. def handle(self) -> None:
  291. """Handles a request ignoring dropped connections."""
  292. try:
  293. BaseHTTPRequestHandler.handle(self)
  294. except (ConnectionError, socket.timeout) as e:
  295. self.connection_dropped(e)
  296. except Exception as e:
  297. if self.server.ssl_context is not None and is_ssl_error(e):
  298. self.log_error("SSL error occurred: %s", e)
  299. else:
  300. raise
  301. if self.server.shutdown_signal:
  302. self.initiate_shutdown()
  303. def initiate_shutdown(self) -> None:
  304. if is_running_from_reloader():
  305. # Windows does not provide SIGKILL, go with SIGTERM then.
  306. sig = getattr(signal, "SIGKILL", signal.SIGTERM)
  307. os.kill(os.getpid(), sig)
  308. self.server._BaseServer__shutdown_request = True # type: ignore
  309. def connection_dropped(
  310. self, error: BaseException, environ: t.Optional["WSGIEnvironment"] = None
  311. ) -> None:
  312. """Called if the connection was closed by the client. By default
  313. nothing happens.
  314. """
  315. def handle_one_request(self) -> None:
  316. """Handle a single HTTP request."""
  317. self.raw_requestline = self.rfile.readline()
  318. if not self.raw_requestline:
  319. self.close_connection = True
  320. elif self.parse_request():
  321. self.run_wsgi()
  322. def send_response(self, code: int, message: t.Optional[str] = None) -> None:
  323. """Send the response header and log the response code."""
  324. self.log_request(code)
  325. if message is None:
  326. message = self.responses[code][0] if code in self.responses else ""
  327. if self.request_version != "HTTP/0.9":
  328. hdr = f"{self.protocol_version} {code} {message}\r\n"
  329. self.wfile.write(hdr.encode("ascii"))
  330. def version_string(self) -> str:
  331. return super().version_string().strip()
  332. def address_string(self) -> str:
  333. if getattr(self, "environ", None):
  334. return self.environ["REMOTE_ADDR"] # type: ignore
  335. if not self.client_address:
  336. return "<local>"
  337. return self.client_address[0]
  338. def port_integer(self) -> int:
  339. return self.client_address[1]
  340. def log_request(
  341. self, code: t.Union[int, str] = "-", size: t.Union[int, str] = "-"
  342. ) -> None:
  343. try:
  344. path = uri_to_iri(self.path)
  345. msg = f"{self.command} {path} {self.request_version}"
  346. except AttributeError:
  347. # path isn't set if the requestline was bad
  348. msg = self.requestline
  349. code = str(code)
  350. if _log_add_style:
  351. if code[0] == "1": # 1xx - Informational
  352. msg = _ansi_style(msg, "bold")
  353. elif code == "200": # 2xx - Success
  354. pass
  355. elif code == "304": # 304 - Resource Not Modified
  356. msg = _ansi_style(msg, "cyan")
  357. elif code[0] == "3": # 3xx - Redirection
  358. msg = _ansi_style(msg, "green")
  359. elif code == "404": # 404 - Resource Not Found
  360. msg = _ansi_style(msg, "yellow")
  361. elif code[0] == "4": # 4xx - Client Error
  362. msg = _ansi_style(msg, "bold", "red")
  363. else: # 5xx, or any other response
  364. msg = _ansi_style(msg, "bold", "magenta")
  365. self.log("info", '"%s" %s %s', msg, code, size)
  366. def log_error(self, format: str, *args: t.Any) -> None:
  367. self.log("error", format, *args)
  368. def log_message(self, format: str, *args: t.Any) -> None:
  369. self.log("info", format, *args)
  370. def log(self, type: str, message: str, *args: t.Any) -> None:
  371. _log(
  372. type,
  373. f"{self.address_string()} - - [{self.log_date_time_string()}] {message}\n",
  374. *args,
  375. )
  376. def _ansi_style(value: str, *styles: str) -> str:
  377. codes = {
  378. "bold": 1,
  379. "red": 31,
  380. "green": 32,
  381. "yellow": 33,
  382. "magenta": 35,
  383. "cyan": 36,
  384. }
  385. for style in styles:
  386. value = f"\x1b[{codes[style]}m{value}"
  387. return f"{value}\x1b[0m"
  388. def generate_adhoc_ssl_pair(
  389. cn: t.Optional[str] = None,
  390. ) -> t.Tuple["Certificate", "RSAPrivateKeyWithSerialization"]:
  391. try:
  392. from cryptography import x509
  393. from cryptography.x509.oid import NameOID
  394. from cryptography.hazmat.backends import default_backend
  395. from cryptography.hazmat.primitives import hashes
  396. from cryptography.hazmat.primitives.asymmetric import rsa
  397. except ImportError:
  398. raise TypeError(
  399. "Using ad-hoc certificates requires the cryptography library."
  400. ) from None
  401. backend = default_backend()
  402. pkey = rsa.generate_private_key(
  403. public_exponent=65537, key_size=2048, backend=backend
  404. )
  405. # pretty damn sure that this is not actually accepted by anyone
  406. if cn is None:
  407. cn = "*"
  408. subject = x509.Name(
  409. [
  410. x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Dummy Certificate"),
  411. x509.NameAttribute(NameOID.COMMON_NAME, cn),
  412. ]
  413. )
  414. backend = default_backend()
  415. cert = (
  416. x509.CertificateBuilder()
  417. .subject_name(subject)
  418. .issuer_name(subject)
  419. .public_key(pkey.public_key())
  420. .serial_number(x509.random_serial_number())
  421. .not_valid_before(dt.now(timezone.utc))
  422. .not_valid_after(dt.now(timezone.utc) + timedelta(days=365))
  423. .add_extension(x509.ExtendedKeyUsage([x509.OID_SERVER_AUTH]), critical=False)
  424. .add_extension(x509.SubjectAlternativeName([x509.DNSName(cn)]), critical=False)
  425. .sign(pkey, hashes.SHA256(), backend)
  426. )
  427. return cert, pkey
  428. def make_ssl_devcert(
  429. base_path: str, host: t.Optional[str] = None, cn: t.Optional[str] = None
  430. ) -> t.Tuple[str, str]:
  431. """Creates an SSL key for development. This should be used instead of
  432. the ``'adhoc'`` key which generates a new cert on each server start.
  433. It accepts a path for where it should store the key and cert and
  434. either a host or CN. If a host is given it will use the CN
  435. ``*.host/CN=host``.
  436. For more information see :func:`run_simple`.
  437. .. versionadded:: 0.9
  438. :param base_path: the path to the certificate and key. The extension
  439. ``.crt`` is added for the certificate, ``.key`` is
  440. added for the key.
  441. :param host: the name of the host. This can be used as an alternative
  442. for the `cn`.
  443. :param cn: the `CN` to use.
  444. """
  445. if host is not None:
  446. cn = f"*.{host}/CN={host}"
  447. cert, pkey = generate_adhoc_ssl_pair(cn=cn)
  448. from cryptography.hazmat.primitives import serialization
  449. cert_file = f"{base_path}.crt"
  450. pkey_file = f"{base_path}.key"
  451. with open(cert_file, "wb") as f:
  452. f.write(cert.public_bytes(serialization.Encoding.PEM))
  453. with open(pkey_file, "wb") as f:
  454. f.write(
  455. pkey.private_bytes(
  456. encoding=serialization.Encoding.PEM,
  457. format=serialization.PrivateFormat.TraditionalOpenSSL,
  458. encryption_algorithm=serialization.NoEncryption(),
  459. )
  460. )
  461. return cert_file, pkey_file
  462. def generate_adhoc_ssl_context() -> "ssl.SSLContext":
  463. """Generates an adhoc SSL context for the development server."""
  464. import tempfile
  465. import atexit
  466. cert, pkey = generate_adhoc_ssl_pair()
  467. from cryptography.hazmat.primitives import serialization
  468. cert_handle, cert_file = tempfile.mkstemp()
  469. pkey_handle, pkey_file = tempfile.mkstemp()
  470. atexit.register(os.remove, pkey_file)
  471. atexit.register(os.remove, cert_file)
  472. os.write(cert_handle, cert.public_bytes(serialization.Encoding.PEM))
  473. os.write(
  474. pkey_handle,
  475. pkey.private_bytes(
  476. encoding=serialization.Encoding.PEM,
  477. format=serialization.PrivateFormat.TraditionalOpenSSL,
  478. encryption_algorithm=serialization.NoEncryption(),
  479. ),
  480. )
  481. os.close(cert_handle)
  482. os.close(pkey_handle)
  483. ctx = load_ssl_context(cert_file, pkey_file)
  484. return ctx
  485. def load_ssl_context(
  486. cert_file: str, pkey_file: t.Optional[str] = None, protocol: t.Optional[int] = None
  487. ) -> "ssl.SSLContext":
  488. """Loads SSL context from cert/private key files and optional protocol.
  489. Many parameters are directly taken from the API of
  490. :py:class:`ssl.SSLContext`.
  491. :param cert_file: Path of the certificate to use.
  492. :param pkey_file: Path of the private key to use. If not given, the key
  493. will be obtained from the certificate file.
  494. :param protocol: A ``PROTOCOL`` constant from the :mod:`ssl` module.
  495. Defaults to :data:`ssl.PROTOCOL_TLS_SERVER`.
  496. """
  497. if protocol is None:
  498. protocol = ssl.PROTOCOL_TLS_SERVER
  499. ctx = ssl.SSLContext(protocol)
  500. ctx.load_cert_chain(cert_file, pkey_file)
  501. return ctx
  502. def is_ssl_error(error: t.Optional[Exception] = None) -> bool:
  503. """Checks if the given error (or the current one) is an SSL error."""
  504. if error is None:
  505. error = t.cast(Exception, sys.exc_info()[1])
  506. return isinstance(error, ssl.SSLError)
  507. def select_address_family(host: str, port: int) -> socket.AddressFamily:
  508. """Return ``AF_INET4``, ``AF_INET6``, or ``AF_UNIX`` depending on
  509. the host and port."""
  510. if host.startswith("unix://"):
  511. return socket.AF_UNIX
  512. elif ":" in host and hasattr(socket, "AF_INET6"):
  513. return socket.AF_INET6
  514. return socket.AF_INET
  515. def get_sockaddr(
  516. host: str, port: int, family: socket.AddressFamily
  517. ) -> t.Union[t.Tuple[str, int], str]:
  518. """Return a fully qualified socket address that can be passed to
  519. :func:`socket.bind`."""
  520. if family == af_unix:
  521. return host.split("://", 1)[1]
  522. try:
  523. res = socket.getaddrinfo(
  524. host, port, family, socket.SOCK_STREAM, socket.IPPROTO_TCP
  525. )
  526. except socket.gaierror:
  527. return host, port
  528. return res[0][4] # type: ignore
  529. def get_interface_ip(family: socket.AddressFamily) -> str:
  530. """Get the IP address of an external interface. Used when binding to
  531. 0.0.0.0 or ::1 to show a more useful URL.
  532. :meta private:
  533. """
  534. # arbitrary private address
  535. host = "fd31:f903:5ab5:1::1" if family == socket.AF_INET6 else "10.253.155.219"
  536. with socket.socket(family, socket.SOCK_DGRAM) as s:
  537. try:
  538. s.connect((host, 58162))
  539. except OSError:
  540. return "::1" if family == socket.AF_INET6 else "127.0.0.1"
  541. return s.getsockname()[0] # type: ignore
  542. class BaseWSGIServer(HTTPServer):
  543. """Simple single-threaded, single-process WSGI server."""
  544. multithread = False
  545. multiprocess = False
  546. request_queue_size = LISTEN_QUEUE
  547. def __init__(
  548. self,
  549. host: str,
  550. port: int,
  551. app: "WSGIApplication",
  552. handler: t.Optional[t.Type[WSGIRequestHandler]] = None,
  553. passthrough_errors: bool = False,
  554. ssl_context: t.Optional[_TSSLContextArg] = None,
  555. fd: t.Optional[int] = None,
  556. ) -> None:
  557. if handler is None:
  558. handler = WSGIRequestHandler
  559. self.address_family = select_address_family(host, port)
  560. if fd is not None:
  561. real_sock = socket.fromfd(fd, self.address_family, socket.SOCK_STREAM)
  562. port = 0
  563. server_address = get_sockaddr(host, int(port), self.address_family)
  564. # remove socket file if it already exists
  565. if self.address_family == af_unix:
  566. server_address = t.cast(str, server_address)
  567. if os.path.exists(server_address):
  568. os.unlink(server_address)
  569. super().__init__(server_address, handler) # type: ignore
  570. self.app = app
  571. self.passthrough_errors = passthrough_errors
  572. self.shutdown_signal = False
  573. self.host = host
  574. self.port = self.socket.getsockname()[1]
  575. # Patch in the original socket.
  576. if fd is not None:
  577. self.socket.close()
  578. self.socket = real_sock
  579. self.server_address = self.socket.getsockname()
  580. if ssl_context is not None:
  581. if isinstance(ssl_context, tuple):
  582. ssl_context = load_ssl_context(*ssl_context)
  583. if ssl_context == "adhoc":
  584. ssl_context = generate_adhoc_ssl_context()
  585. self.socket = ssl_context.wrap_socket(self.socket, server_side=True)
  586. self.ssl_context: t.Optional["ssl.SSLContext"] = ssl_context
  587. else:
  588. self.ssl_context = None
  589. def log(self, type: str, message: str, *args: t.Any) -> None:
  590. _log(type, message, *args)
  591. def serve_forever(self, poll_interval: float = 0.5) -> None:
  592. self.shutdown_signal = False
  593. try:
  594. super().serve_forever(poll_interval=poll_interval)
  595. except KeyboardInterrupt:
  596. pass
  597. finally:
  598. self.server_close()
  599. def handle_error(
  600. self, request: t.Any, client_address: t.Union[t.Tuple[str, int], str]
  601. ) -> None:
  602. if self.passthrough_errors:
  603. raise
  604. return super().handle_error(request, client_address)
  605. class ThreadedWSGIServer(socketserver.ThreadingMixIn, BaseWSGIServer):
  606. """A WSGI server that does threading."""
  607. multithread = True
  608. daemon_threads = True
  609. class ForkingWSGIServer(ForkingMixIn, BaseWSGIServer):
  610. """A WSGI server that does forking."""
  611. multiprocess = True
  612. def __init__(
  613. self,
  614. host: str,
  615. port: int,
  616. app: "WSGIApplication",
  617. processes: int = 40,
  618. handler: t.Optional[t.Type[WSGIRequestHandler]] = None,
  619. passthrough_errors: bool = False,
  620. ssl_context: t.Optional[_TSSLContextArg] = None,
  621. fd: t.Optional[int] = None,
  622. ) -> None:
  623. if not can_fork:
  624. raise ValueError("Your platform does not support forking.")
  625. BaseWSGIServer.__init__(
  626. self, host, port, app, handler, passthrough_errors, ssl_context, fd
  627. )
  628. self.max_children = processes
  629. def make_server(
  630. host: str,
  631. port: int,
  632. app: "WSGIApplication",
  633. threaded: bool = False,
  634. processes: int = 1,
  635. request_handler: t.Optional[t.Type[WSGIRequestHandler]] = None,
  636. passthrough_errors: bool = False,
  637. ssl_context: t.Optional[_TSSLContextArg] = None,
  638. fd: t.Optional[int] = None,
  639. ) -> BaseWSGIServer:
  640. """Create a new server instance that is either threaded, or forks
  641. or just processes one request after another.
  642. """
  643. if threaded and processes > 1:
  644. raise ValueError("cannot have a multithreaded and multi process server.")
  645. elif threaded:
  646. return ThreadedWSGIServer(
  647. host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd
  648. )
  649. elif processes > 1:
  650. return ForkingWSGIServer(
  651. host,
  652. port,
  653. app,
  654. processes,
  655. request_handler,
  656. passthrough_errors,
  657. ssl_context,
  658. fd=fd,
  659. )
  660. else:
  661. return BaseWSGIServer(
  662. host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd
  663. )
  664. def is_running_from_reloader() -> bool:
  665. """Checks if the application is running from within the Werkzeug
  666. reloader subprocess.
  667. .. versionadded:: 0.10
  668. """
  669. return os.environ.get("WERKZEUG_RUN_MAIN") == "true"
  670. def run_simple(
  671. hostname: str,
  672. port: int,
  673. application: "WSGIApplication",
  674. use_reloader: bool = False,
  675. use_debugger: bool = False,
  676. use_evalex: bool = True,
  677. extra_files: t.Optional[t.Iterable[str]] = None,
  678. exclude_patterns: t.Optional[t.Iterable[str]] = None,
  679. reloader_interval: int = 1,
  680. reloader_type: str = "auto",
  681. threaded: bool = False,
  682. processes: int = 1,
  683. request_handler: t.Optional[t.Type[WSGIRequestHandler]] = None,
  684. static_files: t.Optional[t.Dict[str, t.Union[str, t.Tuple[str, str]]]] = None,
  685. passthrough_errors: bool = False,
  686. ssl_context: t.Optional[_TSSLContextArg] = None,
  687. ) -> None:
  688. """Start a WSGI application. Optional features include a reloader,
  689. multithreading and fork support.
  690. This function has a command-line interface too::
  691. python -m werkzeug.serving --help
  692. .. versionchanged:: 2.0
  693. Added ``exclude_patterns`` parameter.
  694. .. versionadded:: 0.5
  695. `static_files` was added to simplify serving of static files as well
  696. as `passthrough_errors`.
  697. .. versionadded:: 0.6
  698. support for SSL was added.
  699. .. versionadded:: 0.8
  700. Added support for automatically loading a SSL context from certificate
  701. file and private key.
  702. .. versionadded:: 0.9
  703. Added command-line interface.
  704. .. versionadded:: 0.10
  705. Improved the reloader and added support for changing the backend
  706. through the `reloader_type` parameter. See :ref:`reloader`
  707. for more information.
  708. .. versionchanged:: 0.15
  709. Bind to a Unix socket by passing a path that starts with
  710. ``unix://`` as the ``hostname``.
  711. :param hostname: The host to bind to, for example ``'localhost'``.
  712. If the value is a path that starts with ``unix://`` it will bind
  713. to a Unix socket instead of a TCP socket..
  714. :param port: The port for the server. eg: ``8080``
  715. :param application: the WSGI application to execute
  716. :param use_reloader: should the server automatically restart the python
  717. process if modules were changed?
  718. :param use_debugger: should the werkzeug debugging system be used?
  719. :param use_evalex: should the exception evaluation feature be enabled?
  720. :param extra_files: a list of files the reloader should watch
  721. additionally to the modules. For example configuration
  722. files.
  723. :param exclude_patterns: List of :mod:`fnmatch` patterns to ignore
  724. when running the reloader. For example, ignore cache files that
  725. shouldn't reload when updated.
  726. :param reloader_interval: the interval for the reloader in seconds.
  727. :param reloader_type: the type of reloader to use. The default is
  728. auto detection. Valid values are ``'stat'`` and
  729. ``'watchdog'``. See :ref:`reloader` for more
  730. information.
  731. :param threaded: should the process handle each request in a separate
  732. thread?
  733. :param processes: if greater than 1 then handle each request in a new process
  734. up to this maximum number of concurrent processes.
  735. :param request_handler: optional parameter that can be used to replace
  736. the default one. You can use this to replace it
  737. with a different
  738. :class:`~BaseHTTPServer.BaseHTTPRequestHandler`
  739. subclass.
  740. :param static_files: a list or dict of paths for static files. This works
  741. exactly like :class:`SharedDataMiddleware`, it's actually
  742. just wrapping the application in that middleware before
  743. serving.
  744. :param passthrough_errors: set this to `True` to disable the error catching.
  745. This means that the server will die on errors but
  746. it can be useful to hook debuggers in (pdb etc.)
  747. :param ssl_context: an SSL context for the connection. Either an
  748. :class:`ssl.SSLContext`, a tuple in the form
  749. ``(cert_file, pkey_file)``, the string ``'adhoc'`` if
  750. the server should automatically create one, or ``None``
  751. to disable SSL (which is the default).
  752. """
  753. if not isinstance(port, int):
  754. raise TypeError("port must be an integer")
  755. if use_debugger:
  756. from .debug import DebuggedApplication
  757. application = DebuggedApplication(application, use_evalex)
  758. if static_files:
  759. from .middleware.shared_data import SharedDataMiddleware
  760. application = SharedDataMiddleware(application, static_files)
  761. def log_startup(sock: socket.socket) -> None:
  762. all_addresses_message = (
  763. " * Running on all addresses.\n"
  764. " WARNING: This is a development server. Do not use it in"
  765. " a production deployment."
  766. )
  767. if sock.family == af_unix:
  768. _log("info", " * Running on %s (Press CTRL+C to quit)", hostname)
  769. else:
  770. if hostname == "0.0.0.0":
  771. _log("warning", all_addresses_message)
  772. display_hostname = get_interface_ip(socket.AF_INET)
  773. elif hostname == "::":
  774. _log("warning", all_addresses_message)
  775. display_hostname = get_interface_ip(socket.AF_INET6)
  776. else:
  777. display_hostname = hostname
  778. if ":" in display_hostname:
  779. display_hostname = f"[{display_hostname}]"
  780. _log(
  781. "info",
  782. " * Running on %s://%s:%d/ (Press CTRL+C to quit)",
  783. "http" if ssl_context is None else "https",
  784. display_hostname,
  785. sock.getsockname()[1],
  786. )
  787. def inner() -> None:
  788. try:
  789. fd: t.Optional[int] = int(os.environ["WERKZEUG_SERVER_FD"])
  790. except (LookupError, ValueError):
  791. fd = None
  792. srv = make_server(
  793. hostname,
  794. port,
  795. application,
  796. threaded,
  797. processes,
  798. request_handler,
  799. passthrough_errors,
  800. ssl_context,
  801. fd=fd,
  802. )
  803. if fd is None:
  804. log_startup(srv.socket)
  805. srv.serve_forever()
  806. if use_reloader:
  807. # If we're not running already in the subprocess that is the
  808. # reloader we want to open up a socket early to make sure the
  809. # port is actually available.
  810. if not is_running_from_reloader():
  811. if port == 0 and not can_open_by_fd:
  812. raise ValueError(
  813. "Cannot bind to a random port with enabled "
  814. "reloader if the Python interpreter does "
  815. "not support socket opening by fd."
  816. )
  817. # Create and destroy a socket so that any exceptions are
  818. # raised before we spawn a separate Python interpreter and
  819. # lose this ability.
  820. address_family = select_address_family(hostname, port)
  821. server_address = get_sockaddr(hostname, port, address_family)
  822. s = socket.socket(address_family, socket.SOCK_STREAM)
  823. s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  824. s.bind(server_address)
  825. s.set_inheritable(True)
  826. # If we can open the socket by file descriptor, then we can just
  827. # reuse this one and our socket will survive the restarts.
  828. if can_open_by_fd:
  829. os.environ["WERKZEUG_SERVER_FD"] = str(s.fileno())
  830. s.listen(LISTEN_QUEUE)
  831. log_startup(s)
  832. else:
  833. s.close()
  834. if address_family == af_unix:
  835. server_address = t.cast(str, server_address)
  836. _log("info", "Unlinking %s", server_address)
  837. os.unlink(server_address)
  838. from ._reloader import run_with_reloader as _rwr
  839. _rwr(
  840. inner,
  841. extra_files=extra_files,
  842. exclude_patterns=exclude_patterns,
  843. interval=reloader_interval,
  844. reloader_type=reloader_type,
  845. )
  846. else:
  847. inner()
  848. def run_with_reloader(*args: t.Any, **kwargs: t.Any) -> None:
  849. """Run a process with the reloader. This is not a public API, do
  850. not use this function.
  851. .. deprecated:: 2.0
  852. Will be removed in Werkzeug 2.1.
  853. """
  854. from ._reloader import run_with_reloader as _rwr
  855. warnings.warn(
  856. (
  857. "'run_with_reloader' is a private API, it will no longer be"
  858. " accessible in Werkzeug 2.1. Use 'run_simple' instead."
  859. ),
  860. DeprecationWarning,
  861. stacklevel=2,
  862. )
  863. _rwr(*args, **kwargs)
  864. def main() -> None:
  865. """A simple command-line interface for :py:func:`run_simple`."""
  866. import argparse
  867. from .utils import import_string
  868. _log("warning", "This CLI is deprecated and will be removed in version 2.1.")
  869. parser = argparse.ArgumentParser(
  870. description="Run the given WSGI application with the development server.",
  871. allow_abbrev=False,
  872. )
  873. parser.add_argument(
  874. "-b",
  875. "--bind",
  876. dest="address",
  877. help="The hostname:port the app should listen on.",
  878. )
  879. parser.add_argument(
  880. "-d",
  881. "--debug",
  882. action="store_true",
  883. help="Show the interactive debugger for unhandled exceptions.",
  884. )
  885. parser.add_argument(
  886. "-r",
  887. "--reload",
  888. action="store_true",
  889. help="Reload the process if modules change.",
  890. )
  891. parser.add_argument(
  892. "application", help="Application to import and serve, in the form module:app."
  893. )
  894. args = parser.parse_args()
  895. hostname, port = None, None
  896. if args.address:
  897. hostname, _, port = args.address.partition(":")
  898. run_simple(
  899. hostname=hostname or "127.0.0.1",
  900. port=int(port or 5000),
  901. application=import_string(args.application),
  902. use_reloader=args.reload,
  903. use_debugger=args.debug,
  904. )
  905. if __name__ == "__main__":
  906. main()