gevent.wsgi – Backwards compatibility alias for gevent.pywsgi

In the past, this used libevent’s http support, but that was dropped with the introduction of libev. libevent’s http support had several limitations, including not supporting stream, not supporting pipelining, and not supporting SSL.

Deprecated since version 1.1: Use gevent.pywsgi

class WSGIServer(listener, application=None, backlog=None, spawn='default', log='default', error_log='default', handler_class=None, environ=None, **ssl_args)

Bases: gevent.server.StreamServer

A WSGI server based on StreamServer that supports HTTPS.

Parameters:
  • log – If given, an object with a write method to which request (access) logs will be written. If not given, defaults to sys.stderr. You may pass None to disable request logging. You may use a wrapper, around e.g., logging, to support objects that don’t implement a write method. (If you pass a Logger instance, or in general something that provides a log method but not a write method, such a wrapper will automatically be created and it will be logged to at the INFO level.)
  • error_log – If given, a file-like object with write, writelines and flush methods to which error logs will be written. If not given, defaults to sys.stderr. You may pass None to disable error logging (not recommended). You may use a wrapper, around e.g., logging, to support objects that don’t implement the proper methods. This parameter will become the value for wsgi.errors in the WSGI environment (if not already set). (As with log, wrappers for Logger instances and the like will be created automatically and logged to at the ERROR level.)

See also

LoggingLogAdapter
See important warnings before attempting to use logging.

Changed in version 1.1a3: Added the error_log parameter, and set wsgi.errors in the WSGI environment to this value.

Changed in version 1.1a3: Add support for passing logging.Logger objects to the log and error_log arguments.

handler_class

alias of WSGIHandler

update_environ()

Called before the first request is handled to fill in WSGI environment values.

This includes getting the correct server name and port.

handle(socket, address)

Create an instance of handler_class to handle the request.

This method blocks until the handler returns.

class WSGIHandler(socket, address, server, rfile=None)

Bases: object

Handles HTTP requests from a socket, creates the WSGI environment, and interacts with the WSGI application.

This is the default value of WSGIServer.handler_class. This class may be subclassed carefully, and that class set on a WSGIServer instance through a keyword argument at construction time.

Instances are constructed with the same arguments as passed to the server’s WSGIServer.handle() method followed by the server itself. The application and environment are obtained from the server.

MessageClass

alias of Message

handle()

The main request handling method, called by the server.

This method runs a request handling loop, calling handle_one_request() until all requests on the connection have been handled (that is, it implements keep-alive).

read_request(raw_requestline)

Parse the incoming request.

Parses various headers into self.headers using MessageClass. Other attributes that are set upon a successful return of this method include self.content_length and self.close_connection.

Parameters:raw_requestline (str) – A native str representing the request line. A processed version of this will be stored into self.requestline.
Raises ValueError:
 If the request is invalid. This error will not be logged as a traceback (because it’s a client issue, not a server problem).
Returns:A boolean value indicating whether the request was successfully parsed. This method should either return a true value or have raised a ValueError with details about the parsing error.

Changed in version 1.1b6: Raise the previously documented ValueError in more cases instead of returning a false value; this allows subclasses more opportunity to customize behaviour.

read_requestline()

Read and return the HTTP request line.

Under both Python 2 and 3, this should return the native str type; under Python 3, this probably means the bytes read from the network need to be decoded (using the ISO-8859-1 charset, aka latin-1).

handle_one_request()

Handles one HTTP request using self.socket and self.rfile.

Each invocation of this method will do several things, including (but not limited to):

  • Read the request line using read_requestline();
  • Read the rest of the request, including headers, with read_request();
  • Construct a new WSGI environment in self.environ using get_environ();
  • Store the application in self.application, retrieving it from the server;
  • Handle the remainder of the request, including invoking the application, with handle_one_response()

There are several possible return values to indicate the state of the client connection:

  • None

    The client connection is already closed or should be closed because the WSGI application or client set the Connection: close header. The request handling loop should terminate and perform cleanup steps.

  • (status, body)

    An HTTP status and body tuple. The request was in error, as detailed by the status and body. The request handling loop should terminate, close the connection, and perform cleanup steps. Note that the body is the complete contents to send to the client, including all headers and the initial status line.

  • True

    The literal True value. The request was successfully handled and the response sent to the client by handle_one_response(). The connection remains open to process more requests and the connection handling loop should call this method again. This is the typical return value.

See also

handle()

Changed in version 1.1b6: Funnel exceptions having to do with invalid HTTP requests through _handle_client_error() to allow subclasses to customize. Note that this is experimental and may change in the future.

start_response(status, headers, exc_info=None)

Changed in version 1.1b5: Pro-actively handle checking the encoding of the status line and headers during this method. On Python 2, avoid some extra encodings.

get_environ()

Construct and return a new WSGI environment dictionary for a specific request.

This should begin with asking the server for the base environment using WSGIServer.get_environ(), and then proceed to add the request specific values.

By the time this method is invoked the request line and request shall have been parsed and self.headers shall be populated.

class LoggingLogAdapter(logger, level=20)

Bases: object

An adapter for logging.Logger instances to let them be used with WSGIServer.

Warning

Unless the entire process is monkey-patched at a very early part of the lifecycle (before logging is configured), loggers are likely to not be gevent-cooperative. For example, the socket and syslog handlers use the socket module in a way that can block, and most handlers acquire threading locks.

Warning

It may be possible for the logging functions to be called in the gevent.Hub greenlet. Code running in the hub greenlet cannot use any gevent blocking functions without triggering a LoopExit.

New in version 1.1a3.

Changed in version 1.1b6: Attributes not present on this object are proxied to the underlying logger instance. This permits using custom Logger subclasses (or indeed, even duck-typed objects).

Changed in version 1.1: Strip trailing newline characters on the message passed to write() because log handlers will usually add one themselves.

Write information to the logger at the given level (default to INFO).

flush()

No-op; required to be a file-like object

Next page: Name Resolution (DNS)