annotate MoinMoin/support/werkzeug/serving.py @ 6129:7f12cf241d5e

update werkzeug to 0.12.1, update CHANGES
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 11 Apr 2017 22:42:23 +0200
parents 9f12f41504fc
children df65dcf7daea
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):
04c4fdfe48a9 updated to Werkzeug 0.5 release
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4627
diff changeset
282 return self.client_address[0]
04c4fdfe48a9 updated to Werkzeug 0.5 release
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4627
diff changeset
283
6094
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
284 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
285 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
286
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
287 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
288 msg = self.requestline
7f12cf241d5e update werkzeug to 0.12.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6094
diff changeset
289 code = str(code)
7f12cf241d5e update werkzeug to 0.12.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6094
diff changeset
290
7f12cf241d5e update werkzeug to 0.12.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6094
diff changeset
291 if termcolor:
7f12cf241d5e update werkzeug to 0.12.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6094
diff changeset
292 color = termcolor.colored
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 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
295 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
296 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
297 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
298 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
299 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
300 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
301 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
302 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
303 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
304 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
305 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
306 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
307 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
308
7f12cf241d5e update werkzeug to 0.12.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6094
diff changeset
309 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
310
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
311 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
312 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
313
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_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
315 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
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(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
318 _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
319 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
320 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
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
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 #: 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
324 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
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
6094
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
327 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
328 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
329 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
330
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
331 # 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
332 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
333 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
334
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
335 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
336 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
337 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
338 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
339
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 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
341 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
342 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
343
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
344 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
345 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
346 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
347
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 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
349 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
350 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
351 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
352
6094
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
353 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
354
6094
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
355
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
356 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
357 """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
358 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
359 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
360 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
361 ``*.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
362
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
363 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
364
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
365 .. 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
366
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
367 :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
368 ``.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
369 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
370 :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
371 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
372 :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
373 """
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
374 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
375 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
376 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
377 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
378
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
379 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
380 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
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 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
383 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
384 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
385 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
386
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
387 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
388
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 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
391 """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
392 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
393 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
394 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
395
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
396 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
397 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
398 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
399 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
400 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
401
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
402 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
403 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
404 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
405 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
406 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
407 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
408
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
409
6094
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
410 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
411 """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
412 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
413 :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
414
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
415 :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
416 :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
417 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
418 :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
419 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
420 """
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
421 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
422 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
423 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
424 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
425 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
426
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
427
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
428 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
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 '''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
431 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
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 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
434 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
435 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
436 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
437 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
438
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
439 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
440 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
441 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
442 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
443
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
444 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
445 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
446 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
447 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
448
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
449
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
450 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
451 """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
452 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
453 try:
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
454 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
455 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
456 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
457 pass
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
458
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
459 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
460 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
461 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
462
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
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
464 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
465 """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
466 # 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
467 # 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
468 # 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
469 # 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
470 # try:
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
471 # 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
472 # 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
473 # 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
474 # 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
475 # 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
476 # 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
477 # 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
478 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
479 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
480 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
481
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
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 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
484
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
485 """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
486 multithread = False
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
487 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
488 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
489
4619
9edfdbed12a8 werkzeug updated
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4614
diff changeset
490 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
491 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
492 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
493 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
494
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
495 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
496
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
497 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
498 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
499 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
500 port = 0
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
501 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
502 self.app = app
4619
9edfdbed12a8 werkzeug updated
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4614
diff changeset
503 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
504 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
505 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
506 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
507
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
508 # 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
509 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
510 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
511 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
512 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
513
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
514 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
515 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
516 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
517 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
518 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
519 # 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
520 # 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
521 # 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
522 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
523 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
524 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
525 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
526 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
527 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
528 self.ssl_context = None
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
529
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
530 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
531 _log(type, message, *args)
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 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
534 self.shutdown_signal = False
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
535 try:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
536 HTTPServer.serve_forever(self)
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
537 except KeyboardInterrupt:
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
538 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
539 finally:
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
540 self.server_close()
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
541
4623
16ee781ef3ad updated werkzeug
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4619
diff changeset
542 def handle_error(self, request, client_address):
16ee781ef3ad updated werkzeug
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4619
diff changeset
543 if self.passthrough_errors:
16ee781ef3ad updated werkzeug
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4619
diff changeset
544 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
545 return HTTPServer.handle_error(self, request, client_address)
4623
16ee781ef3ad updated werkzeug
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4619
diff changeset
546
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
547 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
548 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
549 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
550
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
551
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
552 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
553
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
554 """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
555 multithread = True
6129
7f12cf241d5e update werkzeug to 0.12.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6094
diff changeset
556 daemon_threads = True
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
557
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
558
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
559 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
560
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
561 """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
562 multiprocess = True
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
563
4619
9edfdbed12a8 werkzeug updated
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4614
diff changeset
564 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
565 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
566 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
567 raise ValueError('Your platform does not support forking.')
4619
9edfdbed12a8 werkzeug updated
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4614
diff changeset
568 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
569 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
570 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
571
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
572
6094
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
573 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
574 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
575 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
576 """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
577 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
578 """
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
579 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
580 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
581 "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
582 elif threaded:
4619
9edfdbed12a8 werkzeug updated
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4614
diff changeset
583 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
584 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
585 elif processes > 1:
4619
9edfdbed12a8 werkzeug updated
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4614
diff changeset
586 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
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 else:
4619
9edfdbed12a8 werkzeug updated
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4614
diff changeset
589 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
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
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
592
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 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
594 """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
595 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
596
6094
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
597 .. 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
598 """
6094
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
599 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
600
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
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
602 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
603 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
604 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
605 reloader_type='auto', threaded=False,
4619
9edfdbed12a8 werkzeug updated
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4614
diff changeset
606 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
607 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
608 """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
609 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
610
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
611 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
612
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
613 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
614
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
615 .. versionadded:: 0.5
4619
9edfdbed12a8 werkzeug updated
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4614
diff changeset
616 `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
617 as `passthrough_errors`.
4609
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
618
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
619 .. 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
620 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
621
6094
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
622 .. 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
623 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
624 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
625
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
626 .. 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
627 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
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.10
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
630 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
631 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
632 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
633
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
634 :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
635 :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
636 :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
637 :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
638 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
639 :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
640 :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
641 :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
642 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
643 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
644 :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
645 :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
646 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
647 ``'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
648 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
649 :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
650 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
651 :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
652 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
653 :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
654 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
655 with a different
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
656 :class:`~BaseHTTPServer.BaseHTTPRequestHandler`
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
657 subclass.
246ba4eecab2 updated werkzeug to 0.5.pre20090228
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4301
diff changeset
658 :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
659 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
660 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
661 serving.
4623
16ee781ef3ad updated werkzeug
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4619
diff changeset
662 :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
663 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
664 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
665 :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
666 :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
667 ``(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
668 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
669 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
670 """
c689dfa55de1 Import werkzeug 0.3.1 package into MoinMoin.support (still need to insert CREDITS)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
671 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
672 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
673 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
674 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
675 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
676 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
677
6094
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
678 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
679 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
680 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
681 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
682 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
683 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
684 _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
685 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
686 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
687
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
688 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
689 try:
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
690 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
691 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
692 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
693 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
694 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
695 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
696 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
697 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
698 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
699 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
700
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
701 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
702 # 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
703 # 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
704 # 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
705 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
706 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
707 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
708 '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
709 '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
710
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
711 # 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
712 # 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
713 # 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
714 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
715 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
716 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
717 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
718 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
719 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
720
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 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
722 # 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
723 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
724 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
725 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
726 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
727 else:
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.close()
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
729
6129
7f12cf241d5e update werkzeug to 0.12.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6094
diff changeset
730 # 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
731 # breaks.
7f12cf241d5e update werkzeug to 0.12.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6094
diff changeset
732 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
733 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
734 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
735 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
736 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
737
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
738
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
739 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
740 # 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
741 # 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
742 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
743 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
744
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
745
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
746 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
747 '''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
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 # 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
750 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
751 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
752
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
753 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
754 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
755 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
756 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
757 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
758 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
759 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
760 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
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='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
763 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
764
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
765 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
766 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
767 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
768 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
769 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
770 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
771
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(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
773 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
774 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
775 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
776
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
777 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
778 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
779 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
780 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
781 )
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
782
9f12f41504fc upgrade werkzeug from 0.8.3 to 0.11.11
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6010
diff changeset
783 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
784 main()