API

Servers

The core server class, BaseServer, handles the core functionality like TCP socket creation, accepting connections, and making calls to a RequestHandler.

class pyhttp.BaseServer(host='localhost', port=8888, request_handler=<class 'pyhttp.requests.BaseHttpRequestHandler'>)[source]

Basic class for a simple http server. Accepts client connections and passes off to a RequestHandler class to for processing and responding.

Parameters:
  • host (str) – Host address to listen on
  • port (int) – Port to listen on
BaseServer.serve_forever()[source]

Multi-Threading Support

Multi-threaded support is implemented through the Mixin pattern in Python. The ThreadedingMixin class overrides methods in BaseServer to allow for handling client requests in a separate thread. This keeps the ThreadedServer implementation as simple as declaring a class which first inherits from ThreadingMixin.

class pyhttp.ThreadingMixin[source]

A threading mixing class that overrides the process_request() function. Rather than immediately calling the request handler class, it creates a processing thread whose target function is threaded_process_request(). Also keeps track of all threads created and waits for them to finish before closing the connection.

ThreadingMixin.process_request(conn, addr)[source]

Overrides process_request() in BaseServer. Passes arguments to a function to be run in separate thread. Stores the thread in the class attribute list _threads.

Parameters:
  • conn – Open client socket
  • addr – Client socket address
ThreadingMixin.close()[source]

Calls BaseServer.close(). Joins threads before shutting down.

class pyhttp.ThreadedServer(host='localhost', port=8888, request_handler=<class 'pyhttp.requests.BaseHttpRequestHandler'>)[source]

Uses the basic functionality and call api of BaseServer. Overrides the initial processing of a request to put it into a thread. For details see ThreadingMixin

Request Handling

class pyhttp.Request(conn)[source]

A simple class to encapsulate reading and parsing a raw HTTP request string. It takes a client socket connection and stores the http command, resource path requested, and http version. It also stores a headers dictionary.

Parameters:conn – A client socket connection
class pyhttp.BaseHttpRequestHandler(conn, addr, server, directory=None)[source]

Base request handler. Parses connection into a Request object

Parameters:
  • conn – A client connection socket
  • addr – Client address
  • server – Server instance that accepted the client connection
  • directory – Base directory to serve files from. Defaults to current working directory
BaseHttpRequestHandler.handle()[source]

Dispatches appropriate method to handle a request. Could be serving a static file, directory listing, or running a CGI script.

BaseHttpRequestHandler.get_path(path)[source]

Takes the path specified in the request header and translates to an absolute path on the server’s filesystem. Doesn’t accept relative path markers like ‘.’ or ‘..’ currently.

Parameters:path – path from http request
Returns:absolute file path on the server’s FS
Return type:str
BaseHttpRequestHandler.list_dir(path)[source]

Takes an absolute path on the server’s filesystem and generates HTML which lists the path’s contents if it is a directory or file contents otherwise.

Parameters:path (str) – an absolute file system path
Returns:HTML string.
Return type:str
BaseHttpRequestHandler.do_file(path)[source]

Handles the response for all file types. Determines whether to serve file contents or if the path is a CGI script that should be executed.

Parameters:path (str) – An absolute filesystem path
BaseHttpRequestHandler.run_cgi(path)[source]

Executes a CGI script in a separate process and writes the output into the response buffer.

Parameters:path (str) – An absolute filesystem path
class pyhttp.requests.SocketWriter(sock)[source]

A simple wrapper around a socket to make interacting with client connections similar to doing file writes. Doesn’t do much, but modeled on Python’s SocketWriter used by the standard library’s Http server. This one doesn’t inherit from BufferedIOBase but just expects the caller sends appropriately encoded bytes to send to client connection.

Parameters:sock – Open socket connection to client
SocketWriter.write(b)[source]

Write an encoded byte string to the SocketWriter’s client connection. This is unbuffered.

Parameters:b (bytes) – An encoded byte string. We’re currently using utf-8 encodings
Returns:Number of bytes written
SocketWriter.close()[source]

Shutdown and close the client connection.