annotate MoinMoin/support/werkzeug/serving.py @ 6132:df65dcf7daea

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