我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用wsgiref.simple_server.ServerHandler()。
def handle(self): """Copy of WSGIRequestHandler, but with different ServerHandler""" self.raw_requestline = self.rfile.readline(65537) if len(self.raw_requestline) > 65536: self.requestline = '' self.request_version = '' self.command = '' self.send_error(414) return if not self.parse_request(): # An error code has been sent, just exit return handler = ServerHandler( self.rfile, self.wfile, self.get_stderr(), self.get_environ() ) handler.request_handler = self # backpointer for logging handler.run(self.server.get_app())
def finish_response(self): # If the browser closes the connection while we still want to sen stuff back, # we want to fail silently and give up. This often happens in tests where the # browser may want to request embedded links (like stylesheets) too, yet the # test has already clicked on the next link. if six.PY3: ssl_eof_error = ssl.SSLEOFError broken_pipe_error = BrokenPipeError else: ssl_eof_error = ssl.SSLError broken_pipe_error = socket.error try: ServerHandler.finish_response(self) except (ssl_eof_error, broken_pipe_error): # Silently ignore it if it looks like the client browser closed the connection. pass
def handle_error(self): # Ignore broken pipe errors, otherwise pass on if not is_broken_pipe_error(): super(ServerHandler, self).handle_error()
def handle(self): # A modified copy of simple_server.WSGIRequestHandler.handle # # We first check if there really IS something to read on the socket # because as soon as ReahlWSGIServer.connection_is_pending becomes # true, A SingleWSGIRequestHandler is created already. However, the # fact that the browser made a connection does not mean it has sent # HTTP requests yet. So, we cannot really serve yet. # # We first check whether something has actually arrived here by # temporarily setting a timeout on the socket to read the first byte. # We suspect that the browser connects eagerly (pre-connect) before it # has a request to send in order to gain a speed benefit. # # We also use this override to use our PatchedServerHandler instead of # ServerHandler to handle HTTP requests. try: self.request.settimeout(0.1) self.raw_requestline = self.rfile.read(1) except (socket.timeout, ssl.SSLError): return finally: self.request.settimeout(None) self.raw_requestline = self.raw_requestline + self.rfile.readline(65536) if len(self.raw_requestline) > 65536: self.requestline = '' self.request_version = '' self.command = '' self.send_error(414) return if not self.parse_request(): # An error code has been sent, just exit return handler = PatchedServerHandler( self.rfile, self.wfile, self.get_stderr(), self.get_environ() ) handler.request_handler = self # backpointer for logging handler.run(self.server.get_app())
def error_output(self, environ, start_response): super(ServerHandler, self).error_output(environ, start_response) return ['\n'.join(traceback.format_exception(*sys.exc_info()))] # Backport of http://hg.python.org/cpython/rev/d5af1b235dab. See #16241. # This can be removed when support for Python <= 2.7.3 is deprecated.