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.WebTestHttpd(host='127.0.0.1', port=8000, server_cls=None, handler_cls=<class wptserve.server.WebTestRequestHandler>, use_ssl=False, key_file=None, certificate=None, encrypt_after_connect=False, router_cls=<class 'wptserve.router.Router'>, doc_root='.', routes=None, rewriter_cls=<class 'wptserve.server.RequestRewriter'>, bind_hostname=True, rewrites=None, latency=None, config=None)[source]
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
  • 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_hostname – Boolean indicating whether to bind server to hostname.
  • latency – Delay in ms to wait before seving 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 indictaing whether the server is running

start(block=False)[source]

Start the server.

Parameters:block – True to run the server on the current thread, blocking, False to run on a separate thread.
stop()[source]

Stops the server.

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

class wptserve.server.WebTestRequestHandler(request, client_address, server)[source]

RequestHandler for WebTestHttpd