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 -def launch_server_wsgiref(host, port, app):
    27      """use python's builtin simple_server, this is a last resort since
    28      it doesn't support concurrency at all"""
    29      from wsgiref import simple_server
    30      class CustomRequestHandler(simple_server.WSGIRequestHandler):
    31          """Custom request handler, disables some inefficient defaults"""
    32
    33          def address_string(self):
    34              """Disable client reverse dns lookup."""
    35              return  self.client_address[0]
    36
    37          def log_error(self, format, *args):
    38              """Log errors using logging instead of printing to
    39              stderror"""
    40              logging.error("%s - - [%s] %s",
    41                            self.address_string(), self.log_date_time_string(), format % args)
    42
    43          def log_message(self, format, *args):
    44              """Log requests using logging instead of printing to
    45              stderror."""
    46              logging.info("%s - - [%s] %s",
    47                           self.address_string(),  self.log_date_time_string(), format % args)
    48
    49      server = simple_server.make_server(host, port, app, handler_class=CustomRequestHandler)
    50      logging.info("Starting wsgiref server, listening on port %s", port)
    51      server.serve_forever()
    52
    53
    54 -def launch_server_django(host, port, app):
    55      """use django's development server, only works for django apps"""
    56      if 'DJANGO_SETTINGS_MODULE' not in os.environ:
    57          raise ImportError("no django settings module specified")
    58
    59      from django.core.servers.basehttp import  run
    60
    61      logging.info("Starting Django server, listening on port %s", port)
    62      run(host, port, app)
    63
    64
    65 -def launch_server_cherrypy(host, port, app):
    66      """use cherrypy's wsgiserver, a multithreaded scallable server"""
    67      from cherrypy.wsgiserver import  CherryPyWSGIServer
    68
    69      server = CherryPyWSGIServer((host, port), app)
    70      logging.info("Starting CherryPy server, listening on port %s", port)
    71      try:
    72          server.start()
    73      except KeyboardInterrupt:
    74          server.stop()
    75
    76  #FIXME: implement threading http server based on BaseHTTPServer and wsgiref
    77
    78  servers = [launch_server_cherrypy, launch_server_django, launch_server_wsgiref]
    79
    80 -def launch_server(host, port, app):
    81      """use the best possible wsgi server"""
    82      for server in servers:
    83          try:
    84              server(host, port, app)
    85              break
    86          except ImportError:
    87              pass
    88