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)
- 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 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)¶
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)¶
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.
The host name or ip address of the server
The port on which the server is running
The Router object used to associate requests with resources for this server
The Rewriter object used for URL rewriting
Boolean indicating whether the server is using ssl
Boolean indicating whether the server is running
Start the server.
block – True to run the server on the current thread, blocking, False to run on a separate thread.
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)¶
- handle_error(request, client_address)¶
Handle an error gracefully. May be overridden.
The default is to print a traceback and continue.