Package translate :: Package misc :: Module wsgi
[hide private]
[frames] | no frames]

Source Code for Module translate.misc.wsgi

 1  #!/usr/bin/env python 
 2  # -*- coding: utf-8 -*- 
 3  # 
 4  # Copyright 2008-2009 Zuza Software Foundation 
 5  # 
 6  # This file is part of translate. 
 7  # 
 8  # This program is free software; you can redistribute it and/or modify 
 9  # it under the terms of the GNU General Public License as published by 
10  # the Free Software Foundation; either version 2 of the License, or 
11  # (at your option) any later version. 
12  # 
13  # This program is distributed in the hope that it will be useful, 
14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
16  # GNU General Public License for more details. 
17  # 
18  # You should have received a copy of the GNU General Public License 
19  # along with this program; if not, see <http://www.gnu.org/licenses/>. 
20   
21  """wrapper tht tries to pick the best available wsgi server""" 
22   
23  import logging 
24  import os 
25   
26   
27 -def launch_server_wsgiref(host, port, app):
28 """use python's builtin simple_server, this is a last resort since 29 it doesn't support concurrency at all""" 30 from wsgiref import simple_server 31 32 class CustomRequestHandler(simple_server.WSGIRequestHandler): 33 """Custom request handler, disables some inefficient defaults""" 34 35 def address_string(self): 36 """Disable client reverse dns lookup.""" 37 return self.client_address[0]
38 39 def log_error(self, format, *args): 40 """Log errors using logging instead of printing to 41 stderror""" 42 logging.error("%s - - [%s] %s", 43 self.address_string(), self.log_date_time_string(), format % args) 44 45 def log_message(self, format, *args): 46 """Log requests using logging instead of printing to 47 stderror.""" 48 logging.info("%s - - [%s] %s", 49 self.address_string(), self.log_date_time_string(), format % args) 50 51 server = simple_server.make_server(host, port, app, handler_class=CustomRequestHandler) 52 logging.info("Starting wsgiref server, listening on port %s", port) 53 server.serve_forever() 54 55
56 -def launch_server_django(host, port, app):
57 """use django's development server, only works for django apps""" 58 if 'DJANGO_SETTINGS_MODULE' not in os.environ: 59 raise ImportError("no django settings module specified") 60 61 from django.core.servers.basehttp import run 62 63 logging.info("Starting Django server, listening on port %s", port) 64 run(host, port, app)
65 66
67 -def launch_server_cherrypy(host, port, app):
68 """use cherrypy's wsgiserver, a multithreaded scallable server""" 69 from cherrypy.wsgiserver import CherryPyWSGIServer 70 71 server = CherryPyWSGIServer((host, port), app) 72 logging.info("Starting CherryPy server, listening on port %s", port) 73 try: 74 server.start() 75 except KeyboardInterrupt: 76 server.stop()
77
78 -def launch_server_werkzeug(host, port, app):
79 """use werkzeug's simple multiprocess multithreaded server""" 80 from werkzeug import run_simple 81 logging.info("Starting Werkzeug server, listening on port %s", port) 82 run_simple(host, port, app, threaded=True)
83 84 85 #FIXME: implement threading http server based on BaseHTTPServer and wsgiref 86 87 servers = [launch_server_cherrypy, launch_server_werkzeug, launch_server_django, launch_server_wsgiref] 88 89
90 -def launch_server(host, port, app):
91 """use the best possible wsgi server""" 92 for server in servers: 93 try: 94 server(host, port, app) 95 break 96 except ImportError: 97 pass
98