serving.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. # -*- coding: utf-8 -*-
  2. """
  3. werkzeug.serving
  4. ~~~~~~~~~~~~~~~~
  5. There are many ways to serve a WSGI application. While you're developing
  6. it you usually don't want a full blown webserver like Apache but a simple
  7. standalone one. From Python 2.5 onwards there is the `wsgiref`_ server in
  8. the standard library. If you're using older versions of Python you can
  9. download the package from the cheeseshop.
  10. However there are some caveats. Sourcecode won't reload itself when
  11. changed and each time you kill the server using ``^C`` you get an
  12. `KeyboardInterrupt` error. While the latter is easy to solve the first
  13. one can be a pain in the ass in some situations.
  14. The easiest way is creating a small ``start-myproject.py`` that runs the
  15. application::
  16. #!/usr/bin/env python
  17. # -*- coding: utf-8 -*-
  18. from myproject import make_app
  19. from werkzeug.serving import run_simple
  20. app = make_app(...)
  21. run_simple('localhost', 8080, app, use_reloader=True)
  22. You can also pass it a `extra_files` keyword argument with a list of
  23. additional files (like configuration files) you want to observe.
  24. For bigger applications you should consider using `click`
  25. (http://click.pocoo.org) instead of a simple start file.
  26. :copyright: 2007 Pallets
  27. :license: BSD-3-Clause
  28. """
  29. import io
  30. import os
  31. import signal
  32. import socket
  33. import sys
  34. from ._compat import PY2
  35. from ._compat import reraise
  36. from ._compat import WIN
  37. from ._compat import wsgi_encoding_dance
  38. from ._internal import _log
  39. from .exceptions import InternalServerError
  40. from .urls import uri_to_iri
  41. from .urls import url_parse
  42. from .urls import url_unquote
  43. try:
  44. import socketserver
  45. from http.server import BaseHTTPRequestHandler
  46. from http.server import HTTPServer
  47. except ImportError:
  48. import SocketServer as socketserver
  49. from BaseHTTPServer import HTTPServer
  50. from BaseHTTPServer import BaseHTTPRequestHandler
  51. try:
  52. import ssl
  53. except ImportError:
  54. class _SslDummy(object):
  55. def __getattr__(self, name):
  56. raise RuntimeError("SSL support unavailable")
  57. ssl = _SslDummy()
  58. try:
  59. import termcolor
  60. except ImportError:
  61. termcolor = None
  62. def _get_openssl_crypto_module():
  63. try:
  64. from OpenSSL import crypto
  65. except ImportError:
  66. raise TypeError("Using ad-hoc certificates requires the pyOpenSSL library.")
  67. else:
  68. return crypto
  69. ThreadingMixIn = socketserver.ThreadingMixIn
  70. can_fork = hasattr(os, "fork")
  71. if can_fork:
  72. ForkingMixIn = socketserver.ForkingMixIn
  73. else:
  74. class ForkingMixIn(object):
  75. pass
  76. try:
  77. af_unix = socket.AF_UNIX
  78. except AttributeError:
  79. af_unix = None
  80. LISTEN_QUEUE = 128
  81. can_open_by_fd = not WIN and hasattr(socket, "fromfd")
  82. # On Python 3, ConnectionError represents the same errnos as
  83. # socket.error from Python 2, while socket.error is an alias for the
  84. # more generic OSError.
  85. if PY2:
  86. _ConnectionError = socket.error
  87. else:
  88. _ConnectionError = ConnectionError
  89. class DechunkedInput(io.RawIOBase):
  90. """An input stream that handles Transfer-Encoding 'chunked'"""
  91. def __init__(self, rfile):
  92. self._rfile = rfile
  93. self._done = False
  94. self._len = 0
  95. def readable(self):
  96. return True
  97. def read_chunk_len(self):
  98. try:
  99. line = self._rfile.readline().decode("latin1")
  100. _len = int(line.strip(), 16)
  101. except ValueError:
  102. raise IOError("Invalid chunk header")
  103. if _len < 0:
  104. raise IOError("Negative chunk length not allowed")
  105. return _len
  106. def readinto(self, buf):
  107. read = 0
  108. while not self._done and read < len(buf):
  109. if self._len == 0:
  110. # This is the first chunk or we fully consumed the previous
  111. # one. Read the next length of the next chunk
  112. self._len = self.read_chunk_len()
  113. if self._len == 0:
  114. # Found the final chunk of size 0. The stream is now exhausted,
  115. # but there is still a final newline that should be consumed
  116. self._done = True
  117. if self._len > 0:
  118. # There is data (left) in this chunk, so append it to the
  119. # buffer. If this operation fully consumes the chunk, this will
  120. # reset self._len to 0.
  121. n = min(len(buf), self._len)
  122. buf[read : read + n] = self._rfile.read(n)
  123. self._len -= n
  124. read += n
  125. if self._len == 0:
  126. # Skip the terminating newline of a chunk that has been fully
  127. # consumed. This also applies to the 0-sized final chunk
  128. terminator = self._rfile.readline()
  129. if terminator not in (b"\n", b"\r\n", b"\r"):
  130. raise IOError("Missing chunk terminating newline")
  131. return read
  132. class WSGIRequestHandler(BaseHTTPRequestHandler, object):
  133. """A request handler that implements WSGI dispatching."""
  134. @property
  135. def server_version(self):
  136. from . import __version__
  137. return "Werkzeug/" + __version__
  138. def make_environ(self):
  139. request_url = url_parse(self.path)
  140. def shutdown_server():
  141. self.server.shutdown_signal = True
  142. url_scheme = "http" if self.server.ssl_context is None else "https"
  143. if not self.client_address:
  144. self.client_address = "<local>"
  145. if isinstance(self.client_address, str):
  146. self.client_address = (self.client_address, 0)
  147. else:
  148. pass
  149. path_info = url_unquote(request_url.path)
  150. environ = {
  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. "SERVER_SOFTWARE": self.server_version,
  160. "REQUEST_METHOD": self.command,
  161. "SCRIPT_NAME": "",
  162. "PATH_INFO": wsgi_encoding_dance(path_info),
  163. "QUERY_STRING": wsgi_encoding_dance(request_url.query),
  164. # Non-standard, added by mod_wsgi, uWSGI
  165. "REQUEST_URI": wsgi_encoding_dance(self.path),
  166. # Non-standard, added by gunicorn
  167. "RAW_URI": wsgi_encoding_dance(self.path),
  168. "REMOTE_ADDR": self.address_string(),
  169. "REMOTE_PORT": self.port_integer(),
  170. "SERVER_NAME": self.server.server_address[0],
  171. "SERVER_PORT": str(self.server.server_address[1]),
  172. "SERVER_PROTOCOL": self.request_version,
  173. }
  174. for key, value in self.get_header_items():
  175. key = key.upper().replace("-", "_")
  176. value = value.replace("\r\n", "")
  177. if key not in ("CONTENT_TYPE", "CONTENT_LENGTH"):
  178. key = "HTTP_" + key
  179. if key in environ:
  180. value = "{},{}".format(environ[key], value)
  181. environ[key] = value
  182. if environ.get("HTTP_TRANSFER_ENCODING", "").strip().lower() == "chunked":
  183. environ["wsgi.input_terminated"] = True
  184. environ["wsgi.input"] = DechunkedInput(environ["wsgi.input"])
  185. if request_url.scheme and request_url.netloc:
  186. environ["HTTP_HOST"] = request_url.netloc
  187. return environ
  188. def run_wsgi(self):
  189. if self.headers.get("Expect", "").lower().strip() == "100-continue":
  190. self.wfile.write(b"HTTP/1.1 100 Continue\r\n\r\n")
  191. self.environ = environ = self.make_environ()
  192. headers_set = []
  193. headers_sent = []
  194. def write(data):
  195. assert headers_set, "write() before start_response"
  196. if not headers_sent:
  197. status, response_headers = headers_sent[:] = headers_set
  198. try:
  199. code, msg = status.split(None, 1)
  200. except ValueError:
  201. code, msg = status, ""
  202. code = int(code)
  203. self.send_response(code, msg)
  204. header_keys = set()
  205. for key, value in response_headers:
  206. self.send_header(key, value)
  207. key = key.lower()
  208. header_keys.add(key)
  209. if not (
  210. "content-length" in header_keys
  211. or environ["REQUEST_METHOD"] == "HEAD"
  212. or code < 200
  213. or code in (204, 304)
  214. ):
  215. self.close_connection = True
  216. self.send_header("Connection", "close")
  217. if "server" not in header_keys:
  218. self.send_header("Server", self.version_string())
  219. if "date" not in header_keys:
  220. self.send_header("Date", self.date_time_string())
  221. self.end_headers()
  222. assert isinstance(data, bytes), "applications must write bytes"
  223. if data:
  224. # Only write data if there is any to avoid Python 3.5 SSL bug
  225. self.wfile.write(data)
  226. self.wfile.flush()
  227. def start_response(status, response_headers, exc_info=None):
  228. if exc_info:
  229. try:
  230. if headers_sent:
  231. reraise(*exc_info)
  232. finally:
  233. exc_info = None
  234. elif headers_set:
  235. raise AssertionError("Headers already set")
  236. headers_set[:] = [status, response_headers]
  237. return write
  238. def execute(app):
  239. application_iter = app(environ, start_response)
  240. try:
  241. for data in application_iter:
  242. write(data)
  243. if not headers_sent:
  244. write(b"")
  245. finally:
  246. if hasattr(application_iter, "close"):
  247. application_iter.close()
  248. application_iter = None
  249. try:
  250. execute(self.server.app)
  251. except (_ConnectionError, socket.timeout) as e:
  252. self.connection_dropped(e, environ)
  253. except Exception:
  254. if self.server.passthrough_errors:
  255. raise
  256. from .debug.tbtools import get_current_traceback
  257. traceback = get_current_traceback(ignore_system_exceptions=True)
  258. try:
  259. # if we haven't yet sent the headers but they are set
  260. # we roll back to be able to set them again.
  261. if not headers_sent:
  262. del headers_set[:]
  263. execute(InternalServerError())
  264. except Exception:
  265. pass
  266. self.server.log("error", "Error on request:\n%s", traceback.plaintext)
  267. def handle(self):
  268. """Handles a request ignoring dropped connections."""
  269. rv = None
  270. try:
  271. rv = BaseHTTPRequestHandler.handle(self)
  272. except (_ConnectionError, socket.timeout) as e:
  273. self.connection_dropped(e)
  274. except Exception as e:
  275. if self.server.ssl_context is None or not is_ssl_error(e):
  276. raise
  277. if self.server.shutdown_signal:
  278. self.initiate_shutdown()
  279. return rv
  280. def initiate_shutdown(self):
  281. """A horrible, horrible way to kill the server for Python 2.6 and
  282. later. It's the best we can do.
  283. """
  284. # Windows does not provide SIGKILL, go with SIGTERM then.
  285. sig = getattr(signal, "SIGKILL", signal.SIGTERM)
  286. # reloader active
  287. if is_running_from_reloader():
  288. os.kill(os.getpid(), sig)
  289. # python 2.7
  290. self.server._BaseServer__shutdown_request = True
  291. # python 2.6
  292. self.server._BaseServer__serving = False
  293. def connection_dropped(self, error, environ=None):
  294. """Called if the connection was closed by the client. By default
  295. nothing happens.
  296. """
  297. def handle_one_request(self):
  298. """Handle a single HTTP request."""
  299. self.raw_requestline = self.rfile.readline()
  300. if not self.raw_requestline:
  301. self.close_connection = 1
  302. elif self.parse_request():
  303. return self.run_wsgi()
  304. def send_response(self, code, message=None):
  305. """Send the response header and log the response code."""
  306. self.log_request(code)
  307. if message is None:
  308. message = code in self.responses and self.responses[code][0] or ""
  309. if self.request_version != "HTTP/0.9":
  310. hdr = "%s %d %s\r\n" % (self.protocol_version, code, message)
  311. self.wfile.write(hdr.encode("ascii"))
  312. def version_string(self):
  313. return BaseHTTPRequestHandler.version_string(self).strip()
  314. def address_string(self):
  315. if getattr(self, "environ", None):
  316. return self.environ["REMOTE_ADDR"]
  317. elif not self.client_address:
  318. return "<local>"
  319. elif isinstance(self.client_address, str):
  320. return self.client_address
  321. else:
  322. return self.client_address[0]
  323. def port_integer(self):
  324. return self.client_address[1]
  325. def log_request(self, code="-", size="-"):
  326. try:
  327. path = uri_to_iri(self.path)
  328. msg = "%s %s %s" % (self.command, path, self.request_version)
  329. except AttributeError:
  330. # path isn't set if the requestline was bad
  331. msg = self.requestline
  332. code = str(code)
  333. if termcolor:
  334. color = termcolor.colored
  335. if code[0] == "1": # 1xx - Informational
  336. msg = color(msg, attrs=["bold"])
  337. elif code[0] == "2": # 2xx - Success
  338. msg = color(msg, color="white")
  339. elif code == "304": # 304 - Resource Not Modified
  340. msg = color(msg, color="cyan")
  341. elif code[0] == "3": # 3xx - Redirection
  342. msg = color(msg, color="green")
  343. elif code == "404": # 404 - Resource Not Found
  344. msg = color(msg, color="yellow")
  345. elif code[0] == "4": # 4xx - Client Error
  346. msg = color(msg, color="red", attrs=["bold"])
  347. else: # 5xx, or any other response
  348. msg = color(msg, color="magenta", attrs=["bold"])
  349. self.log("info", '"%s" %s %s', msg, code, size)
  350. def log_error(self, *args):
  351. self.log("error", *args)
  352. def log_message(self, format, *args):
  353. self.log("info", format, *args)
  354. def log(self, type, message, *args):
  355. _log(
  356. type,
  357. "%s - - [%s] %s\n"
  358. % (self.address_string(), self.log_date_time_string(), message % args),
  359. )
  360. def get_header_items(self):
  361. """
  362. Get an iterable list of key/value pairs representing headers.
  363. This function provides Python 2/3 compatibility as related to the
  364. parsing of request headers. Python 2.7 is not compliant with
  365. RFC 3875 Section 4.1.18 which requires multiple values for headers
  366. to be provided or RFC 2616 which allows for folding of multi-line
  367. headers. This function will return a matching list regardless
  368. of Python version. It can be removed once Python 2.7 support
  369. is dropped.
  370. :return: List of tuples containing header hey/value pairs
  371. """
  372. if PY2:
  373. # For Python 2, process the headers manually according to
  374. # W3C RFC 2616 Section 4.2.
  375. items = []
  376. for header in self.headers.headers:
  377. # Remove "\r\n" from the header and split on ":" to get
  378. # the field name and value.
  379. try:
  380. key, value = header[0:-2].split(":", 1)
  381. except ValueError:
  382. # If header could not be slit with : but starts with white
  383. # space and it follows an existing header, it's a folded
  384. # header.
  385. if header[0] in ("\t", " ") and items:
  386. # Pop off the last header
  387. key, value = items.pop()
  388. # Append the current header to the value of the last
  389. # header which will be placed back on the end of the
  390. # list
  391. value = value + header
  392. # Otherwise it's just a bad header and should error
  393. else:
  394. # Re-raise the value error
  395. raise
  396. # Add the key and the value once stripped of leading
  397. # white space. The specification allows for stripping
  398. # trailing white space but the Python 3 code does not
  399. # strip trailing white space. Therefore, trailing space
  400. # will be left as is to match the Python 3 behavior.
  401. items.append((key, value.lstrip()))
  402. else:
  403. items = self.headers.items()
  404. return items
  405. #: backwards compatible name if someone is subclassing it
  406. BaseRequestHandler = WSGIRequestHandler
  407. def generate_adhoc_ssl_pair(cn=None):
  408. from random import random
  409. crypto = _get_openssl_crypto_module()
  410. # pretty damn sure that this is not actually accepted by anyone
  411. if cn is None:
  412. cn = "*"
  413. cert = crypto.X509()
  414. cert.set_serial_number(int(random() * sys.maxsize))
  415. cert.gmtime_adj_notBefore(0)
  416. cert.gmtime_adj_notAfter(60 * 60 * 24 * 365)
  417. subject = cert.get_subject()
  418. subject.CN = cn
  419. subject.O = "Dummy Certificate" # noqa: E741
  420. issuer = cert.get_issuer()
  421. issuer.CN = subject.CN
  422. issuer.O = subject.O # noqa: E741
  423. pkey = crypto.PKey()
  424. pkey.generate_key(crypto.TYPE_RSA, 2048)
  425. cert.set_pubkey(pkey)
  426. cert.sign(pkey, "sha256")
  427. return cert, pkey
  428. def make_ssl_devcert(base_path, host=None, cn=None):
  429. """Creates an SSL key for development. This should be used instead of
  430. the ``'adhoc'`` key which generates a new cert on each server start.
  431. It accepts a path for where it should store the key and cert and
  432. either a host or CN. If a host is given it will use the CN
  433. ``*.host/CN=host``.
  434. For more information see :func:`run_simple`.
  435. .. versionadded:: 0.9
  436. :param base_path: the path to the certificate and key. The extension
  437. ``.crt`` is added for the certificate, ``.key`` is
  438. added for the key.
  439. :param host: the name of the host. This can be used as an alternative
  440. for the `cn`.
  441. :param cn: the `CN` to use.
  442. """
  443. from OpenSSL import crypto
  444. if host is not None:
  445. cn = "*.%s/CN=%s" % (host, host)
  446. cert, pkey = generate_adhoc_ssl_pair(cn=cn)
  447. cert_file = base_path + ".crt"
  448. pkey_file = base_path + ".key"
  449. with open(cert_file, "wb") as f:
  450. f.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert))
  451. with open(pkey_file, "wb") as f:
  452. f.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey))
  453. return cert_file, pkey_file
  454. def generate_adhoc_ssl_context():
  455. """Generates an adhoc SSL context for the development server."""
  456. crypto = _get_openssl_crypto_module()
  457. import tempfile
  458. import atexit
  459. cert, pkey = generate_adhoc_ssl_pair()
  460. cert_handle, cert_file = tempfile.mkstemp()
  461. pkey_handle, pkey_file = tempfile.mkstemp()
  462. atexit.register(os.remove, pkey_file)
  463. atexit.register(os.remove, cert_file)
  464. os.write(cert_handle, crypto.dump_certificate(crypto.FILETYPE_PEM, cert))
  465. os.write(pkey_handle, crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey))
  466. os.close(cert_handle)
  467. os.close(pkey_handle)
  468. ctx = load_ssl_context(cert_file, pkey_file)
  469. return ctx
  470. def load_ssl_context(cert_file, pkey_file=None, protocol=None):
  471. """Loads SSL context from cert/private key files and optional protocol.
  472. Many parameters are directly taken from the API of
  473. :py:class:`ssl.SSLContext`.
  474. :param cert_file: Path of the certificate to use.
  475. :param pkey_file: Path of the private key to use. If not given, the key
  476. will be obtained from the certificate file.
  477. :param protocol: One of the ``PROTOCOL_*`` constants in the stdlib ``ssl``
  478. module. Defaults to ``PROTOCOL_SSLv23``.
  479. """
  480. if protocol is None:
  481. protocol = ssl.PROTOCOL_SSLv23
  482. ctx = _SSLContext(protocol)
  483. ctx.load_cert_chain(cert_file, pkey_file)
  484. return ctx
  485. class _SSLContext(object):
  486. """A dummy class with a small subset of Python3's ``ssl.SSLContext``, only
  487. intended to be used with and by Werkzeug."""
  488. def __init__(self, protocol):
  489. self._protocol = protocol
  490. self._certfile = None
  491. self._keyfile = None
  492. self._password = None
  493. def load_cert_chain(self, certfile, keyfile=None, password=None):
  494. self._certfile = certfile
  495. self._keyfile = keyfile or certfile
  496. self._password = password
  497. def wrap_socket(self, sock, **kwargs):
  498. return ssl.wrap_socket(
  499. sock,
  500. keyfile=self._keyfile,
  501. certfile=self._certfile,
  502. ssl_version=self._protocol,
  503. **kwargs
  504. )
  505. def is_ssl_error(error=None):
  506. """Checks if the given error (or the current one) is an SSL error."""
  507. exc_types = (ssl.SSLError,)
  508. try:
  509. from OpenSSL.SSL import Error
  510. exc_types += (Error,)
  511. except ImportError:
  512. pass
  513. if error is None:
  514. error = sys.exc_info()[1]
  515. return isinstance(error, exc_types)
  516. def select_address_family(host, port):
  517. """Return ``AF_INET4``, ``AF_INET6``, or ``AF_UNIX`` depending on
  518. the host and port."""
  519. # disabled due to problems with current ipv6 implementations
  520. # and various operating systems. Probably this code also is
  521. # not supposed to work, but I can't come up with any other
  522. # ways to implement this.
  523. # try:
  524. # info = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
  525. # socket.SOCK_STREAM, 0,
  526. # socket.AI_PASSIVE)
  527. # if info:
  528. # return info[0][0]
  529. # except socket.gaierror:
  530. # pass
  531. if host.startswith("unix://"):
  532. return socket.AF_UNIX
  533. elif ":" in host and hasattr(socket, "AF_INET6"):
  534. return socket.AF_INET6
  535. return socket.AF_INET
  536. def get_sockaddr(host, port, family):
  537. """Return a fully qualified socket address that can be passed to
  538. :func:`socket.bind`."""
  539. if family == af_unix:
  540. return host.split("://", 1)[1]
  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]
  548. class BaseWSGIServer(HTTPServer, object):
  549. """Simple single-threaded, single-process WSGI server."""
  550. multithread = False
  551. multiprocess = False
  552. request_queue_size = LISTEN_QUEUE
  553. def __init__(
  554. self,
  555. host,
  556. port,
  557. app,
  558. handler=None,
  559. passthrough_errors=False,
  560. ssl_context=None,
  561. fd=None,
  562. ):
  563. if handler is None:
  564. handler = WSGIRequestHandler
  565. self.address_family = select_address_family(host, port)
  566. if fd is not None:
  567. real_sock = socket.fromfd(fd, self.address_family, socket.SOCK_STREAM)
  568. port = 0
  569. server_address = get_sockaddr(host, int(port), self.address_family)
  570. # remove socket file if it already exists
  571. if self.address_family == af_unix and os.path.exists(server_address):
  572. os.unlink(server_address)
  573. HTTPServer.__init__(self, server_address, handler)
  574. self.app = app
  575. self.passthrough_errors = passthrough_errors
  576. self.shutdown_signal = False
  577. self.host = host
  578. self.port = self.socket.getsockname()[1]
  579. # Patch in the original socket.
  580. if fd is not None:
  581. self.socket.close()
  582. self.socket = real_sock
  583. self.server_address = self.socket.getsockname()
  584. if ssl_context is not None:
  585. if isinstance(ssl_context, tuple):
  586. ssl_context = load_ssl_context(*ssl_context)
  587. if ssl_context == "adhoc":
  588. ssl_context = generate_adhoc_ssl_context()
  589. # If we are on Python 2 the return value from socket.fromfd
  590. # is an internal socket object but what we need for ssl wrap
  591. # is the wrapper around it :(
  592. sock = self.socket
  593. if PY2 and not isinstance(sock, socket.socket):
  594. sock = socket.socket(sock.family, sock.type, sock.proto, sock)
  595. self.socket = ssl_context.wrap_socket(sock, server_side=True)
  596. self.ssl_context = ssl_context
  597. else:
  598. self.ssl_context = None
  599. def log(self, type, message, *args):
  600. _log(type, message, *args)
  601. def serve_forever(self):
  602. self.shutdown_signal = False
  603. try:
  604. HTTPServer.serve_forever(self)
  605. except KeyboardInterrupt:
  606. pass
  607. finally:
  608. self.server_close()
  609. def handle_error(self, request, client_address):
  610. if self.passthrough_errors:
  611. raise
  612. # Python 2 still causes a socket.error after the earlier
  613. # handling, so silence it here.
  614. if isinstance(sys.exc_info()[1], _ConnectionError):
  615. return
  616. return HTTPServer.handle_error(self, request, client_address)
  617. def get_request(self):
  618. con, info = self.socket.accept()
  619. return con, info
  620. class ThreadedWSGIServer(ThreadingMixIn, BaseWSGIServer):
  621. """A WSGI server that does threading."""
  622. multithread = True
  623. daemon_threads = True
  624. class ForkingWSGIServer(ForkingMixIn, BaseWSGIServer):
  625. """A WSGI server that does forking."""
  626. multiprocess = True
  627. def __init__(
  628. self,
  629. host,
  630. port,
  631. app,
  632. processes=40,
  633. handler=None,
  634. passthrough_errors=False,
  635. ssl_context=None,
  636. fd=None,
  637. ):
  638. if not can_fork:
  639. raise ValueError("Your platform does not support forking.")
  640. BaseWSGIServer.__init__(
  641. self, host, port, app, handler, passthrough_errors, ssl_context, fd
  642. )
  643. self.max_children = processes
  644. def make_server(
  645. host=None,
  646. port=None,
  647. app=None,
  648. threaded=False,
  649. processes=1,
  650. request_handler=None,
  651. passthrough_errors=False,
  652. ssl_context=None,
  653. fd=None,
  654. ):
  655. """Create a new server instance that is either threaded, or forks
  656. or just processes one request after another.
  657. """
  658. if threaded and processes > 1:
  659. raise ValueError("cannot have a multithreaded and multi process server.")
  660. elif threaded:
  661. return ThreadedWSGIServer(
  662. host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd
  663. )
  664. elif processes > 1:
  665. return ForkingWSGIServer(
  666. host,
  667. port,
  668. app,
  669. processes,
  670. request_handler,
  671. passthrough_errors,
  672. ssl_context,
  673. fd=fd,
  674. )
  675. else:
  676. return BaseWSGIServer(
  677. host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd
  678. )
  679. def is_running_from_reloader():
  680. """Checks if the application is running from within the Werkzeug
  681. reloader subprocess.
  682. .. versionadded:: 0.10
  683. """
  684. return os.environ.get("WERKZEUG_RUN_MAIN") == "true"
  685. def run_simple(
  686. hostname,
  687. port,
  688. application,
  689. use_reloader=False,
  690. use_debugger=False,
  691. use_evalex=True,
  692. extra_files=None,
  693. reloader_interval=1,
  694. reloader_type="auto",
  695. threaded=False,
  696. processes=1,
  697. request_handler=None,
  698. static_files=None,
  699. passthrough_errors=False,
  700. ssl_context=None,
  701. ):
  702. """Start a WSGI application. Optional features include a reloader,
  703. multithreading and fork support.
  704. This function has a command-line interface too::
  705. python -m werkzeug.serving --help
  706. .. versionadded:: 0.5
  707. `static_files` was added to simplify serving of static files as well
  708. as `passthrough_errors`.
  709. .. versionadded:: 0.6
  710. support for SSL was added.
  711. .. versionadded:: 0.8
  712. Added support for automatically loading a SSL context from certificate
  713. file and private key.
  714. .. versionadded:: 0.9
  715. Added command-line interface.
  716. .. versionadded:: 0.10
  717. Improved the reloader and added support for changing the backend
  718. through the `reloader_type` parameter. See :ref:`reloader`
  719. for more information.
  720. .. versionchanged:: 0.15
  721. Bind to a Unix socket by passing a path that starts with
  722. ``unix://`` as the ``hostname``.
  723. :param hostname: The host to bind to, for example ``'localhost'``.
  724. If the value is a path that starts with ``unix://`` it will bind
  725. to a Unix socket instead of a TCP socket..
  726. :param port: The port for the server. eg: ``8080``
  727. :param application: the WSGI application to execute
  728. :param use_reloader: should the server automatically restart the python
  729. process if modules were changed?
  730. :param use_debugger: should the werkzeug debugging system be used?
  731. :param use_evalex: should the exception evaluation feature be enabled?
  732. :param extra_files: a list of files the reloader should watch
  733. additionally to the modules. For example configuration
  734. files.
  735. :param reloader_interval: the interval for the reloader in seconds.
  736. :param reloader_type: the type of reloader to use. The default is
  737. auto detection. Valid values are ``'stat'`` and
  738. ``'watchdog'``. See :ref:`reloader` for more
  739. information.
  740. :param threaded: should the process handle each request in a separate
  741. thread?
  742. :param processes: if greater than 1 then handle each request in a new process
  743. up to this maximum number of concurrent processes.
  744. :param request_handler: optional parameter that can be used to replace
  745. the default one. You can use this to replace it
  746. with a different
  747. :class:`~BaseHTTPServer.BaseHTTPRequestHandler`
  748. subclass.
  749. :param static_files: a list or dict of paths for static files. This works
  750. exactly like :class:`SharedDataMiddleware`, it's actually
  751. just wrapping the application in that middleware before
  752. serving.
  753. :param passthrough_errors: set this to `True` to disable the error catching.
  754. This means that the server will die on errors but
  755. it can be useful to hook debuggers in (pdb etc.)
  756. :param ssl_context: an SSL context for the connection. Either an
  757. :class:`ssl.SSLContext`, a tuple in the form
  758. ``(cert_file, pkey_file)``, the string ``'adhoc'`` if
  759. the server should automatically create one, or ``None``
  760. to disable SSL (which is the default).
  761. """
  762. if not isinstance(port, int):
  763. raise TypeError("port must be an integer")
  764. if use_debugger:
  765. from .debug import DebuggedApplication
  766. application = DebuggedApplication(application, use_evalex)
  767. if static_files:
  768. from .middleware.shared_data import SharedDataMiddleware
  769. application = SharedDataMiddleware(application, static_files)
  770. def log_startup(sock):
  771. display_hostname = hostname if hostname not in ("", "*") else "localhost"
  772. quit_msg = "(Press CTRL+C to quit)"
  773. if sock.family == af_unix:
  774. _log("info", " * Running on %s %s", display_hostname, quit_msg)
  775. else:
  776. if ":" in display_hostname:
  777. display_hostname = "[%s]" % display_hostname
  778. port = sock.getsockname()[1]
  779. _log(
  780. "info",
  781. " * Running on %s://%s:%d/ %s",
  782. "http" if ssl_context is None else "https",
  783. display_hostname,
  784. port,
  785. quit_msg,
  786. )
  787. def inner():
  788. try:
  789. fd = 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. if hasattr(s, "set_inheritable"):
  826. s.set_inheritable(True)
  827. # If we can open the socket by file descriptor, then we can just
  828. # reuse this one and our socket will survive the restarts.
  829. if can_open_by_fd:
  830. os.environ["WERKZEUG_SERVER_FD"] = str(s.fileno())
  831. s.listen(LISTEN_QUEUE)
  832. log_startup(s)
  833. else:
  834. s.close()
  835. if address_family == af_unix:
  836. _log("info", "Unlinking %s" % server_address)
  837. os.unlink(server_address)
  838. # Do not use relative imports, otherwise "python -m werkzeug.serving"
  839. # breaks.
  840. from ._reloader import run_with_reloader
  841. run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
  842. else:
  843. inner()
  844. def run_with_reloader(*args, **kwargs):
  845. # People keep using undocumented APIs. Do not use this function
  846. # please, we do not guarantee that it continues working.
  847. from ._reloader import run_with_reloader
  848. return run_with_reloader(*args, **kwargs)
  849. def main():
  850. """A simple command-line interface for :py:func:`run_simple`."""
  851. # in contrast to argparse, this works at least under Python < 2.7
  852. import optparse
  853. from .utils import import_string
  854. parser = optparse.OptionParser(usage="Usage: %prog [options] app_module:app_object")
  855. parser.add_option(
  856. "-b",
  857. "--bind",
  858. dest="address",
  859. help="The hostname:port the app should listen on.",
  860. )
  861. parser.add_option(
  862. "-d",
  863. "--debug",
  864. dest="use_debugger",
  865. action="store_true",
  866. default=False,
  867. help="Use Werkzeug's debugger.",
  868. )
  869. parser.add_option(
  870. "-r",
  871. "--reload",
  872. dest="use_reloader",
  873. action="store_true",
  874. default=False,
  875. help="Reload Python process if modules change.",
  876. )
  877. options, args = parser.parse_args()
  878. hostname, port = None, None
  879. if options.address:
  880. address = options.address.split(":")
  881. hostname = address[0]
  882. if len(address) > 1:
  883. port = address[1]
  884. if len(args) != 1:
  885. sys.stdout.write("No application supplied, or too much. See --help\n")
  886. sys.exit(1)
  887. app = import_string(args[0])
  888. run_simple(
  889. hostname=(hostname or "127.0.0.1"),
  890. port=int(port or 5000),
  891. application=app,
  892. use_reloader=options.use_reloader,
  893. use_debugger=options.use_debugger,
  894. )
  895. if __name__ == "__main__":
  896. main()