"Fossies" - the Fresh Open Source Software Archive

Member "PURELIB/trac/web/cgi_frontend.py" (27 Aug 2019, 2322 Bytes) of package /windows/misc/Trac-1.4.win-amd64.exe:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "cgi_frontend.py": 1.3.5_vs_1.3.6.

    1 #!/usr/bin/env python
    2 # -*- coding: utf-8 -*-
    3 #
    4 # Copyright (C) 2005-2019 Edgewall Software
    5 # Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de>
    6 # Copyright (C) 2005 Matthew Good <trac@matt-good.net>
    7 # All rights reserved.
    8 #
    9 # This software is licensed as described in the file COPYING, which
   10 # you should have received as part of this distribution. The terms
   11 # are also available at https://trac.edgewall.org/wiki/TracLicense.
   12 #
   13 # This software consists of voluntary contributions made by many
   14 # individuals. For the exact contribution history, see the revision
   15 # history and logs, available at https://trac.edgewall.org/log/.
   16 #
   17 # Author: Christopher Lenz <cmlenz@gmx.de>
   18 #         Matthew Good <trac@matt-good.net>
   19 
   20 import os
   21 import pkg_resources
   22 import sys
   23 
   24 from trac import __version__ as VERSION
   25 from trac.web.main import dispatch_request
   26 from trac.web.wsgi import WSGIGateway
   27 
   28 
   29 class CGIGateway(WSGIGateway):
   30 
   31     wsgi_multithread = False
   32     wsgi_multiprocess = False
   33     wsgi_run_once = True
   34 
   35     def __init__(self):
   36         WSGIGateway.__init__(self, dict(os.environ))
   37 
   38     def _write(self, data):
   39         assert self.headers_set, 'Response not started'
   40 
   41         if not self.headers_sent:
   42             status, headers = self.headers_sent = self.headers_set
   43             sys.stdout.write('Status: %s\r\n' % status)
   44             for header in headers:
   45                 sys.stdout.write('%s: %s\r\n' % header)
   46             sys.stdout.write('\r\n')
   47             sys.stdout.flush()
   48 
   49         sys.stdout.write(data)
   50         sys.stdout.flush()
   51 
   52 
   53 def run():
   54     try: # Make FreeBSD use blocking I/O like other platforms
   55         import fcntl
   56         for stream in [sys.stdin, sys.stdout]:
   57             fd = stream.fileno()
   58             flags = fcntl.fcntl(fd, fcntl.F_GETFL)
   59             fcntl.fcntl(fd, fcntl.F_SETFL, flags & ~os.O_NONBLOCK)
   60     except (ImportError, AttributeError):
   61         pass
   62 
   63     try: # Use binary I/O on Windows
   64         import msvcrt
   65         msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
   66         msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
   67     except ImportError:
   68         pass
   69 
   70     gateway = CGIGateway()
   71     gateway.run(dispatch_request)
   72 
   73 if __name__ == '__main__':
   74     pkg_resources.require('Trac==%s' % VERSION)
   75     run()