Server

Basic server classes and router.

The following example creates a server that serves static files from the files subdirectory of the current directory and causes it to run on port 8080 until it is killed:

from wptserve import server, handlers

httpd = server.WebTestHttpd(port=8080, doc_root="./files/",
                            routes=[("GET", "*", handlers.file_handler)])
httpd.start(block=True)

Interface

class wptserve.server.BaseWebTestRequestHandler(*args, **kwargs)

RequestHandler for WebTestHttpd

class wptserve.server.H2ConnectionGuard(obj)

H2Connection objects are not threadsafe, so this keeps thread safety

class wptserve.server.H2Headers(headers)
class wptserve.server.Http1WebTestRequestHandler(*args, **kwargs)
handle_one_request()

Handle a single HTTP request.

You normally don’t need to override this method; see the class __doc__ string for information on how to handle specific HTTP commands such as GET and POST.

class wptserve.server.Http2WebTestRequestHandler(*args, **kwargs)
handle_one_request()

This is the main HTTP/2.0 Handler.

When a browser opens a connection to the server on the HTTP/2.0 port, the server enters this which will initiate the h2 connection and keep running throughout the duration of the interaction, and will read/write directly from the socket.

Because there can be multiple H2 connections active at the same time, a UUID is created for each so that it is easier to tell them apart in the logs.

start_stream_thread(frame, queue)

This starts a new thread to handle frames for a specific stream. :param frame: The first frame on the stream :param queue: A queue object that the thread will use to check for new frames :return: The thread object that has already been started

class wptserve.server.WebTestHttpd(host='127.0.0.1', port=8000, server_cls=None, handler_cls=<class 'wptserve.server.Http1WebTestRequestHandler'>, use_ssl=False, key_file=None, certificate=None, encrypt_after_connect=False, router_cls=<class 'wptserve.router.Router'>, doc_root='.', ws_doc_root=None, routes=None, rewriter_cls=<class 'wptserve.server.RequestRewriter'>, bind_address=True, rewrites=None, latency=None, config=None, http2=False)
Parameters:
  • host – Host from which to serve (default: 127.0.0.1)

  • port – Port from which to serve (default: 8000)

  • server_cls – Class to use for the server (default depends on ssl vs non-ssl)

  • handler_cls – Class to use for the RequestHandler

  • use_ssl – Use a SSL server if no explicit server_cls is supplied

  • key_file – Path to key file to use if ssl is enabled

  • certificate – Path to certificate file to use if ssl is enabled

  • encrypt_after_connect – For each connection, don’t start encryption until a CONNECT message has been received. This enables the server to act as a self-proxy.

  • router_cls – Router class to use when matching URLs to handlers

  • doc_root – Document root for serving files

  • ws_doc_root – Document root for websockets

  • routes – List of routes with which to initialize the router

  • rewriter_cls – Class to use for request rewriter

  • rewrites – List of rewrites with which to initialize the rewriter_cls

  • config – Dictionary holding environment configuration settings for handlers to read, or None to use the default values.

  • bind_address – Boolean indicating whether to bind server to IP address.

  • latency – Delay in ms to wait before serving each response, or callable that returns a delay in ms

HTTP server designed for testing scenarios.

Takes a router class which provides one method get_handler which takes a Request and returns a handler function.

host

The host name or ip address of the server

port

The port on which the server is running

router

The Router object used to associate requests with resources for this server

rewriter

The Rewriter object used for URL rewriting

use_ssl

Boolean indicating whether the server is using ssl

started

Boolean indicating whether the server is running

start()

Start the server.

Parameters:

block – True to run the server on the current thread, blocking, False to run on a separate thread.

stop()

Stops the server.

If the server is not running, this method has no effect.

class wptserve.server.WebTestServer(server_address, request_handler_cls, router, rewriter, bind_address, ws_doc_root=None, config=None, use_ssl=False, key_file=None, certificate=None, encrypt_after_connect=False, latency=None, http2=False, **kwargs)
finish_request(request, client_address)

Finish one request by instantiating RequestHandlerClass.

handle_error(request, client_address)

Handle an error gracefully. May be overridden.

The default is to print a traceback and continue.