Manage the invocation of a WSGI application
| 93 | |
| 94 | |
| 95 | class BaseHandler: |
| 96 | """Manage the invocation of a WSGI application""" |
| 97 | |
| 98 | # Configuration parameters; can override per-subclass or per-instance |
| 99 | wsgi_version = (1,0) |
| 100 | wsgi_multithread = True |
| 101 | wsgi_multiprocess = True |
| 102 | wsgi_run_once = False |
| 103 | |
| 104 | origin_server = True # We are transmitting direct to client |
| 105 | http_version = "1.0" # Version that should be used for response |
| 106 | server_software = None # String name of server software, if any |
| 107 | |
| 108 | # os_environ is used to supply configuration from the OS environment: |
| 109 | # by default it's a copy of 'os.environ' as of import time, but you can |
| 110 | # override this in e.g. your __init__ method. |
| 111 | os_environ= read_environ() |
| 112 | |
| 113 | # Collaborator classes |
| 114 | wsgi_file_wrapper = FileWrapper # set to None to disable |
| 115 | headers_class = Headers # must be a Headers-like class |
| 116 | |
| 117 | # Error handling (also per-subclass or per-instance) |
| 118 | traceback_limit = None # Print entire traceback to self.get_stderr() |
| 119 | error_status = "500 Internal Server Error" |
| 120 | error_headers = [('Content-Type','text/plain')] |
| 121 | error_body = b"A server error occurred. Please contact the administrator." |
| 122 | |
| 123 | # State variables (don't mess with these) |
| 124 | status = result = None |
| 125 | headers_sent = False |
| 126 | headers = None |
| 127 | bytes_sent = 0 |
| 128 | |
| 129 | def run(self, application): |
| 130 | """Invoke the application""" |
| 131 | # Note to self: don't move the close()! Asynchronous servers shouldn't |
| 132 | # call close() from finish_response(), so if you close() anywhere but |
| 133 | # the double-error branch here, you'll break asynchronous servers by |
| 134 | # prematurely closing. Async servers must return from 'run()' without |
| 135 | # closing if there might still be output to iterate over. |
| 136 | try: |
| 137 | self.setup_environ() |
| 138 | self.result = application(self.environ, self.start_response) |
| 139 | self.finish_response() |
| 140 | except (ConnectionAbortedError, BrokenPipeError, ConnectionResetError): |
| 141 | # We expect the client to close the connection abruptly from time |
| 142 | # to time. |
| 143 | return |
| 144 | except: |
| 145 | try: |
| 146 | self.handle_error() |
| 147 | except: |
| 148 | # If we get an error handling an error, just give up already! |
| 149 | self.close() |
| 150 | raise # ...and let the actual server figure it out. |
| 151 | |
| 152 |
searching dependent graphs…