| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
class Middleware(object): |
|---|
| 6 |
def __init__(self, middleware): |
|---|
| 7 |
self.middleware = middleware |
|---|
| 8 |
|
|---|
| 9 |
def __call__(self, **conf): |
|---|
| 10 |
return lambda app: self.middleware(app, **conf) |
|---|
| 11 |
|
|---|
| 12 |
class ChangeCase(object): |
|---|
| 13 |
def __init__(self, app, to=None): |
|---|
| 14 |
self.app = app |
|---|
| 15 |
self.to = to |
|---|
| 16 |
|
|---|
| 17 |
def __call__(self, environ, start_response): |
|---|
| 18 |
response_iter = self.app(environ, start_response) |
|---|
| 19 |
response_string = ''.join(response_iter) |
|---|
| 20 |
if self.to == "upper": |
|---|
| 21 |
return [response_string.upper()] |
|---|
| 22 |
elif self.to == "lower": |
|---|
| 23 |
return [response_string.lower()] |
|---|
| 24 |
else: |
|---|
| 25 |
return [response_string] |
|---|
| 26 |
|
|---|
| 27 |
import cherrypy |
|---|
| 28 |
|
|---|
| 29 |
class Root(object): |
|---|
| 30 |
def index(self): |
|---|
| 31 |
dumb = 50 |
|---|
| 32 |
return "HeLlO WoRlD!" |
|---|
| 33 |
index.exposed = True |
|---|
| 34 |
|
|---|
| 35 |
global_conf = { |
|---|
| 36 |
'log.screen': True, |
|---|
| 37 |
'request.throw_errors': True, |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
app_conf = { |
|---|
| 43 |
'/': { |
|---|
| 44 |
'wsgi.middleware_pipeline': ['changecase'], |
|---|
| 45 |
'wsgi.changecase.to': 'lower', |
|---|
| 46 |
} |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
cherrypy.wsgi.changecase = Middleware(ChangeCase) |
|---|
| 51 |
|
|---|
| 52 |
cherrypy.config.update(global_conf) |
|---|
| 53 |
cherrypy.quickstart(Root(), script_name='/', conf=app_conf) |
|---|