gevent.pywsgi – A pure-Python, gevent-friendly WSGI server

The server is provided in WSGIServer, but most of the actual WSGI work is handled by WSGIHandler — a new instance is created for each request. The server can be customized to use different subclasses of WSGIHandler.

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

Bases: object

MessageClass

alias of Message

protocol_version = 'HTTP/1.1'
handle()

The main request handling method, called by the server.

This method runs until all requests on the connection have been handled (that is, it implements pipelining).

read_request(raw_requestline)

Process the incoming request. Parse various headers.

Raises:ValueError – If the request is invalid. This error will not be logged (because it’s a client issue, not a server problem).
log_error(msg, *args)
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()
finalize_headers()
write(data)
start_response(status, headers, exc_info=None)
log_request()
format_request()
process_result()
run_application()
handle_one_response()
handle_error(type, value, tb)
get_environ()
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 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 logging.Logger instance, it will be logged to at the logging.INFO level.)
  • error_log – If given, a file-like object with a write method 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 a write method. (If you pass a logging.Logger instance, it will be logged to at the logging.ERROR level.)

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.

base_env = {'wsgi.version': (1, 0), 'GATEWAY_INTERFACE': 'CGI/1.1', 'wsgi.run_once': False, 'wsgi.multiprocess': False, 'SERVER_SOFTWARE': 'gevent/1.1 Python/2.7', 'SCRIPT_NAME': '', 'wsgi.multithread': False}
error_log = None

The object to which error logs will be written. It will never be None.

handler_class

alias of WSGIHandler

log = None

The object to which request logs will be written. It will never be None.

set_environ(environ=None)
set_max_accept()
get_environ()
init_socket()
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 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.

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

write(msg)
flush()

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

writelines(lines)

Next page: gevent.wsgi – Backwards compatibility alias for gevent.pywsgi